This commit is contained in:
2021-08-06 15:18:00 +08:00
parent 776a251123
commit fc253b52bf
4 changed files with 37 additions and 15 deletions
-7
View File
@@ -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;
}
+26 -8
View File
@@ -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);
}
+8
View File
@@ -0,0 +1,8 @@
<?php
namespace Server\Events;
class OnAfterWorkerStart
{
}
+3
View File
@@ -7,6 +7,7 @@ use Annotation\Inject;
use Exception;
use ReflectionException;
use Server\Constant;
use Server\Events\OnAfterWorkerStart;
use Server\Events\OnWorkerError;
use Server\Events\OnWorkerExit;
use Server\Events\OnWorkerStart;
@@ -56,6 +57,8 @@ class OnServerWorker extends \Server\Abstracts\Server
$this->workerInitExecutor($server, $annotation, $workerId);
$this->interpretDirectory($annotation);
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
}