From 3914fa22d9b23046e7c8a6a2af7c0a4e8b4e723c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 4 Sep 2020 18:13:06 +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/Annotation/Annotation.php | 38 +++++++++++----------- HttpServer/Route/Node.php | 12 +++++-- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/HttpServer/Route/Annotation/Annotation.php b/HttpServer/Route/Annotation/Annotation.php index 40e125e3..e70c464d 100644 --- a/HttpServer/Route/Annotation/Annotation.php +++ b/HttpServer/Route/Annotation/Annotation.php @@ -76,11 +76,11 @@ class Annotation extends \Snowflake\Annotation\Annotation case 'Method': $this->bindMethod($node, $annotation); break; - case 'Middleware': - $this->bindMiddleware($node, $this->pop($this->getName(...$annotation))); - break; case'Interceptor': - $this->bindInterceptors($node, $this->pop($this->getName(...$annotation))); + $this->bindInterceptors($node, $annotation); + break; + case 'Middleware': + $this->bindMiddleware($node, $annotation); break; } } @@ -102,6 +102,7 @@ class Annotation extends \Snowflake\Annotation\Annotation } + /** * @param Node $node * @param $annotation @@ -112,16 +113,14 @@ class Annotation extends \Snowflake\Annotation\Annotation if (!isset($annotation[1][2])) { return; } - -// $explode = explode(',', $annotation[1][2]); -// foreach ($explode as $middleware) { -// $middleware = 'App\Http\Interceptor\\' . $middleware; -// if (!class_exists($middleware)) { -// continue; -// } -// } - $node->addMiddleware($annotation); - + $explode = explode(',', $annotation[1][2]); + foreach ($explode as $middleware) { + $middleware = 'App\Http\Middleware\\' . $middleware; + if (!class_exists($middleware)) { + continue; + } + $node->addMiddleware($middleware); + } } @@ -136,14 +135,15 @@ class Annotation extends \Snowflake\Annotation\Annotation return; } -// $explode = explode(',', $annotation[1][2]); -// foreach ($explode as $middleware) { -// } - $node->addInterceptor($annotation); + $explode = explode(',', $annotation[1][2]); + [$keyName, $matchs] = $annotation; + foreach ($explode as $middleware) { + $params = [$keyName, [$matchs[0], $matchs[1], $middleware]]; + $node->addInterceptor($this->pop($this->getName(...$params))); + } } - /** * @param $controller * @param $methodName diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index 8aa43592..948f0da9 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -310,9 +310,15 @@ class Node extends Application */ public function addMiddleware(string $class) { -// if (in_array($class, $this->middleware)) { -// return; -// } + if (is_string($class)) { + $class = Snowflake::createObject($class); + if (!($class instanceof \HttpServer\IInterface\Middleware)) { + return; + } + $class = [$class, 'handler']; + } else if (!is_callable($class, true)) { + return; + } $this->middleware[] = $class; $this->restructure(); }