This commit is contained in:
as2252258@163.com
2021-07-11 02:05:56 +08:00
parent ec37380175
commit 712e6b8f2a
4 changed files with 127 additions and 156 deletions
+4 -56
View File
@@ -263,17 +263,7 @@ class HttpParams
*/
public function int(string $name, bool $isNeed = FALSE, array|int|null $min = NULL, int|null $max = NULL): ?int
{
$int = $this->required($name, $isNeed);
if (is_null($int)) return null;
if (is_array($min)) {
list($min, $max) = $min;
}
$length = strlen((string)$int);
if (!is_numeric($int) || intval($int) != $int) {
throw new RequestException("The request parameter $name must integer.");
}
$this->between($length, $min, $max);
return (int)$int;
return (int)$this->required($name, $isNeed);
}
/**
@@ -285,15 +275,7 @@ class HttpParams
*/
public function float(string $name, bool $isNeed = FALSE, int $round = 0): ?float
{
$int = $this->required($name, $isNeed);
if ($int === null) {
return null;
}
if ($round > 0) {
return round(floatval($int), $round);
} else {
return floatval($int);
}
return (float)$this->required($name, $isNeed);
}
/**
@@ -306,37 +288,7 @@ class HttpParams
*/
public function string(string $name, bool $isNeed = FALSE, int|array|null $length = NULL): ?string
{
$string = $this->required($name, $isNeed);
if ($string === null || $length === null) {
return $string;
}
if (is_numeric($length)) {
if ($length !== mb_strlen((string)$string)) {
throw new RequestException("The minimum value cannot be lower than $length, has length $length");
}
return $string;
} else {
return $this->between($string, $length[0], $length[1] ?? $length[0]);
}
}
/**
* @param $string
* @param $min
* @param $max
* @return mixed
* @throws RequestException
*/
private function between($string, $min, $max): mixed
{
$_length = mb_strlen((string)$string);
if ($min !== NULL && $_length < $min) {
throw new RequestException("The minimum value cannot be lower than $min, has length $_length");
}
if ($max !== NULL && $_length > $max) {
throw new RequestException("Maximum cannot exceed $max, has length " . $_length);
}
return $string;
return (string)$this->required($name, $isNeed);
}
/**
@@ -368,11 +320,7 @@ class HttpParams
*/
public function bool(string $name, bool $isNeed = FALSE): bool
{
$email = $this->required($name, $isNeed);
if ($email === null) {
return false;
}
return (bool)$email;
return (boolean)$this->required($name, $isNeed);
}
/**
+18 -22
View File
@@ -304,10 +304,10 @@ class Server extends HttpService
return;
}
foreach ($config['events'] as $name => $_event) {
if ($name === Event::SERVER_CLIENT_CLOSE) {
Event::on($name, $_event);
}else{
if ($name !== Event::SERVER_CLIENT_CLOSE) {
Event::on('listen ' . $config['port'] . ' ' . $name, $_event);
} else {
Event::on($name, $_event);
}
}
}
@@ -342,10 +342,8 @@ class Server extends HttpService
exit($this->addError(sprintf('Listen %s::%d fail.', $config['host'], $config['port'])));
}
if (isset($config['settings']) && is_array($config['settings'])) {
$newListener->set($config['settings']);
}
$this->onListenerBind($config);
$newListener->set($config['settings'] ?? []);
$this->onListenerBind($newListener, $config);
return $this->swoole;
}
@@ -405,9 +403,9 @@ class Server extends HttpService
* @return Packet|Websocket|Receive|Http|null
* @throws Exception
*/
private function onListenerBind($config): Packet|Websocket|Receive|Http|null
private function onListenerBind($server, $config): Packet|Websocket|Receive|Http|null
{
$this->bindServerEvent($config['type']);
$this->bindServerEvent($server, $config['type']);
$this->debug(sprintf('Check listen %s::%d -> ok', $config['host'], $config['port']));
@@ -419,18 +417,16 @@ class Server extends HttpService
* @param string $type
* @throws Exception
*/
private function bindServerEvent($type = self::TCP)
private function bindServerEvent($server, $type = self::TCP)
{
if (in_array($type, [self::PACKAGE, self::TCP])) {
$this->onBindCallback('connect', [make(OnConnect::class), 'onHandler']);
$this->onBindCallback('close', [make(OnClose::class), 'onHandler']);
if ($type == self::PACKAGE) {
$this->onBindCallback('packet', [make(OnPacket::class), 'onHandler']);
} else if ($type == self::TCP) {
$this->onBindCallback('receive', [make(OnReceive::class), 'onHandler']);
}
if (self::PACKAGE == $type) {
$this->onBindCallback($server, 'packet', [make(OnPacket::class), 'onHandler']);
} else if ($type == self::TCP) {
$this->onBindCallback($server, 'connect', [make(OnConnect::class), 'onHandler']);
$this->onBindCallback($server, 'close', [make(OnClose::class), 'onHandler']);
$this->onBindCallback($server, 'receive', [make(OnReceive::class), 'onHandler']);
} else if ($type === self::HTTP) {
$this->onBindCallback('request', [make(OnRequest::class), 'onHandler']);
$this->onBindCallback($server, 'request', [make(OnRequest::class), 'onHandler']);
} else {
throw new Exception('Unknown server type(' . $type . ').');
}
@@ -442,12 +438,12 @@ class Server extends HttpService
* @param $callback
* @throws Exception
*/
public function onBindCallback($name, $callback)
public function onBindCallback($server, $name, $callback)
{
if ($this->swoole->getCallback($name) !== null) {
if ($server->getCallback($name) !== null) {
return;
}
$this->swoole->on($name, $callback);
$server->on($name, $callback);
}