diff --git a/function.php b/function.php index cf4b770e..64f19605 100644 --- a/function.php +++ b/function.php @@ -935,7 +935,6 @@ if (!function_exists('di')) { /** * @param string $className * @return mixed - * @throws ReflectionException */ function di(string $className): mixed { diff --git a/kiri-engine/Core/DateFormat.php b/kiri-engine/Core/DateFormat.php index de00dec4..46ba8915 100644 --- a/kiri-engine/Core/DateFormat.php +++ b/kiri-engine/Core/DateFormat.php @@ -27,7 +27,7 @@ class DateFormat if ($time === null) { $time = time(); } else if (is_numeric($time)) { - $length = strlen(floatval($time)); + $length = strlen((string)$time); if ($length != 10 && $length != 13) { return false; } @@ -86,9 +86,7 @@ class DateFormat return false; } - $time = date('t', $time); - - return $time; + return date('t', $time); } /** diff --git a/kiri-engine/Core/Dtl.php b/kiri-engine/Core/Dtl.php index 926e84f9..9f50b295 100644 --- a/kiri-engine/Core/Dtl.php +++ b/kiri-engine/Core/Dtl.php @@ -21,6 +21,7 @@ class Dtl extends Component /** * Dtl constructor. * @param $params + * @throws Exception */ public function __construct($params) { @@ -35,9 +36,6 @@ class Dtl extends Component */ public function toArray(): array { - if (!is_array($this->params)) { - return ArrayAccess::toArray($this->params); - } return $this->params; } diff --git a/kiri-engine/Core/HashMap.php b/kiri-engine/Core/HashMap.php index 75ca890b..42586964 100644 --- a/kiri-engine/Core/HashMap.php +++ b/kiri-engine/Core/HashMap.php @@ -3,6 +3,7 @@ namespace Kiri\Core; use JetBrains\PhpStorm\Pure; +use ReturnTypeWillChange; class HashMap implements \ArrayAccess { @@ -62,7 +63,7 @@ class HashMap implements \ArrayAccess * @param mixed $offset * @return bool */ - public function offsetExists($offset): bool + public function offsetExists(mixed $offset): bool { return isset($this->lists[$offset]); } @@ -72,7 +73,7 @@ class HashMap implements \ArrayAccess * @param mixed $offset * @return mixed */ - #[Pure] public function offsetGet($offset): mixed + #[Pure] public function offsetGet(mixed $offset): mixed { return $this->get($offset); } @@ -82,7 +83,8 @@ class HashMap implements \ArrayAccess * @param mixed $offset * @param mixed $value */ - public function offsetSet($offset, $value) + #[ReturnTypeWillChange] + public function offsetSet(mixed $offset, mixed $value) { $this->put($offset, $value); } @@ -91,7 +93,8 @@ class HashMap implements \ArrayAccess /** * @param mixed $offset */ - public function offsetUnset($offset) + #[ReturnTypeWillChange] + public function offsetUnset(mixed $offset) { unset($this->lists[$offset]); } diff --git a/kiri-engine/Core/Help.php b/kiri-engine/Core/Help.php index 830354b3..e5ba0a9a 100644 --- a/kiri-engine/Core/Help.php +++ b/kiri-engine/Core/Help.php @@ -59,6 +59,7 @@ class Help /** * @param $xml * @return mixed + * @throws Exception */ public static function toArray($xml): mixed { diff --git a/kiri-engine/Core/Json.php b/kiri-engine/Core/Json.php index e8b25de6..a50a99d0 100644 --- a/kiri-engine/Core/Json.php +++ b/kiri-engine/Core/Json.php @@ -43,7 +43,7 @@ class Json * @param bool $asArray * @return mixed */ - public static function decode($data, $asArray = true): mixed + public static function decode($data, bool $asArray = true): mixed { if (is_array($data) || is_numeric($data)) { return $data; @@ -55,14 +55,13 @@ class Json /** * @param $code - * @param string $message - * @param array $data + * @param string|array $message + * @param array|int $data * @param int $count * @param array $exPageInfo - * @return mixed - * @throws + * @return string|bool */ - public static function to($code, $message = '', $data = [], $count = 0, $exPageInfo = []): mixed + public static function to($code, string|array $message = '', array|int $data = [], int $count = 0, array $exPageInfo = []): string|bool { $params['code'] = $code; if (!is_string($message)) { diff --git a/kiri-engine/Di/Container.php b/kiri-engine/Di/Container.php index 8138c256..9b9a760f 100644 --- a/kiri-engine/Di/Container.php +++ b/kiri-engine/Di/Container.php @@ -108,7 +108,6 @@ class Container implements ContainerInterface /** * @param $class * @return bool - * @throws ReflectionException */ public function isInterface($class): bool { @@ -212,7 +211,6 @@ class Container implements ContainerInterface * @param $className * @param $method * @return array - * @throws ReflectionException */ public function getMethodAttribute($className, $method = null): array { @@ -228,7 +226,6 @@ class Container implements ContainerInterface * @param string $class * @param string|null $property * @return ReflectionProperty|ReflectionProperty[]|null - * @throws ReflectionException */ public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array { @@ -339,9 +336,6 @@ class Container implements ContainerInterface if (!isset($this->_parameters[$class])) { $this->_parameters[$class] = []; } - if (!isset($this->_parameters[$class][$method])) { - $this->_parameters[$class][$method] = []; - } return $this->_parameters[$class][$method] = $parameters; } @@ -360,7 +354,6 @@ class Container implements ContainerInterface /** * @param ReflectionMethod|ReflectionFunction $reflectionMethod * @return array - * @throws ReflectionException */ private function resolveMethodParameters(ReflectionMethod|ReflectionFunction $reflectionMethod): array { @@ -394,7 +387,6 @@ class Container implements ContainerInterface /** * @param $class * @return ReflectionClass|null - * @throws ReflectionException */ public function getReflect($class): ?ReflectionClass { diff --git a/kiri-websocket-server/FdCollector.php b/kiri-websocket-server/FdCollector.php new file mode 100644 index 00000000..03e33214 --- /dev/null +++ b/kiri-websocket-server/FdCollector.php @@ -0,0 +1,14 @@ +router = $this->container->get(DataGrip::class)->get($this->serverName); + $handler = $this->router->find('/', 'GET'); + if (is_int($handler) || is_null($handler)) { + return; + } + $this->callback = $handler->callback[0]; + } + + + /** + * @param \Swoole\Server $server + * @param int $fd + */ + public function onClose(\Swoole\Server $server, int $fd): void + { + if ($this->callback instanceof OnCloseInterface) { + $this->callback->onClose($server, $fd); + } + } + + + /** + * @param Request $request + * @param Response $response + */ + public function onHandshake(Request $request, Response $response): void + { + try { + if (!$this->callback instanceof OnHandshakeInterface) { + throw new \Exception('Page not found.'); + } + $this->callback->onHandshake($request, $response); + + $this->afterHandshake($request); + if ($this->server instanceof \Swoole\Coroutine\Http\Server) { + while (true) { + $data = $response->recv(); + if ($data === '' || $data === false || $data instanceof CloseFrame) { + $this->onClose($this->server, $response->fd); + break; + } + $this->onMessage($this->server, $data); + } + } + } catch (\Throwable $throwable) { + $response->status(500, $throwable->getMessage()); + $response->end(); + } + } + + + /** + * @param $request + */ + public function afterHandshake($request) + { + if (!($this->callback instanceof OnOpenInterface)) { + return; + } + $this->callback->onOpen($this->server, $request); + } + + + /** + * @param \Swoole\Server $server + * @param Frame $frame + */ + public function onMessage(\Swoole\Server $server, Frame $frame): void + { + if ($this->callback instanceof OnMessageInterface) { + $this->callback->onMessage($server, $frame); + } + } +}