This commit is contained in:
2021-03-05 17:41:07 +08:00
parent c537a0a53a
commit fad7c43f59
3 changed files with 18 additions and 22 deletions
+2 -2
View File
@@ -43,8 +43,8 @@ use Snowflake\Snowflake;
// TODO: Implement setHandler() method. // TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter(); $router = Snowflake::app()->getRouter();
$node = $router->addRoute($this->uri, $handler, $this->method); $router->addRoute($this->uri, $handler, $this->method);
$node::annotationInject($node, get_class($handler[0]), $handler[1]);
return $router; return $router;
} }
+1 -3
View File
@@ -53,9 +53,7 @@ use Snowflake\Snowflake;
$method = $this->event . '::' . (is_null($this->uri) ? 'event' : $this->uri); $method = $this->event . '::' . (is_null($this->uri) ? 'event' : $this->uri);
$node = $router->addRoute($method, $handler, 'sw::socket'); $router->addRoute($method, $handler, 'sw::socket');
$node::annotationInject($node, get_class($handler[0]), $handler[1]);
return $router; return $router;
} }
+15 -17
View File
@@ -79,10 +79,12 @@ class Node extends HttpService
} else if ($handler != null && !is_callable($handler, true)) { } else if ($handler != null && !is_callable($handler, true)) {
$this->_error = 'Controller is con\'t exec.'; $this->_error = 'Controller is con\'t exec.';
} else { } else {
$this->handler = $handler; [$controller, $action] = $this->handler = $handler;
$this->annotationInject(get_class($controller), $action);
} }
if (!empty($this->handler)) { if (!empty($this->handler)) {
$this->callback = Reduce::reduce($this->createDispatch(), $this->annotation($this)); $this->callback = Reduce::reduce($this->createDispatch(), $this->annotation());
} }
return $this; return $this;
} }
@@ -100,14 +102,13 @@ class Node extends HttpService
/** /**
* @param Node $node
* @return array * @return array
*/ */
protected function annotation(Node $node): array protected function annotation(): array
{ {
$middleWares = $node->getMiddleWares(); $middleWares = $this->getMiddleWares();
$middleWares = $this->annotation_limit($node, $middleWares); $middleWares = $this->annotation_limit($this, $middleWares);
$middleWares = $this->annotation_interceptor($node, $middleWares); $middleWares = $this->annotation_interceptor($this, $middleWares);
return $middleWares; return $middleWares;
} }
@@ -315,30 +316,27 @@ class Node extends HttpService
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public static function annotationInject(Node $node, string $className, string $action): Node public function annotationInject(string $className, string $action): Node
{ {
$annotation = annotation()->getMethods($className, $action); $annotation = annotation()->getMethods($className, $action);
if (empty($annotation)) { if (empty($annotation)) {
return $node; return $this;
} }
foreach ($annotation as $name => $attribute) { foreach ($annotation as $name => $attribute) {
if ($attribute instanceof \Annotation\Route\Interceptor) { if ($attribute instanceof \Annotation\Route\Interceptor) {
$node->addInterceptor($attribute->interceptor); $this->addInterceptor($attribute->interceptor);
} }
if ($attribute instanceof \Annotation\Route\After) { if ($attribute instanceof \Annotation\Route\After) {
$node->addAfter($attribute->after); $this->addAfter($attribute->after);
} }
if ($attribute instanceof Middleware) { if ($attribute instanceof Middleware) {
$node->addMiddleware($attribute->middleware); $this->addMiddleware($attribute->middleware);
} }
if ($attribute instanceof \Annotation\Route\Limits) { if ($attribute instanceof \Annotation\Route\Limits) {
$node->addLimits($attribute->limits); $this->addLimits($attribute->limits);
} }
} }
if (!empty($node->handler)) { return $this;
$node->callback = Reduce::reduce($node->createDispatch(), $node->annotation($node));
}
return $node;
} }