From bfebcce204b79ea351f350b452b36adbac06cca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 5 Mar 2021 17:11:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Annotation/Attribute.php | 9 +++++ Annotation/Route/Route.php | 3 +- Annotation/Route/Socket.php | 4 ++- HttpServer/Route/Node.php | 70 ++++++++++++++++++------------------- 4 files changed, 49 insertions(+), 37 deletions(-) diff --git a/Annotation/Attribute.php b/Annotation/Attribute.php index 6bc79df0..27cc7580 100644 --- a/Annotation/Attribute.php +++ b/Annotation/Attribute.php @@ -4,6 +4,14 @@ namespace Annotation; +use Annotation\Route\After; +use Annotation\Route\Interceptor; +use Annotation\Route\Limits; +use Annotation\Route\Middleware as RMiddleware; +use HttpServer\Route\Node; +use ReflectionException; +use Snowflake\Exception\ComponentException; +use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; /** @@ -23,4 +31,5 @@ abstract class Attribute implements IAnnotation return $this; } + } diff --git a/Annotation/Route/Route.php b/Annotation/Route/Route.php index 8491cd3a..62b88c24 100644 --- a/Annotation/Route/Route.php +++ b/Annotation/Route/Route.php @@ -43,7 +43,8 @@ use Snowflake\Snowflake; // TODO: Implement setHandler() method. $router = Snowflake::app()->getRouter(); - $router->addRoute($this->uri, $handler, $this->method); + $node = $router->addRoute($this->uri, $handler, $this->method); + $node::annotationInject($node, get_class($handler[0]), $handler[1]); return $router; } diff --git a/Annotation/Route/Socket.php b/Annotation/Route/Socket.php index a728b08e..14cfd5ad 100644 --- a/Annotation/Route/Socket.php +++ b/Annotation/Route/Socket.php @@ -53,7 +53,9 @@ use Snowflake\Snowflake; $method = $this->event . '::' . (is_null($this->uri) ? 'event' : $this->uri); - $router->addRoute($method, $handler, 'sw::socket'); + $node = $router->addRoute($method, $handler, 'sw::socket'); + + $node::annotationInject($node, get_class($handler[0]), $handler[1]); return $router; } diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index dd7eb3e4..80605b93 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -276,7 +276,7 @@ class Node extends HttpService throw new Exception('method ' . $action . ' not exists at ' . $controller . '.'); } - $this->annotationInject($reflect->getName(), $action); + static::annotationInject($this, $reflect->getName(), $action); return [$reflect->newInstance(), $action]; } catch (Throwable $exception) { @@ -287,40 +287,6 @@ class Node extends HttpService } - /** - * @param string $className - * @param string $action - * @return $this - * @throws ComponentException - * @throws NotFindClassException - * @throws ReflectionException - * @throws Exception - */ - private function annotationInject(string $className, string $action): static - { - $annotation = annotation()->getMethods($className, $action); - if (empty($annotation)) { - return $this; - } - foreach ($annotation as $name => $attribute) { - var_dump($this->path . ':' . get_class($attribute)); - if ($attribute instanceof \Annotation\Route\Interceptor) { - $this->addInterceptor($attribute->interceptor); - } - if ($attribute instanceof \Annotation\Route\After) { - $this->addAfter($attribute->after); - } - if ($attribute instanceof RMiddleware) { - $this->addMiddleware($attribute->middleware); - } - if ($attribute instanceof \Annotation\Route\Limits) { - $this->addLimits($attribute->limits); - } - } - return $this; - } - - /** * @param Closure|array|string $handler * @throws Exception @@ -339,6 +305,40 @@ class Node extends HttpService } + /** + * @param Node $node + * @param string $className + * @param string $action + * @return Node + * @throws ComponentException + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + public static function annotationInject(Node $node, string $className, string $action): Node + { + $annotation = annotation()->getMethods($className, $action); + if (empty($annotation)) { + return $node; + } + foreach ($annotation as $name => $attribute) { + if ($attribute instanceof \Annotation\Route\Interceptor) { + $node->addInterceptor($attribute->interceptor); + } + if ($attribute instanceof \Annotation\Route\After) { + $node->addAfter($attribute->after); + } + if ($attribute instanceof RMiddleware) { + $node->addMiddleware($attribute->middleware); + } + if ($attribute instanceof \Annotation\Route\Limits) { + $node->addLimits($attribute->limits); + } + } + return $node; + } + + /** * @param Closure|array|string $handler * @throws Exception