diff --git a/core/Application.php b/core/Application.php index b07bed4b..9b0ad09b 100644 --- a/core/Application.php +++ b/core/Application.php @@ -28,10 +28,10 @@ use Server\ServerProviders; use stdClass; use Swoole\Process; use Swoole\Timer; +use Symfony\Component\Console\Application as ConsoleApplication; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\ConsoleOutput; -use Symfony\Component\Console\Application as ConsoleApplication; /** * Class Init @@ -68,7 +68,7 @@ class Application extends BaseApplication /** - * @throws NotFindClassException + * @throws */ public function withDatabase() { @@ -77,7 +77,7 @@ class Application extends BaseApplication /** - * @throws NotFindClassException + * @throws */ public function withCrontab() { @@ -118,9 +118,7 @@ class Application extends BaseApplication /** - * @throws NotFindClassException - * @throws ReflectionException - * @throws Exception + * @throws */ public function withFileChangeListen() { @@ -163,7 +161,7 @@ class Application extends BaseApplication public function import(string $service): static { if (!class_exists($service)) { - throw new NotFindClassException($service); + return $this; } $class = Kiri::getDi()->get($service); if (method_exists($class, 'onImport')) { @@ -192,7 +190,7 @@ class Application extends BaseApplication */ public function register(string $command) { - di(ConsoleApplication::class)->add(di($command)); + di(ConsoleApplication::class)->add(di($command)); } @@ -239,7 +237,7 @@ class Application extends BaseApplication */ private function enableFileChange(Command $class, $input, $output): void { - fire(new OnBeforeCommandExecute()); + fire(new OnBeforeCommandExecute()); if (!($class instanceof ServerCommand)) { scan_directory(directory('app'), 'App'); } diff --git a/http-helper/Route/Router.php b/http-helper/Route/Router.php index 51138cc4..ec9d0ff2 100644 --- a/http-helper/Route/Router.php +++ b/http-helper/Route/Router.php @@ -29,526 +29,535 @@ 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'; - - const NOT_FOUND = 'Page not found or method not allowed.'; - - /** @var string[] */ - public array $methods = ['GET', 'POST', 'OPTIONS', 'PUT', 'DELETE', 'RECEIVE', 'HEAD']; - - - public ?Closure $middleware = null; - - public int $useTree = ROUTER_TREE; - - public ?Response $response = null; - - - /** - * @var RequestInterface - */ - #[Inject(RequestInterface::class)] - public RequestInterface $request; - - - /** - * @param Closure $middleware - */ - public function setMiddleware(Closure $middleware): void - { - $this->middleware = $middleware; - } - - - /** - * @throws ConfigException - * @throws Exception - * 初始化函数路径 - */ - public function init() - { - $this->dir = Config::get('http.namespace', $this->dir); - - $this->response = Kiri::app()->get('response'); - } - - - /** - * @param bool $useTree - */ - public function setUseTree(bool $useTree): void - { - $this->useTree = $useTree ? ROUTER_TREE : ROUTER_HASH; - } - - - /** - * @param $path - * @param $handler - * @param string $method - * @return ?Node - * @throws Exception - */ - public function addRoute($path, $handler, string $method = 'any'): ?Node - { - if ($handler instanceof Closure) { - $handler = Closure::bind($handler, di(Controller::class)); - } - return $this->tree($path, $handler, $method); - } - - /** - * @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 - * @throws Exception - */ - private function tree($path, $handler, string $method = 'any'): Node - { - $explode = $this->split($path); - $start = array_shift($explode); - $parent = $this->nodes[$start] ?? null; - if (is_null($parent)) { - $parent = $this->nodes[$start] = $this->NodeInstance($start, 0, $method); - } - if (!empty($explode)) { - $parent = $this->bindNode($parent, $explode, $method); - } - if (!in_array($method, $parent->method)) { - $parent->method[] = $method; - } - return $parent->setHandler($handler, $method, $path); - } - - - /** - * @param Node $parent - * @param array $explode - * @param $method - * @return Node - * @throws Exception - */ - private function bindNode(Node $parent, array $explode, $method): Node - { - $a = 0; - foreach ($explode as $value) { - ++$a; - $search = $parent->findNode($value); - if ($search === null) { - $parent = $parent->addChild($this->NodeInstance($value, $a, $method)); - } else { - $parent = $search; - } - } - return $parent; - } - - /** - * @param $route - * @param $handler - * @return void - * @throws - */ - public function socket($route, $handler): void - { - $this->addRoute($route, $handler, 'SOCKET'); - } - - - /** - * @param $route - * @param $handler - * @return void - * @throws - */ - public function post($route, $handler): void - { - $this->addRoute($route, $handler, 'POST'); - } - - /** - * @param $route - * @param $handler - * @return void - * @throws - */ - public function get($route, $handler): void - { - $this->addRoute($route, $handler, 'GET'); - } - - - /** - * @param $route - * @param $handler - * @return void - * @throws - */ - public function options($route, $handler): void - { - $this->addRoute($route, $handler, 'OPTIONS'); - } - - - /** - * @param $route - * @param $handler - * @throws - */ - public function any($route, $handler): void - { - foreach ($this->methods as $method) { - $this->addRoute($route, $handler, $method); - } - } - - /** - * @param $route - * @param $handler - * @return void - * @throws - */ - public function delete($route, $handler): void - { - $this->addRoute($route, $handler, 'DELETE'); - } - - - /** - * @param $route - * @param $handler - * @return void - * @throws Exception - */ - public function head($route, $handler): void - { - $this->addRoute($route, $handler, 'HEAD'); - } - - - /** - * @param $route - * @param $handler - * @return void - * @throws - */ - public function put($route, $handler): void - { - $this->addRoute($route, $handler, 'PUT'); - } - - /** - * @param $value - * @param int $index - * @param string $method - * @return Node - * @throws - */ - public function NodeInstance($value, int $index = 0, string $method = 'GET'): Node - { - $node = new Node($this); - $node->childes = []; - $node->path = $value; - $node->index = $index; - $node->method[] = $method; - $node->namespace = $this->loadNamespace(); - - $name = array_column($this->groupTacks, 'middleware'); - if (is_array($name)) { - $node->addMiddleware($method, $this->resolve_middleware($name)); - } - return $node; - } - - - /** - * @return Closure - */ - public function getMiddleware(): ?Closure - { - return $this->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_filter($array); - } - - - /** - * @param $value - * @return Closure|array|null - * @throws NotFindClassException - * @throws ReflectionException - */ - private function getMiddlewareInstance($value): null|Closure|array - { - if (!is_string($value)) { - return $value; - } - $value = Kiri::createObject($value); - if (!($value instanceof MiddlewareInterface)) { - return null; - } - return [$value, 'onHandler']; - } - - - /** - * @return array - */ - private function loadNamespace(): array - { - $name = array_column($this->groupTacks, 'namespace'); - array_unshift($name, $this->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); - } - - /** - * @return string - */ - public function addPrefix(): string - { - $prefix = array_column($this->groupTacks, 'prefix'); - - $prefix = array_filter($prefix); - - if (empty($prefix)) { - return ''; - } - - return '/' . implode('/', $prefix); - } - - /** - * @param array|null $explode - * @return Node|null - * 查找指定路由 - * @throws Exception - */ - public function tree_search(?array $explode): ?Node - { - if (empty($this->nodes)) { - return null; - } - $parent = $this->nodes[array_shift($explode)] ?? null; - if (!($parent instanceof Node)) { - return null; - } - while ($value = array_shift($explode)) { - $node = $parent->findNode($value); - if (!$node) { - return null; - } - $parent = $node; - } - return $parent; - } - - /** - * @param $path - * @return array|null - */ - public function split($path): ?array - { - $path = $this->addPrefix() . '/' . ltrim($path, '/'); - if ($path === '/') { - return ['/']; - } - $filter = array_filter(explode('/', $path)); - if (!empty($filter)) { - return $filter; - } - return ['/']; - } - - /** - * @return array - */ - public function each(): array - { - $paths = []; - foreach ($this->nodes as $_node) { - /** @var Node[] $node */ - $paths[] = ['method' => $_node->method, 'path' => $_node->sourcePath, 'alias' => $_node->getAlias()]; - $paths = $this->getChildes($_node, $paths); - } - return $paths; - } - - - /** - * @param Node $node - * @param array $path - * @return array - */ - private function getChildes(Node $node, array $path): array - { - foreach ($node->childes as $item) { - $path[] = ['method' => $item->method, 'path' => $item->sourcePath, 'alias' => $item->getAlias()]; - if (!empty($item->childes)) { - $path = $this->getChildes($item, $path); - } - } - return $path; - } - - - /** - * @param $exception - * @return mixed - * @throws Exception - */ - public function exception($exception): mixed - { - return Kiri::app()->getLogger()->exception($exception); - } - - - /** - * @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 RequestInterface $request - * @return Node|null - * 树杈搜索 - * @throws Exception - */ - public function Branch_search(RequestInterface $request): ?Node - { - $uri = $request->getUri(); - if ($request->isMethod('OPTIONS')) { - $node = $this->tree_search(['*']); - if (!($node instanceof Node)) { - $node = $this->tree_search($uri->getExplode()); - } - } else { - $node = $this->tree_search($uri->getExplode()); - } - if (!($node instanceof Node)) { - return null; - } - return $node; - } - - - /** - * @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 $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); - } - } - } + /** @var Node[] $nodes */ + public array $nodes = []; + public array $groupTacks = []; + public ?string $dir = 'App\\Http\\Controllers'; + + const NOT_FOUND = 'Page not found or method not allowed.'; + + /** @var string[] */ + public array $methods = ['GET', 'POST', 'OPTIONS', 'PUT', 'DELETE', 'RECEIVE', 'HEAD']; + + + public ?Closure $middleware = null; + + public int $useTree = ROUTER_TREE; + + public ?Response $response = null; + + + /** + * @var RequestInterface + */ + #[Inject(RequestInterface::class)] + public RequestInterface $request; + + + /** + * @param Closure $middleware + */ + public function setMiddleware(Closure $middleware): void + { + $this->middleware = $middleware; + } + + + /** + * @param string $namespace + */ + public function setNamespace(string $namespace): void + { + $this->dir = $namespace; + } + + + /** + * @throws ConfigException + * @throws Exception + * 初始化函数路径 + */ + public function init() + { + $this->dir = Config::get('http.namespace', $this->dir); + + $this->response = Kiri::app()->get('response'); + } + + + /** + * @param bool $useTree + */ + public function setUseTree(bool $useTree): void + { + $this->useTree = $useTree ? ROUTER_TREE : ROUTER_HASH; + } + + + /** + * @param $path + * @param $handler + * @param string $method + * @return ?Node + * @throws Exception + */ + public function addRoute($path, $handler, string $method = 'any'): ?Node + { + if ($handler instanceof Closure) { + $handler = Closure::bind($handler, di(Controller::class)); + } + return $this->tree($path, $handler, $method); + } + + /** + * @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 + * @throws Exception + */ + private function tree($path, $handler, string $method = 'any'): Node + { + $explode = $this->split($path); + $start = array_shift($explode); + $parent = $this->nodes[$start] ?? null; + if (is_null($parent)) { + $parent = $this->nodes[$start] = $this->NodeInstance($start, 0, $method); + } + if (!empty($explode)) { + $parent = $this->bindNode($parent, $explode, $method); + } + if (!in_array($method, $parent->method)) { + $parent->method[] = $method; + } + return $parent->setHandler($handler, $method, $path); + } + + + /** + * @param Node $parent + * @param array $explode + * @param $method + * @return Node + * @throws Exception + */ + private function bindNode(Node $parent, array $explode, $method): Node + { + $a = 0; + foreach ($explode as $value) { + ++$a; + $search = $parent->findNode($value); + if ($search === null) { + $parent = $parent->addChild($this->NodeInstance($value, $a, $method)); + } else { + $parent = $search; + } + } + return $parent; + } + + /** + * @param $route + * @param $handler + * @return void + * @throws + */ + public function socket($route, $handler): void + { + $this->addRoute($route, $handler, 'SOCKET'); + } + + + /** + * @param $route + * @param $handler + * @return void + * @throws + */ + public function post($route, $handler): void + { + $this->addRoute($route, $handler, 'POST'); + } + + /** + * @param $route + * @param $handler + * @return void + * @throws + */ + public function get($route, $handler): void + { + $this->addRoute($route, $handler, 'GET'); + } + + + /** + * @param $route + * @param $handler + * @return void + * @throws + */ + public function options($route, $handler): void + { + $this->addRoute($route, $handler, 'OPTIONS'); + } + + + /** + * @param $route + * @param $handler + * @throws + */ + public function any($route, $handler): void + { + foreach ($this->methods as $method) { + $this->addRoute($route, $handler, $method); + } + } + + /** + * @param $route + * @param $handler + * @return void + * @throws + */ + public function delete($route, $handler): void + { + $this->addRoute($route, $handler, 'DELETE'); + } + + + /** + * @param $route + * @param $handler + * @return void + * @throws Exception + */ + public function head($route, $handler): void + { + $this->addRoute($route, $handler, 'HEAD'); + } + + + /** + * @param $route + * @param $handler + * @return void + * @throws + */ + public function put($route, $handler): void + { + $this->addRoute($route, $handler, 'PUT'); + } + + /** + * @param $value + * @param int $index + * @param string $method + * @return Node + * @throws + */ + public function NodeInstance($value, int $index = 0, string $method = 'GET'): Node + { + $node = new Node($this); + $node->childes = []; + $node->path = $value; + $node->index = $index; + $node->method[] = $method; + $node->namespace = $this->loadNamespace(); + + $name = array_column($this->groupTacks, 'middleware'); + if (is_array($name)) { + $node->addMiddleware($method, $this->resolve_middleware($name)); + } + return $node; + } + + + /** + * @return Closure + */ + public function getMiddleware(): ?Closure + { + return $this->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_filter($array); + } + + + /** + * @param $value + * @return Closure|array|null + * @throws NotFindClassException + * @throws ReflectionException + */ + private function getMiddlewareInstance($value): null|Closure|array + { + if (!is_string($value)) { + return $value; + } + $value = Kiri::createObject($value); + if (!($value instanceof MiddlewareInterface)) { + return null; + } + return [$value, 'onHandler']; + } + + + /** + * @return array + */ + private function loadNamespace(): array + { + $name = array_column($this->groupTacks, 'namespace'); + array_unshift($name, $this->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); + } + + /** + * @return string + */ + public function addPrefix(): string + { + $prefix = array_column($this->groupTacks, 'prefix'); + + $prefix = array_filter($prefix); + + if (empty($prefix)) { + return ''; + } + + return '/' . implode('/', $prefix); + } + + /** + * @param array|null $explode + * @return Node|null + * 查找指定路由 + * @throws Exception + */ + public function tree_search(?array $explode): ?Node + { + if (empty($this->nodes)) { + return null; + } + $parent = $this->nodes[array_shift($explode)] ?? null; + if (!($parent instanceof Node)) { + return null; + } + while ($value = array_shift($explode)) { + $node = $parent->findNode($value); + if (!$node) { + return null; + } + $parent = $node; + } + return $parent; + } + + /** + * @param $path + * @return array|null + */ + public function split($path): ?array + { + $path = $this->addPrefix() . '/' . ltrim($path, '/'); + if ($path === '/') { + return ['/']; + } + $filter = array_filter(explode('/', $path)); + if (!empty($filter)) { + return $filter; + } + return ['/']; + } + + /** + * @return array + */ + public function each(): array + { + $paths = []; + foreach ($this->nodes as $_node) { + /** @var Node[] $node */ + $paths[] = ['method' => $_node->method, 'path' => $_node->sourcePath, 'alias' => $_node->getAlias()]; + $paths = $this->getChildes($_node, $paths); + } + return $paths; + } + + + /** + * @param Node $node + * @param array $path + * @return array + */ + private function getChildes(Node $node, array $path): array + { + foreach ($node->childes as $item) { + $path[] = ['method' => $item->method, 'path' => $item->sourcePath, 'alias' => $item->getAlias()]; + if (!empty($item->childes)) { + $path = $this->getChildes($item, $path); + } + } + return $path; + } + + + /** + * @param $exception + * @return mixed + * @throws Exception + */ + public function exception($exception): mixed + { + return Kiri::app()->getLogger()->exception($exception); + } + + + /** + * @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 RequestInterface $request + * @return Node|null + * 树杈搜索 + * @throws Exception + */ + public function Branch_search(RequestInterface $request): ?Node + { + $uri = $request->getUri(); + if ($request->isMethod('OPTIONS')) { + $node = $this->tree_search(['*']); + if (!($node instanceof Node)) { + $node = $this->tree_search($uri->getExplode()); + } + } else { + $node = $this->tree_search($uri->getExplode()); + } + if (!($node instanceof Node)) { + return null; + } + return $node; + } + + + /** + * @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 $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/http-server/Worker/OnWorkerStart.php b/http-server/Worker/OnWorkerStart.php index 16dae0c7..352e9afe 100644 --- a/http-server/Worker/OnWorkerStart.php +++ b/http-server/Worker/OnWorkerStart.php @@ -10,7 +10,6 @@ use Kiri\Abstracts\Config; use Kiri\Di\NoteManager; use Kiri\Exception\ConfigException; use Kiri\Kiri; -use Kiri\Runtime; use Psr\EventDispatcher\EventDispatcherInterface; use ReflectionException; use Server\ServerManager; @@ -22,15 +21,15 @@ class OnWorkerStart implements EventDispatcherInterface /** * @var Annotation */ - #[Inject(Annotation::class)] - public Annotation $annotation; + #[Inject(Annotation::class)] + public Annotation $annotation; /** * @var Router */ - #[Inject(Router::class)] - public Router $router; + #[Inject(Router::class)] + public Router $router; /** @@ -40,68 +39,67 @@ class OnWorkerStart implements EventDispatcherInterface * @throws ReflectionException * @throws Exception */ - public function dispatch(object $event) - { - $isWorker = $event->workerId < $event->server->setting['worker_num']; + public function dispatch(object $event) + { + $isWorker = $event->workerId < $event->server->setting['worker_num']; - $this->annotation->read(APP_PATH . 'app', 'App'); - $this->interpretDirectory(); - if ($isWorker) { - ServerManager::setEnv('environmental', Kiri::WORKER); - Kiri::getFactory()->getRouter()->_loader(); + $this->interpretDirectory(); + if ($isWorker) { + ServerManager::setEnv('environmental', Kiri::WORKER); + Kiri::getFactory()->getRouter()->_loader(); - echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL; + echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL; - $this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId)); - } else { - ServerManager::setEnv('environmental', Kiri::TASK); + $this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId)); + } else { + ServerManager::setEnv('environmental', Kiri::TASK); - echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL; + echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL; - $this->setProcessName(sprintf('Tasker[%d].%d', $event->server->worker_pid, $event->workerId)); - } - } + $this->setProcessName(sprintf('Tasker[%d].%d', $event->server->worker_pid, $event->workerId)); + } + } - /** - * @param $prefix - * @throws ConfigException - */ - protected function setProcessName($prefix) - { - if (Kiri::getPlatform()->isMac()) { - return; - } - $name = Config::get('id', 'system-service'); - if (!empty($prefix)) { - $name .= '.' . $prefix; - } - swoole_set_process_name($name); - } + /** + * @param $prefix + * @throws ConfigException + */ + protected function setProcessName($prefix) + { + if (Kiri::getPlatform()->isMac()) { + return; + } + $name = Config::get('id', 'system-service'); + if (!empty($prefix)) { + $name .= '.' . $prefix; + } + swoole_set_process_name($name); + } - /** - * @throws ReflectionException - * @throws Exception - */ - private function interpretDirectory() - { - $fileLists = $this->annotation->runtime(APP_PATH . 'app'); - $di = Kiri::getDi(); - foreach ($fileLists as $class) { - foreach (NoteManager::getTargetNote($class) as $value) { - $value->execute($class); - } - $methods = $di->getMethodAttribute($class); - foreach ($methods as $method => $attribute) { - if (empty($attribute)) { - continue; - } - foreach ($attribute as $item) { - $item->execute($class, $method); - } - } - } - } + /** + * @throws ReflectionException + * @throws Exception + */ + private function interpretDirectory() + { + $di = Kiri::getDi(); + $fileLists = $this->annotation->read(APP_PATH . 'app'); + foreach ($fileLists->runtime(APP_PATH . 'app') as $class) { + foreach (NoteManager::getTargetNote($class) as $value) { + $value->execute($class); + } + $methods = $di->getMethodAttribute($class); + foreach ($methods as $method => $attribute) { + if (empty($attribute)) { + continue; + } + foreach ($attribute as $item) { + $item->execute($class, $method); + } + } + } + } } diff --git a/note/Annotation.php b/note/Annotation.php index cf6924c0..09651136 100644 --- a/note/Annotation.php +++ b/note/Annotation.php @@ -61,12 +61,13 @@ class Annotation extends Component * @param string $path * @param string $namespace * @param array $exclude - * @return void + * @return static * @throws Exception */ - public function read(string $path, string $namespace = 'App', array $exclude = []): void + public function read(string $path, string $namespace = 'App', array $exclude = []): static { $this->_loader->_scanDir(new DirectoryIterator($path), $namespace, $exclude); + return $this; }