From 7c6b5fb71fc66737ad7b62f1d00cf93ff90ff0c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 5 Mar 2021 16:10:36 +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 --- HttpServer/Route/Middleware.php | 32 ------------------------ HttpServer/Route/Node.php | 43 ++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/HttpServer/Route/Middleware.php b/HttpServer/Route/Middleware.php index 1104b7fc..de72ffea 100644 --- a/HttpServer/Route/Middleware.php +++ b/HttpServer/Route/Middleware.php @@ -54,44 +54,12 @@ class Middleware */ public function getGenerate(Node $node): mixed { - $this->set_attributes($node); return $node->callback = Reduce::reduce(function () use ($node) { return Dispatch::create($node->handler, func_get_args())->dispatch(); }, $this->annotation($node)); } - /** - * @param $node - * @throws Exception - */ - private function set_attributes(Node $node) - { - if (!is_array($node->handler)) { - return; - } - [$controller, $action] = $node->handler; - $attributes = Snowflake::app()->getAttributes(); - $annotation = $attributes->getMethods(get_class($controller), $action); - if (empty($annotation)) { - return; - } - foreach ($annotation as $name => $attribute) { - if ($attribute instanceof Interceptor) { - $node->addInterceptor($attribute->interceptor); - } - if ($attribute instanceof After) { - $node->addAfter($attribute->after); - } - if ($attribute instanceof RMiddleware) { - $node->addMiddleware($attribute->middleware); - } - if ($attribute instanceof Limits) { - $node->addLimits($attribute->limits); - } - } - } - /** * @param Node $node diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index 00f296a1..4fe3914f 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace HttpServer\Route; +use Annotation\Route\Middleware as RMiddleware; use Closure; use HttpServer\Abstracts\HttpService; use HttpServer\Http\Request; @@ -79,7 +80,7 @@ class Node extends HttpService } else { $this->handler = $handler; } - return $this; + return $this->restructure(); } @@ -212,6 +213,9 @@ class Node extends HttpService if (!empty($action) && !$reflect->hasMethod($action)) { throw new Exception('method ' . $action . ' not exists at ' . $controller . '.'); } + + $this->annotationInject($reflect->getName(), $action); + return [$reflect->newInstance(), $action]; } catch (Throwable $exception) { $this->_error = $exception->getMessage(); @@ -221,6 +225,40 @@ 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 + { + $attributes = Snowflake::app()->getAttributes(); + $annotation = $attributes->getMethods($className, $action); + if (empty($annotation)) { + return $this; + } + foreach ($annotation as $name => $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 @@ -471,9 +509,6 @@ class Node extends HttpService */ public function dispatch(): mixed { - if (!empty($this->callback)) { - return $this->runWith(...func_get_args()); - } if (empty($this->restructure()->callback)) { return Json::to(404, $this->errorMsg()); }