diff --git a/Annotation/Route/Route.php b/Annotation/Route/Route.php index f9fe4cba..f6e2cc1d 100644 --- a/Annotation/Route/Route.php +++ b/Annotation/Route/Route.php @@ -37,13 +37,6 @@ use Snowflake\Snowflake; { // TODO: Implement setHandler() method. $router = Snowflake::app()->getRouter(); - $attribute = Snowflake::getDi()->getMethodAttribute($class::class, $method); - foreach ($attribute as $item) { - if ($item instanceof Route) { - continue; - } - $item->execute($class, $method); - } $router->addRoute($this->uri, [$class, $method], $this->method); return $router; } diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index ff561da0..162db808 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -9,10 +9,11 @@ use Annotation\Aspect; use Annotation\Route\RpcProducer; use Closure; use Exception; -use HttpServer\Abstracts\HttpService; use HttpServer\Http\Request; use JetBrains\PhpStorm\Pure; use ReflectionException; +use Server\Events\OnAfterWorkerStart; +use Snowflake\Events\EventProvider; use Snowflake\Exception\NotFindClassException; use Snowflake\IAspect; use Snowflake\Snowflake; @@ -21,7 +22,7 @@ use Snowflake\Snowflake; * Class Node * @package Snowflake\Snowflake\Route */ -class Node extends HttpService +class Node { public string $path = ''; @@ -37,6 +38,8 @@ class Node extends HttpService private string $_dataType = ''; + private array|Closure|null $_handler = null; + public string $htmlSuffix = '.html'; public bool $enableHtmlSuffix = false; public array $namespace = []; @@ -59,6 +62,17 @@ class Node extends HttpService } + /** + * @throws NotFindClassException + * @throws ReflectionException + */ + public function __construct() + { + $eventDispatcher = di(EventProvider::class); + $eventDispatcher->on(OnAfterWorkerStart::class, [$this, 'setParameters']); + } + + /** * @param string $data * @return mixed @@ -90,7 +104,8 @@ class Node extends HttpService } else if ($handler != null && !is_callable($handler, true)) { $this->_error = 'Controller is con\'t exec.'; } - return $this->setParameters($handler); + $this->_handler = $handler; + return $this; } @@ -176,19 +191,22 @@ class Node extends HttpService * @throws ReflectionException * @throws NotFindClassException */ - public function setParameters($handler): static + public function setParameters(): static { $container = Snowflake::getDi(); - if ($handler instanceof Closure) { - $this->_injectParameters = $container->resolveFunctionParameters($handler); + + $dispatcher = $this->_handler; + unset($this->_handler); + if ($dispatcher instanceof Closure) { + $this->_injectParameters = $container->resolveFunctionParameters($dispatcher); } else { - [$controller, $action] = $handler; + [$controller, $action] = $dispatcher; if (is_object($controller)) { $controller = get_class($controller); } $this->_injectParameters = $container->getMethodParameters($controller, $action); } - return $this->injectMiddleware($handler); + return $this->injectMiddleware($dispatcher); } diff --git a/Server/Events/OnAfterWorkerStart.php b/Server/Events/OnAfterWorkerStart.php new file mode 100644 index 00000000..9c5dddbe --- /dev/null +++ b/Server/Events/OnAfterWorkerStart.php @@ -0,0 +1,8 @@ +workerInitExecutor($server, $annotation, $workerId); $this->interpretDirectory($annotation); + + $this->eventDispatch->dispatch(new OnAfterWorkerStart()); }