From 325ab04f20567b349dcdc7a6257143d2c8fd4572 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Fri, 23 Apr 2021 03:12:00 +0800 Subject: [PATCH] modify --- HttpServer/Client/Client.php | 3 - HttpServer/Events/OnWorkerStart.php | 133 +-- HttpServer/Events/Pipeline.php | 100 -- HttpServer/Events/Utility/DataResolve.php | 92 -- HttpServer/Route/Filter.php | 131 --- HttpServer/Route/Filter/BodyFilter.php | 26 - HttpServer/Route/Filter/Filter.php | 47 - HttpServer/Route/Filter/FilterException.php | 26 - HttpServer/Route/Filter/HeaderFilter.php | 26 - HttpServer/Route/Filter/QueryFilter.php | 26 - HttpServer/Route/Handler.php | 54 - HttpServer/Route/Node.php | 59 +- HttpServer/Route/Reduce.php | 99 +- HttpServer/Route/Router.php | 1167 +++++++++---------- HttpServer/Service/Abstracts/Tcp.php | 8 - HttpServer/Shutdown.php | 3 - System/Abstracts/BaseApplication.php | 3 +- System/Abstracts/Component.php | 113 -- System/Cache/Redis.php | 5 - System/Channel.php | 125 +- System/Error/Logger.php | 1 - System/Error/LoggerAspect.php | 7 +- System/Jwt/Jwt.php | 5 - System/Pool/Connection.php | 4 - System/Process/Process.php | 2 +- Validator/Validator.php | 3 +- 26 files changed, 775 insertions(+), 1493 deletions(-) delete mode 100644 HttpServer/Events/Pipeline.php delete mode 100644 HttpServer/Events/Utility/DataResolve.php delete mode 100644 HttpServer/Route/Filter.php delete mode 100644 HttpServer/Route/Filter/BodyFilter.php delete mode 100644 HttpServer/Route/Filter/Filter.php delete mode 100644 HttpServer/Route/Filter/FilterException.php delete mode 100644 HttpServer/Route/Filter/HeaderFilter.php delete mode 100644 HttpServer/Route/Filter/QueryFilter.php delete mode 100644 HttpServer/Route/Handler.php diff --git a/HttpServer/Client/Client.php b/HttpServer/Client/Client.php index 04ef31f8..f704a6a6 100644 --- a/HttpServer/Client/Client.php +++ b/HttpServer/Client/Client.php @@ -56,9 +56,6 @@ class Client extends ClientAbstracts if (in_array($client->getStatusCode(), [200, 201])) { return $this->structure($body, $data, $client->getHeaders()); } -// if ($client->getStatusCode() == 302) { -// return $this->get($client->getHeaders()['location']); -// } if (is_string($body)) { $message = 'Request error code ' . $client->getStatusCode(); } else { diff --git a/HttpServer/Events/OnWorkerStart.php b/HttpServer/Events/OnWorkerStart.php index 85cb5170..d3e341d2 100644 --- a/HttpServer/Events/OnWorkerStart.php +++ b/HttpServer/Events/OnWorkerStart.php @@ -20,88 +20,83 @@ class OnWorkerStart extends Callback { - /** - * @param Server $server - * @param int $worker_id - * - * @return mixed - * @throws Exception - */ - public function onHandler(Server $server, int $worker_id): void - { - putenv('state=start'); - putenv('worker=' . $worker_id); + /** + * @param Server $server + * @param int $worker_id + * + * @return mixed + * @throws Exception + */ + public function onHandler(Server $server, int $worker_id): void + { + putenv('state=start'); + putenv('worker=' . $worker_id); - $content = System::readFile(storage('runtime.php')); + $content = System::readFile(storage('runtime.php')); - $annotation = Snowflake::app()->getAnnotation(); - $annotation->setLoader(unserialize($content)); - if ($worker_id < $server->setting['worker_num']) { - $this->onWorker($server, $annotation); - } else { - $this->onTask($server, $annotation); - } - } + $annotation = Snowflake::app()->getAnnotation(); + $annotation->setLoader(unserialize($content)); + if ($worker_id < $server->setting['worker_num']) { + $this->onWorker($server, $annotation); + } else { + $this->onTask($server, $annotation); + } + } - /** - * @param Server $server - * @param int $worker_id - * @return bool - */ - private function isWorker(Server $server, int $worker_id): bool - { - return $worker_id < $server->setting['worker_num']; - } + /** + * @param Server $server + * @param int $worker_id + * @return bool + */ + private function isWorker(Server $server, int $worker_id): bool + { + return $worker_id < $server->setting['worker_num']; + } - /** - * @param Server $server - * @param Annotation $annotation - * @throws ConfigException - * @throws Exception - */ - public function onTask(Server $server, Annotation $annotation) - { - putenv('environmental=' . Snowflake::TASK); + /** + * @param Server $server + * @param Annotation $annotation + * @throws ConfigException + * @throws Exception + */ + public function onTask(Server $server, Annotation $annotation) + { + putenv('environmental=' . Snowflake::TASK); - $annotation->runtime(directory('app'), [ - CONTROLLER_PATH, - LISTENER_PATH, - SOCKET_PATH, - TASK_PATH, - ]); - name($server->worker_pid, 'Task#' . $server->worker_id); + $annotation->runtime(directory('app'), [ + CONTROLLER_PATH, + LISTENER_PATH, + SOCKET_PATH, + TASK_PATH, + ]); + name($server->worker_pid, 'Task#' . $server->worker_id); - Snowflake::setTaskId($server->worker_pid); + Snowflake::setTaskId($server->worker_pid); - fire(Event::SERVER_TASK_START); - } + fire(Event::SERVER_TASK_START); + } - /** - * @param Server $server - * @param Annotation $annotation - * @throws Exception - */ - public function onWorker(Server $server, Annotation $annotation) - { - try { - name($server->worker_pid, 'Worker#' . $server->worker_id); + /** + * @param Server $server + * @param Annotation $annotation + * @throws Exception + */ + public function onWorker(Server $server, Annotation $annotation) + { + name($server->worker_pid, 'Worker#' . $server->worker_id); - $time = microtime(true); - $annotation->runtime(CONTROLLER_PATH); - $this->debug('use time.' . (microtime(true) - $time)); - $annotation->runtime(directory('app'), CONTROLLER_PATH); + $time = microtime(true); + $annotation->runtime(CONTROLLER_PATH); + $this->debug('use time.' . (microtime(true) - $time)); + $annotation->runtime(directory('app'), CONTROLLER_PATH); - Snowflake::setWorkerId($server->worker_pid); - putenv('environmental=' . Snowflake::WORKER); + Snowflake::setWorkerId($server->worker_pid); + putenv('environmental=' . Snowflake::WORKER); - fire(Event::SERVER_WORKER_START, [getenv('worker')]); - } catch (\Throwable $exception) { - $this->addError($exception, 'throwable'); - write($exception->getMessage(), 'worker'); - } - } + fire(Event::SERVER_WORKER_START, [getenv('worker')]); + } } diff --git a/HttpServer/Events/Pipeline.php b/HttpServer/Events/Pipeline.php deleted file mode 100644 index cdc59b40..00000000 --- a/HttpServer/Events/Pipeline.php +++ /dev/null @@ -1,100 +0,0 @@ -condition = $condition; - $this->_if = $handler; - return $this; - } - - - /** - * @param \Closure $handler - * @return $this - */ - public function else(\Closure $handler): static - { - $this->_else = $handler; - return $this; - } - - - /** - * @param \Closure $handler - * @return $this - */ - public function catch(\Closure $handler): static - { - $this->_catch = $handler; - return $this; - } - - /** - * @param \Closure $handler - * @return $this - */ - public function after(\Closure $handler): static - { - $this->_after = $handler; - return $this; - } - - /** - * @param \Closure $handler - * @return $this - */ - public function before(\Closure $handler): static - { - $this->_before = $handler; - return $this; - } - - - /** - * @param $argv - * @return mixed - */ - public function exec(...$argv) - { - try { - if ($this->_before instanceof \Closure) { - call_user_func($this->_before, ...$argv); - } - if ($this->condition !== true) { - call_user_func($this->_else, ...$argv); - } else { - call_user_func($this->_if, ...$argv); - } - return $argv; - } catch (\Throwable $exception) { - call_user_func($this->_catch, $exception); - return $argv; - } finally { - if ($this->_after instanceof \Closure) { - call_user_func($this->_after, ...$argv); - } - } - } - -} diff --git a/HttpServer/Events/Utility/DataResolve.php b/HttpServer/Events/Utility/DataResolve.php deleted file mode 100644 index db275759..00000000 --- a/HttpServer/Events/Utility/DataResolve.php +++ /dev/null @@ -1,92 +0,0 @@ -get($callback[0]); - } - if (!empty($address) && !empty($port)) { - return call_user_func($callback, $address, $port, $data); - } - return call_user_func($callback, $data); - } - -} diff --git a/HttpServer/Route/Filter.php b/HttpServer/Route/Filter.php deleted file mode 100644 index 7f3fa359..00000000 --- a/HttpServer/Route/Filter.php +++ /dev/null @@ -1,131 +0,0 @@ -rules = []; - $class->params = Input()->params(); - - return $this->_filters[] = $class; - } - - - /** - * @param array $value - * @return HeaderFilter|bool - * @throws Exception - */ - public function setHeader(array $value): HeaderFilter|bool - { - if (empty($value)) { - return true; - } - - /** @var HeaderFilter $class */ - $class = Snowflake::createObject(HeaderFilter::class); - $class->rules = []; - $class->params = request()->headers->getHeaders(); - - return $this->_filters[] = $class; - } - - - /** - * @param array $value - * @return QueryFilter|bool - * @throws Exception - */ - public function setQuery(array $value): QueryFilter|bool - { - if (empty($value)) { - return true; - } - - /** @var QueryFilter $class */ - $class = Snowflake::createObject(QueryFilter::class); - $class->rules = []; - $class->params = request()->headers->getHeaders(); - - return $this->_filters[] = $class; - } - - - /** - * @throws Exception - */ - public function handler(): bool - { - if (($error = $this->filters()) !== true) { - throw new FilterException($error); - } - if (!$this->grant()) { - throw new AuthException('Authentication error.'); - } - return true; - } - - /** - * @return bool - */ - private function filters(): bool - { - if (empty($this->_filters)) { - return true; - } - foreach ($this->_filters as $filter) { - if (!$filter->check()) { - return false; - } - } - return true; - } - - /** - * @return mixed - */ - private function grant(): mixed - { - if (!is_callable($this->grant, true)) { - return true; - } - return call_user_func($this->grant); - } - -} diff --git a/HttpServer/Route/Filter/BodyFilter.php b/HttpServer/Route/Filter/BodyFilter.php deleted file mode 100644 index 66b59193..00000000 --- a/HttpServer/Route/Filter/BodyFilter.php +++ /dev/null @@ -1,26 +0,0 @@ -validator(); - } - -} diff --git a/HttpServer/Route/Filter/Filter.php b/HttpServer/Route/Filter/Filter.php deleted file mode 100644 index aa0aa87d..00000000 --- a/HttpServer/Route/Filter/Filter.php +++ /dev/null @@ -1,47 +0,0 @@ -setParams($this->params); - foreach ($this->rules as $val) { - $field = array_shift($val); - if (empty($val)) { - continue; - } - $validator->make($field, $val); - } - if (!$validator->validation()) { - return $this->addError($validator->getError()); - } - return true; - } - -} diff --git a/HttpServer/Route/Filter/FilterException.php b/HttpServer/Route/Filter/FilterException.php deleted file mode 100644 index 90c2cd87..00000000 --- a/HttpServer/Route/Filter/FilterException.php +++ /dev/null @@ -1,26 +0,0 @@ -validator(); - } - -} diff --git a/HttpServer/Route/Filter/QueryFilter.php b/HttpServer/Route/Filter/QueryFilter.php deleted file mode 100644 index 524a4fb7..00000000 --- a/HttpServer/Route/Filter/QueryFilter.php +++ /dev/null @@ -1,26 +0,0 @@ -validator(); - } - -} diff --git a/HttpServer/Route/Handler.php b/HttpServer/Route/Handler.php deleted file mode 100644 index 92b73c5b..00000000 --- a/HttpServer/Route/Handler.php +++ /dev/null @@ -1,54 +0,0 @@ -router = Snowflake::app()->router; - - parent::__construct([]); - } - - /** - * @param $config - * @param $handler - */ - public function group($config, $handler) - { - $this->router->group($config, $handler, $this); - } - - - /** - * @param $route - * @param $handler - * @return Handler|Node|null - */ - public function handler($route, $handler): Handler|Node|null - { - return $this->router->addRoute($route, $handler, 'receive'); - } - -} diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index ea56ec33..17d03ba6 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -126,11 +126,11 @@ class Node extends HttpService public function createDispatch(): Closure { return function () { - $dispatchParam = Context::getContext('dispatch-param'); - if (empty($dispatchParam)) { - $dispatchParam = [\request()]; - } - return \aop($this->handler, $dispatchParam); + $dispatchParam = Context::getContext('dispatch-param'); + if (empty($dispatchParam)) { + $dispatchParam = [\request()]; + } + return \aop($this->handler, $dispatchParam); }; } @@ -276,31 +276,10 @@ class Node extends HttpService return false; } } - return $this->checkRule(); - } - - /** - * @return bool - * @throws Exception - */ - private function checkRule(): bool - { - if (empty($this->rules)) { - return true; - } - foreach ($this->rules as $rule) { - if (!isset($rule['class'])) { - $rule['class'] = Filter::class; - } - /** @var Filter $object */ - $object = Snowflake::createObject($rule); - if (!$object->handler()) { - return false; - } - } return true; } + /** * @param string $controller * @param string $action @@ -518,19 +497,11 @@ class Node extends HttpService */ public function dispatch(): mixed { - try { - Context::setContext('dispatch-param', func_get_args()); - if (empty($this->callback)) { - return Json::to(404, $this->errorMsg()); - } - return $this->httpFilter(); - } catch (Throwable $throwable) { - $this->addError($throwable, 'throwable'); - - $code = $throwable->getCode() == 0 ? 500 : $throwable->getCode(); - - return Json::to($code, $throwable->getMessage()); + Context::setContext('dispatch-param', func_get_args()); + if (empty($this->callback)) { + return Json::to(404, $this->errorMsg()); } + return $this->httpFilter(); } @@ -548,11 +519,11 @@ class Node extends HttpService } - /** - * @param $dispatchParams - * @return mixed - * @throws Exception - */ + /** + * @param $dispatchParams + * @return mixed + * @throws Exception + */ private function runValidator($dispatchParams): mixed { /** @var HttpFilter $filter */ diff --git a/HttpServer/Route/Reduce.php b/HttpServer/Route/Reduce.php index b4e10a18..2c3147b4 100644 --- a/HttpServer/Route/Reduce.php +++ b/HttpServer/Route/Reduce.php @@ -13,63 +13,58 @@ class Reduce { - /** - * @param $last - * @param $middleWares - * @return mixed - */ - public static function reduce($last, $middleWares): mixed - { - return array_reduce(array_reverse($middleWares), static::core(), $last); - } + /** + * @param $last + * @param $middleWares + * @return mixed + */ + public static function reduce($last, $middleWares): mixed + { + return array_reduce(array_reverse($middleWares), static::core(), $last); + } - /** - * @param $middleWares - * @return mixed - */ - public static function after($middleWares): mixed - { - return array_reduce(array_reverse($middleWares), function ($stack, $pipe) { - return function ($request, $passable) use ($stack, $pipe) { - try { - if (!($pipe instanceof After)) { - return call_user_func($pipe, $request, $passable, $stack); - } - return $pipe->onHandler($request, $passable); - } catch (\Throwable $throwable) { - logger()->addError($throwable, 'throwable'); - return Json::to(0, $throwable); - } - }; - }); - } + /** + * @param $middleWares + * @return mixed + */ + public static function after($middleWares): mixed + { + return array_reduce(array_reverse($middleWares), function ($stack, $pipe) { + return function ($request, $passable) use ($stack, $pipe) { + if (!($pipe instanceof After)) { + return call_user_func($pipe, $request, $passable, $stack); + } + return $pipe->onHandler($request, $passable); + }; + }); + } - /** - * @return Closure - */ - private static function core(): Closure - { - return function ($stack, $pipe) { - return static::passable($stack, $pipe); - }; - } + /** + * @return Closure + */ + private static function core(): Closure + { + return function ($stack, $pipe) { + return static::passable($stack, $pipe); + }; + } - /** - * @param $stack - * @param $pipe - * @return Closure - */ - public static function passable($stack, $pipe): Closure - { - return function ($passable) use ($stack, $pipe) { - if ($pipe instanceof Middleware) { - return $pipe->onHandler($passable, $stack); - } - return call_user_func($pipe, $passable, $stack); - }; - } + /** + * @param $stack + * @param $pipe + * @return Closure + */ + public static function passable($stack, $pipe): Closure + { + return function ($passable) use ($stack, $pipe) { + if ($pipe instanceof Middleware) { + return $pipe->onHandler($passable, $stack); + } + return call_user_func($pipe, $passable, $stack); + }; + } } diff --git a/HttpServer/Route/Router.php b/HttpServer/Route/Router.php index 96b7dbf4..42d788f1 100644 --- a/HttpServer/Route/Router.php +++ b/HttpServer/Route/Router.php @@ -27,689 +27,670 @@ defined('ROUTER_HASH') or define('ROUTER_HASH', 2); */ class Router extends HttpService implements RouterInterface { - /** @var Node[] $nodes */ - public array $nodes = []; - public array $groupTacks = []; - public ?string $dir = 'App\\Http\\Controllers'; + /** @var Node[] $nodes */ + public array $nodes = []; + public array $groupTacks = []; + public ?string $dir = 'App\\Http\\Controllers'; - public array $hashMap = []; + public array $hashMap = []; - const NOT_FOUND = 'Page not found or method not allowed.'; + const NOT_FOUND = 'Page not found or method not allowed.'; - /** @var string[] */ - public array $methods = ['get', 'post', 'options', 'put', 'delete', 'receive']; + /** @var string[] */ + public array $methods = ['get', 'post', 'options', 'put', 'delete', 'receive']; - public ?Closure $middleware = null; + public ?Closure $middleware = null; - public int $useTree = ROUTER_TREE; + public int $useTree = ROUTER_TREE; - /** - * @param Closure $middleware - */ - public function setMiddleware(\Closure $middleware): void - { - $this->middleware = $middleware; - } + /** + * @param Closure $middleware + */ + public function setMiddleware(\Closure $middleware): void + { + $this->middleware = $middleware; + } - /** - * @throws ConfigException - * 初始化函数路径 - */ - public function init() - { - $this->dir = Config::get('http.namespace', $this->dir); - } + /** + * @throws ConfigException + * 初始化函数路径 + */ + public function init() + { + $this->dir = Config::get('http.namespace', $this->dir); + } - /** - * @param bool $useTree - */ - public function setUseTree(bool $useTree): void - { - $this->useTree = $useTree ? ROUTER_TREE : ROUTER_HASH; - } + /** + * @param bool $useTree + */ + public function setUseTree(bool $useTree): void + { + $this->useTree = $useTree ? ROUTER_TREE : ROUTER_HASH; + } - /** - * @param $port - * @param Closure|array|string $closure - * @param null $method - * @return Node|bool|null - * @throws - */ - public function addPortListen($port, Closure|array|string $closure, $method = null): Node|null|bool - { - if (!is_string($closure)) { - return $this->addRoute('add-port-listen/port_' . $port, $closure, 'listen'); - } - if (empty($method)) { - return $this->addError($closure . '::' . $method); - } - $_closure = Snowflake::createObject($closure); - if (!method_exists($_closure, $method)) { - return $this->addError($closure . '::' . $method); - } - return $this->addRoute('add-port-listen/port_' . $port, [$_closure, $method], 'listen'); - } + /** + * @param $port + * @param Closure|array|string $closure + * @param null $method + * @return Node|bool|null + * @throws + */ + public function addPortListen($port, Closure|array|string $closure, $method = null): Node|null|bool + { + if (!is_string($closure)) { + return $this->addRoute('add-port-listen/port_' . $port, $closure, 'listen'); + } + if (empty($method)) { + return $this->addError($closure . '::' . $method); + } + $_closure = Snowflake::createObject($closure); + if (!method_exists($_closure, $method)) { + return $this->addError($closure . '::' . $method); + } + return $this->addRoute('add-port-listen/port_' . $port, [$_closure, $method], 'listen'); + } - /** - * @param $path - * @param $handler - * @param string $method - * @return ?Node - */ - public function addRoute($path, $handler, $method = 'any'): ?Node - { - $method = strtolower($method); - if (!isset($this->nodes[$method])) { - $this->nodes[$method] = []; - } + /** + * @param $path + * @param $handler + * @param string $method + * @return ?Node + */ + public function addRoute($path, $handler, $method = 'any'): ?Node + { + $method = strtolower($method); + if (!isset($this->nodes[$method])) { + $this->nodes[$method] = []; + } - if ($handler instanceof Closure) { - $handler = Closure::bind($handler, new Controller()); - } + if ($handler instanceof Closure) { + $handler = Closure::bind($handler, new Controller()); + } - if ($this->useTree == ROUTER_TREE) { - return $this->tree($path, $handler, $method); - } else { - return $this->hash($path, $handler, $method); - } - } + if ($this->useTree == ROUTER_TREE) { + return $this->tree($path, $handler, $method); + } else { + return $this->hash($path, $handler, $method); + } + } - /** - * @param $path - * @param $handler - * @param string $method - * @return ?Node - */ - private function hash($path, $handler, $method = 'any'): ?Node - { - $path = $this->resolve($path); + /** + * @param $path + * @param $handler + * @param string $method + * @return ?Node + */ + private function hash($path, $handler, $method = 'any'): ?Node + { + $path = $this->resolve($path); - $this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method); + $this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method); - return $this->nodes[$method][$path]->bindHandler($handler); - } + return $this->nodes[$method][$path]->bindHandler($handler); + } - /** - * @param $path - * @return string - */ - #[Pure] private function resolve($path): string - { - $paths = array_column($this->groupTacks, 'prefix'); - if (empty($paths)) { - return '/' . ltrim($path, '/'); - } - $paths = '/' . implode('/', $paths); - if ($path != '/') { - return $paths . '/' . ltrim($path, '/'); - } - return $paths . '/'; - } + /** + * @param $path + * @return string + */ + #[Pure] private function resolve($path): string + { + $paths = array_column($this->groupTacks, 'prefix'); + if (empty($paths)) { + return '/' . ltrim($path, '/'); + } + $paths = '/' . implode('/', $paths); + if ($path != '/') { + return $paths . '/' . ltrim($path, '/'); + } + return $paths . '/'; + } - /** - * @param $path - * @param $handler - * @param string $method - * @return Node - */ - private function tree($path, $handler, $method = 'any'): Node - { - list($first, $explode) = $this->split($path); + /** + * @param $path + * @param $handler + * @param string $method + * @return Node + */ + private function tree($path, $handler, $method = 'any'): Node + { + list($first, $explode) = $this->split($path); - $parent = $this->nodes[$method][$first] ?? null; - if (empty($parent)) { - $this->nodes[$method][$first] = $parent = $this->NodeInstance('/', 0, $method); - } + $parent = $this->nodes[$method][$first] ?? null; + if (empty($parent)) { + $this->nodes[$method][$first] = $parent = $this->NodeInstance('/', 0, $method); + } - if ($first !== '/') { - $parent = $this->bindNode($parent, $explode, $method); - } - $parent->path = $path; - return $parent->bindHandler($handler); - } + if ($first !== '/') { + $parent = $this->bindNode($parent, $explode, $method); + } + $parent->path = $path; + return $parent->bindHandler($handler); + } - /** - * @param Node $parent - * @param array $explode - * @param $method - * @return Node - */ - private function bindNode(Node $parent, array $explode, $method): Node - { - $a = 0; - if (empty($explode)) { - return $parent->addChild($this->NodeInstance('/', $a, $method), '/'); - } - foreach ($explode as $value) { - if (empty($value)) { - continue; - } - ++$a; + /** + * @param Node $parent + * @param array $explode + * @param $method + * @return Node + */ + private function bindNode(Node $parent, array $explode, $method): Node + { + $a = 0; + if (empty($explode)) { + return $parent->addChild($this->NodeInstance('/', $a, $method), '/'); + } + foreach ($explode as $value) { + if (empty($value)) { + continue; + } + ++$a; - $search = $parent->findNode($value); - if ($search === null) { - $parent = $parent->addChild($this->NodeInstance($value, $a, $method), $value); - } else { - $parent = $search; - } - } - return $parent; - } + $search = $parent->findNode($value); + if ($search === null) { + $parent = $parent->addChild($this->NodeInstance($value, $a, $method), $value); + } else { + $parent = $search; + } + } + return $parent; + } - /** - * @param $route - * @param $handler - * @return Node|null - * @throws - */ - public function socket($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'socket'); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function socket($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'socket'); + } - /** - * @param $route - * @param $handler - * @return Node|null - * @throws - */ - public function post($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'post'); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function post($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'post'); + } - /** - * @param $route - * @param $handler - * @return Node|null - * @throws - */ - public function get($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'get'); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function get($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'get'); + } - /** - * @param $route - * @param $handler - * @return Node|null - * @throws - */ - public function options($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'options'); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function options($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'options'); + } - /** - * @param $port - * @param Closure $closure - * @throws - */ - public function listen(int $port, Closure $closure) - { - $stdClass = Snowflake::createObject(Handler::class); - $this->group(['prefix' => $port], $closure, $stdClass); - } + /** + * @param $route + * @param $handler + * @return Any + * @throws + */ + public function any($route, $handler): Any + { + $nodes = []; + foreach (['get', 'post', 'options', 'put', 'delete'] as $method) { + $nodes[] = $this->addRoute($route, $handler, $method); + } + return new Any($nodes); + } - /** - * @param $route - * @param $handler - * @return Any - * @throws - */ - public function any($route, $handler): Any - { - $nodes = []; - foreach (['get', 'post', 'options', 'put', 'delete'] as $method) { - $nodes[] = $this->addRoute($route, $handler, $method); - } - return new Any($nodes); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function delete($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'delete'); + } - /** - * @param $route - * @param $handler - * @return Node|null - * @throws - */ - public function delete($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'delete'); - } + /** + * @param $route + * @param $handler + * @return Node|null + * @throws + */ + public function put($route, $handler): ?Node + { + return $this->addRoute($route, $handler, 'put'); + } - /** - * @param $route - * @param $handler - * @return Node|null - * @throws - */ - public function put($route, $handler): ?Node - { - return $this->addRoute($route, $handler, 'put'); - } + /** + * @param $value + * @param $index + * @param $method + * @return Node + * @throws + */ + public function NodeInstance($value, $index = 0, $method = 'get'): Node + { + $node = new Node(); + $node->childes = []; + $node->path = $value; + $node->index = $index; + $node->method = $method; + $node->namespace = $this->loadNamespace($method); - /** - * @param $value - * @param $index - * @param $method - * @return Node - * @throws - */ - public function NodeInstance($value, $index = 0, $method = 'get'): Node - { - $node = new Node(); - $node->childes = []; - $node->path = $value; - $node->index = $index; - $node->method = $method; - $node->namespace = $this->loadNamespace($method); + $name = array_column($this->groupTacks, 'middleware'); + if ($this->middleware instanceof \Closure) { + $node->addMiddleware([$this->middleware]); + } - $name = array_column($this->groupTacks, 'middleware'); - if ($this->middleware instanceof \Closure) { - $node->addMiddleware([$this->middleware]); - } + if (is_array($name)) { + $node->addMiddleware($this->resolve_middleware($name)); + } - if (is_array($name)) { - $node->addMiddleware($this->resolve_middleware($name)); - } - - return $node; - } + return $node; + } - /** - * @param string|array $middleware - * @return array - * @throws NotFindClassException - * @throws ReflectionException - */ - private function resolve_middleware(string|array $middleware): array - { - if (is_string($middleware)) { - $middleware = [$middleware]; - } + /** + * @param string|array $middleware + * @return array + * @throws NotFindClassException + * @throws ReflectionException + */ + private function resolve_middleware(string|array $middleware): array + { + if (is_string($middleware)) { + $middleware = [$middleware]; + } - $array = []; - foreach ($middleware as $value) { - if (is_array($value)) { - foreach ($value as $item) { - $array[] = $this->getMiddlewareInstance($item); - } - } else { - $array[] = $this->getMiddlewareInstance($value); - } - } - return $array; - } + $array = []; + foreach ($middleware as $value) { + if (is_array($value)) { + foreach ($value as $item) { + $array[] = $this->getMiddlewareInstance($item); + } + } else { + $array[] = $this->getMiddlewareInstance($value); + } + } + return $array; + } - /** - * @param $value - * @return Closure|array|null - * @throws NotFindClassException - * @throws ReflectionException - */ - private function getMiddlewareInstance($value): null|Closure|array - { - if (is_string($value)) { - $value = Snowflake::createObject($value); - if (!($value instanceof Middleware)) { - return null; - } - return [$value, 'onHandler']; - } else { - return $value; - } - } + /** + * @param $value + * @return Closure|array|null + * @throws NotFindClassException + * @throws ReflectionException + */ + private function getMiddlewareInstance($value): null|Closure|array + { + if (is_string($value)) { + $value = Snowflake::createObject($value); + if (!($value instanceof Middleware)) { + return null; + } + return [$value, 'onHandler']; + } else { + return $value; + } + } - /** - * @param $method - * @return array - */ - private function loadNamespace($method): array - { - $name = array_column($this->groupTacks, 'namespace'); - if ($method == 'package' || $method == 'receive') { - $dir = 'App\\Listener'; - } else { - $dir = $this->dir; - } - array_unshift($name, $dir); - return array_filter($name); - } + /** + * @param $method + * @return array + */ + private function loadNamespace($method): array + { + $name = array_column($this->groupTacks, 'namespace'); + if ($method == 'package' || $method == 'receive') { + $dir = 'App\\Listener'; + } else { + $dir = $this->dir; + } + array_unshift($name, $dir); + return array_filter($name); + } - /** - * @param array $config - * @param callable $callback - * 路由分组 - * @param null $stdClass - */ - public function group(array $config, callable $callback, $stdClass = null) - { - $this->groupTacks[] = $config; - if ($stdClass) { - $callback($stdClass); - } else { - $callback($this); - } - array_pop($this->groupTacks); - } + /** + * @param array $config + * @param callable $callback + * 路由分组 + * @param null $stdClass + */ + public function group(array $config, callable $callback, $stdClass = null) + { + $this->groupTacks[] = $config; + if ($stdClass) { + $callback($stdClass); + } else { + $callback($this); + } + array_pop($this->groupTacks); + } - /** - * @return string - */ - public function addPrefix(): string - { - $prefix = array_column($this->groupTacks, 'prefix'); + /** + * @return string + */ + public function addPrefix(): string + { + $prefix = array_column($this->groupTacks, 'prefix'); - $prefix = array_filter($prefix); + $prefix = array_filter($prefix); - if (empty($prefix)) { - return ''; - } + if (empty($prefix)) { + return ''; + } - return '/' . implode('/', $prefix); - } + return '/' . implode('/', $prefix); + } - /** - * @param array|null $explode - * @param $method - * @return Node|null - * 查找指定路由 - */ - public function tree_search(?array $explode, $method): ?Node - { - if (empty($explode)) { - return $this->nodes[$method]['/'] ?? null; - } - $first = array_shift($explode); - if (!($parent = $this->nodes[$method][$first] ?? null)) { - return null; - } - if (empty($explode)) { - return $parent->findNode('/'); - } - while ($value = array_shift($explode)) { - $node = $parent->findNode($value); - if (!$node) { - break; - } - $parent = $node; - } - return $parent; - } + /** + * @param array|null $explode + * @param $method + * @return Node|null + * 查找指定路由 + */ + public function tree_search(?array $explode, $method): ?Node + { + if (empty($explode)) { + return $this->nodes[$method]['/'] ?? null; + } + $first = array_shift($explode); + if (!($parent = $this->nodes[$method][$first] ?? null)) { + return null; + } + if (empty($explode)) { + return $parent->findNode('/'); + } + while ($value = array_shift($explode)) { + $node = $parent->findNode($value); + if (!$node) { + break; + } + $parent = $node; + } + return $parent; + } - /** - * @param $path - * @return array - * '*' - */ - public function split($path): array - { - $prefix = $this->addPrefix(); - $path = ltrim($path, '/'); - if (!empty($prefix)) { - $path = $prefix . '/' . $path; - } + /** + * @param $path + * @return array + * '*' + */ + public function split($path): array + { + $prefix = $this->addPrefix(); + $path = ltrim($path, '/'); + if (!empty($prefix)) { + $path = $prefix . '/' . $path; + } - $explode = array_filter(explode('/', $path)); - if (empty($explode)) { - return ['/', []]; - } + $explode = array_filter(explode('/', $path)); + if (empty($explode)) { + return ['/', []]; + } - $first = array_shift($explode); - if (empty($explode)) { - $explode = []; - } - return [$first, $explode]; - } + $first = array_shift($explode); + if (empty($explode)) { + $explode = []; + } + return [$first, $explode]; + } - /** - * @return array - */ - public function each(): array - { - $paths = []; - foreach ($this->nodes as $node) { - /** @var Node[] $node */ - foreach ($node as $_node) { + /** + * @return array + */ + public function each(): array + { + $paths = []; + foreach ($this->nodes as $node) { + /** @var Node[] $node */ + foreach ($node as $_node) { // if ($_node->path == '/') { // continue; // } - $path = strtoupper($_node->method) . ' : ' . $_node->path; - if (!empty($_node->childes)) { - $path = $this->readByChild($_node->childes, $path); - } - $paths[] = $path; - } - } - return $this->readByArray($paths); - } + $path = strtoupper($_node->method) . ' : ' . $_node->path; + if (!empty($_node->childes)) { + $path = $this->readByChild($_node->childes, $path); + } + $paths[] = $path; + } + } + return $this->readByArray($paths); + } - /** - * @param $array - * @param array $returns - * @return array - */ - private function readByArray($array, $returns = []): array - { - foreach ($array as $value) { - if (empty($value)) { - continue; - } - if (is_array($value)) { - $returns = $this->readByArray($value, $returns); - } else { - [$method, $route] = explode(' : ', $value); + /** + * @param $array + * @param array $returns + * @return array + */ + private function readByArray($array, $returns = []): array + { + foreach ($array as $value) { + if (empty($value)) { + continue; + } + if (is_array($value)) { + $returns = $this->readByArray($value, $returns); + } else { + [$method, $route] = explode(' : ', $value); - $returns[] = ['method' => $method, 'route' => $route]; - } - } - return $returns; - } + $returns[] = ['method' => $method, 'route' => $route]; + } + } + return $returns; + } - /** - * @param $child - * @param string $paths - * @return array - */ - private function readByChild($child, $paths = ''): array - { - $newPath = []; - /** @var Node $item */ - foreach ($child as $item) { + /** + * @param $child + * @param string $paths + * @return array + */ + private function readByChild($child, $paths = ''): array + { + $newPath = []; + /** @var Node $item */ + foreach ($child as $item) { // if ($item->path == '/') { // continue; // } - if (!empty($item->childes)) { - $newPath[] = $this->readByChild($item->childes, $paths . '/' . $item->path); - } else { - [$first, $route] = explode(' : ', $paths); + if (!empty($item->childes)) { + $newPath[] = $this->readByChild($item->childes, $paths . '/' . $item->path); + } else { + [$first, $route] = explode(' : ', $paths); - $newPath[] = strtoupper($item->method) . ' : ' . $route . '/' . $item->path; - } - } - return $newPath; - } + $newPath[] = strtoupper($item->method) . ' : ' . $route . '/' . $item->path; + } + } + return $newPath; + } - /** - * @return mixed - * @throws - */ - public function dispatch(): mixed - { - try { - if (!($node = $this->find_path(\request()))) { - return send(self::NOT_FOUND); - } - send($response = $node->dispatch(), 200); - if (!$node->hasAfter()) { - return null; - } - return $node->afterDispatch($response); - } catch (\Throwable $exception) { - $this->addError($exception, 'throwable'); - - $Code = $exception->getCode() == 0 ? 500 : $exception->getCode(); - - return send(Json::to($Code, $exception->getMessage()), 200); - } - } + /** + * @return mixed + * @throws + */ + public function dispatch(): mixed + { + if (!($node = $this->find_path(\request()))) { + return send(self::NOT_FOUND); + } + send($response = $node->dispatch(), 200); + if (!$node->hasAfter()) { + return null; + } + return $node->afterDispatch($response); + } - /** - * @param $exception - * @return mixed - * @throws Exception - */ - private function exception($exception): mixed - { - return Snowflake::app()->getLogger()->exception($exception); - } + /** + * @param $exception + * @return mixed + * @throws Exception + */ + private function exception($exception): mixed + { + return Snowflake::app()->getLogger()->exception($exception); + } - /** - * @param Request $request - * @return Node|null 树干搜索 - * 树干搜索 - */ - public function find_path(Request $request): ?Node - { - if ($this->useTree === ROUTER_TREE) { - return $this->Branch_search($request); - } + /** + * @param Request $request + * @return Node|null 树干搜索 + * 树干搜索 + */ + public function find_path(Request $request): ?Node + { + if ($this->useTree === ROUTER_TREE) { + return $this->Branch_search($request); + } - $method = $request->getMethod(); - $uri = $request->headers->get('request_uri', '/'); + $method = $request->getMethod(); + $uri = $request->headers->get('request_uri', '/'); - if (!isset($this->nodes[$method])) { - return null; - } - $methods = $this->nodes[$method]; - if (isset($methods[$uri])) { - return $methods[$uri]; - } - if (!$request->isOption || !isset($methods['/'])) { - return null; - } - return $methods['/']; - } + if (!isset($this->nodes[$method])) { + return null; + } + $methods = $this->nodes[$method]; + if (isset($methods[$uri])) { + return $methods[$uri]; + } + if (!$request->isOption || !isset($methods['/'])) { + return null; + } + return $methods['/']; + } - /** - * @param $uri - * @param $method - * @return Node|null - */ - public function search($uri, $method): Node|null - { - if (!isset($this->nodes[$method])) { - return null; - } - $methods = $this->nodes[$method]; - if (isset($methods[$uri])) { - return $methods[$uri]; - } - return $methods['/'] ?? null; - } + /** + * @param $uri + * @param $method + * @return Node|null + */ + public function search($uri, $method): Node|null + { + if (!isset($this->nodes[$method])) { + return null; + } + $methods = $this->nodes[$method]; + if (isset($methods[$uri])) { + return $methods[$uri]; + } + return $methods['/'] ?? null; + } - /** - * @param $request - * @return Node|null - */ - private function search_options($request): ?Node - { - $method = $request->getMethod(); - if (!isset($this->nodes[$method])) { - return null; - } - if (!isset($this->nodes[$method]['*'])) { - return null; - } - return $this->nodes[$method]['*']; - } + /** + * @param $request + * @return Node|null + */ + private function search_options($request): ?Node + { + $method = $request->getMethod(); + if (!isset($this->nodes[$method])) { + return null; + } + if (!isset($this->nodes[$method]['*'])) { + return null; + } + return $this->nodes[$method]['*']; + } - /** - * @param Request $request - * @return Node|null - * 树杈搜索 - */ - private function Branch_search(Request $request): ?Node - { - $node = $this->tree_search($request->getExplode(), $request->getMethod()); - if ($node instanceof Node) { - return $node; - } - if (!$request->isOption) { - return null; - } - $node = $this->tree_search(['*'], $request->getMethod()); - if (!($node instanceof Node)) { - return null; - } - return $node; - } + /** + * @param Request $request + * @return Node|null + * 树杈搜索 + */ + private function Branch_search(Request $request): ?Node + { + $node = $this->tree_search($request->getExplode(), $request->getMethod()); + if ($node instanceof Node) { + return $node; + } + if (!$request->isOption) { + return null; + } + $node = $this->tree_search(['*'], $request->getMethod()); + if (!($node instanceof Node)) { + return null; + } + return $node; + } - /** - * @throws - */ - public function _loader() - { - $this->loadRouteDir(APP_PATH . 'routes'); - } + /** + * @throws + */ + public function _loader() + { + $this->loadRouteDir(APP_PATH . 'routes'); + } - /** - * @param $path - * @throws Exception - * 加载目录下的路由文件 - */ - private function loadRouteDir($path) - { - $files = glob($path . '/*'); - for ($i = 0; $i < count($files); $i++) { - if (is_dir($files[$i])) { - $this->loadRouteDir($files[$i]); - } else { - $this->loadRouterFile($files[$i]); - } - } - } + /** + * @param $path + * @throws Exception + * 加载目录下的路由文件 + */ + private function loadRouteDir($path) + { + $files = glob($path . '/*'); + for ($i = 0; $i < count($files); $i++) { + if (is_dir($files[$i])) { + $this->loadRouteDir($files[$i]); + } else { + $this->loadRouterFile($files[$i]); + } + } + } - /** - * @param $files - * @throws Exception - */ - private function loadRouterFile($files) - { - try { - $router = $this; - include_once "{$files}"; - } catch (\Throwable $exception) { - $this->addError($exception, 'throwable'); - } finally { - if (isset($exception)) { - unset($exception); - } - } - } + /** + * @param $files + * @throws Exception + */ + private function loadRouterFile($files) + { + try { + $router = $this; + include_once "{$files}"; + } catch (\Throwable $exception) { + $this->addError($exception, 'throwable'); + } finally { + if (isset($exception)) { + unset($exception); + } + } + } } diff --git a/HttpServer/Service/Abstracts/Tcp.php b/HttpServer/Service/Abstracts/Tcp.php index 2c17d86c..2ed5ef30 100644 --- a/HttpServer/Service/Abstracts/Tcp.php +++ b/HttpServer/Service/Abstracts/Tcp.php @@ -13,12 +13,4 @@ abstract class Tcp extends Server implements Service use \HttpServer\Service\Abstracts\Server; - /** @var Closure|array */ - public array|Closure $unpack; - - - /** @var Closure|array */ - public array|Closure $pack; - - } diff --git a/HttpServer/Shutdown.php b/HttpServer/Shutdown.php index 968c556d..333d5871 100644 --- a/HttpServer/Shutdown.php +++ b/HttpServer/Shutdown.php @@ -4,11 +4,8 @@ namespace HttpServer; -use Annotation\Aspect; -use Database\InjectProperty; use Exception; use Snowflake\Abstracts\Component; -use Snowflake\Abstracts\Config; use Snowflake\Exception\ConfigException; diff --git a/System/Abstracts/BaseApplication.php b/System/Abstracts/BaseApplication.php index aae101b2..7b8fc215 100644 --- a/System/Abstracts/BaseApplication.php +++ b/System/Abstracts/BaseApplication.php @@ -154,8 +154,9 @@ abstract class BaseApplication extends Service */ public function parseInt($config) { + $_config = Snowflake::app()->getConfig(); foreach ($config as $key => $value) { - Config::set($key, $value); + $_config->setData($key, $value); } if ($storage = Config::get('storage', 'storage')) { if (!str_contains($storage, APP_PATH)) { diff --git a/System/Abstracts/Component.php b/System/Abstracts/Component.php index 7515159b..10eb9b3c 100644 --- a/System/Abstracts/Component.php +++ b/System/Abstracts/Component.php @@ -23,119 +23,6 @@ use Snowflake\Snowflake; class Component extends BaseObject { - /** - * @var array - */ - private array $_events = []; - - - /** - * @param $name [事件名称] - * @param $callback [回调函数] - * @param array $param [函数参数] - * - * { - * 事件名, 回调, 参数 - * } - */ - public function on($name, $callback, $param = []) - { - if (isset($this->_events[$name])) { - array_push($this->_events[$name], [$callback, $param]); - } else { - $this->_events[$name][] = [$callback, $param]; - } - } - - /** - * @param $name - * @param null $callback - * @return bool - */ - #[Pure] public function hasEvent($name, $callback = null): bool - { - if (!isset($this->_events[$name])) { - return false; - } - if (!is_array($this->_events[$name])) { - return false; - } - foreach ($this->_events[$name] as $event) { - [$_callback, $param] = $event; - if ($_callback === $callback) { - return true; - } - } - return false; - } - - /** - * @param $name - * @param null $event - * @param array $params - * @param bool $isRemove - * @throws Exception - */ - public function trigger($name, $event = null, $params = [], $isRemove = false) - { - $aEvents = Snowflake::app()->getEvent(); - if (isset($this->_events[$name])) { - $events = $this->_events[$name]; - foreach ($events as $key => $_event) { - if (!empty($event)) { - $_event = $event; - } - call_user_func($_event, ...$params); - if ($isRemove) { - unset($this->_events[$name][$key]); - $aEvents->of($name, $_event); - } - } - } - $aEvents->trigger($name, $event); - } - - /** - * @param $name - * @param null $handler - * @return void - * @throws ComponentException - */ - public function off($name, $handler = NULL): void - { - $aEvents = Snowflake::app()->getEvent(); - if (!isset($this->_events[$name])) { - $aEvents->of($name, $handler); - return; - } - - if (empty($handler)) { - unset($this->_events[$name]); - - $aEvents->of($name, $handler); - return; - } - - foreach ($this->_events[$name] as $key => $val) { - if ($val[0] != $handler) { - continue; - } - unset($this->_events[$name][$key]); - - break; - } - $aEvents->of($name, $handler); - } - - /** - */ - public function offAll() - { - $this->_events = []; - $aEvents = Snowflake::app()->getEvent(); - $aEvents->clean(); - } - /** * @param $name diff --git a/System/Cache/Redis.php b/System/Cache/Redis.php index fe13b305..06a95978 100644 --- a/System/Cache/Redis.php +++ b/System/Cache/Redis.php @@ -9,16 +9,11 @@ declare(strict_types=1); namespace Snowflake\Cache; -use Annotation\Aspect; -use Database\InjectProperty; use Exception; -use ReflectionException; use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Config; use Snowflake\Event; -use Snowflake\Exception\ComponentException; use Snowflake\Exception\ConfigException; -use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; /** diff --git a/System/Channel.php b/System/Channel.php index ff557b70..e0aee59d 100644 --- a/System/Channel.php +++ b/System/Channel.php @@ -18,72 +18,79 @@ class Channel extends Component { - private array $_channels = []; + private array $_channels = []; - /** - * @param mixed $value - * @param string $name - * @throws Exception - */ - public function push(mixed $value, string $name = ''): void - { - $channel = $this->channelInit($name); - $channel->enqueue($value); - } + /** + * @param mixed $value + * @param string $name + * @throws Exception + */ + public function push(mixed $value, string $name = ''): void + { + unset($value); + return; + $channel = $this->channelInit($name); + $channel->enqueue($value); + } - /** - * @param string $name - * @return bool|SplQueue - */ - private function channelInit(string $name = ''): bool|SplQueue - { - if (!isset($this->_channels[$name]) || !($this->_channels[$name] instanceof SplQueue)) { - $this->_channels[$name] = new SplQueue(); - } - return $this->_channels[$name]; - } + /** + * @param string $name + * @return bool|SplQueue + */ + private function channelInit(string $name = ''): bool|SplQueue + { + if (!isset($this->_channels[$name]) || !($this->_channels[$name] instanceof SplQueue)) { + $this->_channels[$name] = new SplQueue(); + } + return $this->_channels[$name]; + } - /** - * - * 清空缓存 - */ - public function cleanAll() - { - /** @var SplQueue $channel */ - foreach ($this->_channels as $channel) { - while ($channel->count() > 0) { - $channel->dequeue(); - } - } - $this->_channels = []; - } + /** + * + * 清空缓存 + */ + public function cleanAll() + { + /** @var SplQueue $channel */ + foreach ($this->_channels as $channel) { + if (!($channel instanceof SplQueue)) { + continue; + } + while ($channel->count() > 0) { + $channel->dequeue(); + } + } + $this->_channels = []; + } - /** - * @param $timeout - * @param Closure $closure - * @param string $name - * @return mixed - * @throws Exception - */ - public function pop(string $name, Closure $closure, int|float $timeout = null): mixed - { - if (($channel = $this->channelInit($name)) == false) { - return $this->addError('Channel is full.'); - } - if (!$channel->isEmpty()) { - return $channel->pop(); - } - if ($timeout !== null) { - $data = $channel->dequeue(); - } - if (empty($data)) { - $data = call_user_func($closure); - } - return $data; - } + /** + * @param $timeout + * @param Closure $closure + * @param string $name + * @return mixed + * @throws Exception + */ + public function pop(string $name, Closure $closure, int|float $timeout = null): mixed + { + return call_user_func($closure); + + if (($channel = $this->channelInit($name)) == false) { + return $this->addError('Channel is full.'); + } + if (!$channel->isEmpty()) { + return $channel->shift(); + } + if ($timeout !== null) { + $data = $channel->dequeue(); + } + if (empty($data)) { + $data = call_user_func($closure); + } + return $data; + } } diff --git a/System/Error/Logger.php b/System/Error/Logger.php index df239ca3..13d6cc7f 100644 --- a/System/Error/Logger.php +++ b/System/Error/Logger.php @@ -29,7 +29,6 @@ class Logger extends Component private array $logs = []; - public int $worker_id; /** * @param $message diff --git a/System/Error/LoggerAspect.php b/System/Error/LoggerAspect.php index 2f240073..bb888619 100644 --- a/System/Error/LoggerAspect.php +++ b/System/Error/LoggerAspect.php @@ -25,8 +25,6 @@ class LoggerAspect implements IAspect */ #[Pure] public function __construct(public array $handler) { - $this->className = get_class($this->handler[0]); - $this->methodName = $this->handler[1]; } @@ -47,8 +45,11 @@ class LoggerAspect implements IAspect private function print_runtime($startTime) { + $className = get_class($this->handler[0]); + $methodName = $this->handler[1]; + $runTime = round(microtime(true) - $startTime, 6); - echo sprintf('run %s::%s use time %6f', $this->className, $this->methodName, $runTime); + echo sprintf('run %s::%s use time %6f', $className, $methodName, $runTime); echo PHP_EOL; } diff --git a/System/Jwt/Jwt.php b/System/Jwt/Jwt.php index 44057e86..435940ea 100644 --- a/System/Jwt/Jwt.php +++ b/System/Jwt/Jwt.php @@ -3,19 +3,14 @@ declare(strict_types=1); namespace Snowflake\Jwt; -use Annotation\Aspect; -use Database\InjectProperty; use Exception; use HttpServer\Http\HttpHeaders; -use ReflectionException; use Snowflake\Cache\Redis; use Snowflake\Abstracts\Config; use Snowflake\Core\Str; use Snowflake\Exception\AuthException; use Snowflake\Abstracts\Component; -use Snowflake\Exception\ComponentException; use Snowflake\Exception\ConfigException; -use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index e93c7a9e..589fed05 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -23,10 +23,6 @@ class Connection extends Pool public int $timeout = 1900; - /** @var PDO[] */ - protected array $connections = []; - - /** * @param $timeout */ diff --git a/System/Process/Process.php b/System/Process/Process.php index 13aa5a89..8f53a4d0 100644 --- a/System/Process/Process.php +++ b/System/Process/Process.php @@ -20,7 +20,7 @@ use Swoole\Coroutine\System; abstract class Process extends \Swoole\Process implements SProcess { - + /** * Process constructor. * @param $application diff --git a/Validator/Validator.php b/Validator/Validator.php index 70505a6c..03a89f5f 100644 --- a/Validator/Validator.php +++ b/Validator/Validator.php @@ -17,7 +17,7 @@ class Validator extends BaseValidator { /** @var BaseValidator[] */ - private array $validators = []; + private ?array $validators = []; /** @var ?Validator */ private static ?Validator $instance = null; @@ -197,6 +197,7 @@ class Validator extends BaseValidator } break; } + unset($this->validators); $this->validators = []; return !isset($isTrue); }