From 76ed7406b450ecf3a5ba28b110d89eaefda74fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 8 Sep 2020 11:46:22 +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 | 37 +++++++++++++++++++++- HttpServer/Route/Middleware.php | 16 ++++++++-- HttpServer/Route/Node.php | 21 ++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/HttpServer/Route/Annotation/Annotation.php b/HttpServer/Route/Annotation/Annotation.php index 27a02fc1..79cd8bc9 100644 --- a/HttpServer/Route/Annotation/Annotation.php +++ b/HttpServer/Route/Annotation/Annotation.php @@ -39,7 +39,7 @@ class Annotation extends \Snowflake\Annotation\Annotation private $Middleware = ''; -// private $Route = ''; +// private $After = ''; protected $_annotations = []; @@ -87,6 +87,9 @@ class Annotation extends \Snowflake\Annotation\Annotation case 'Limits': $this->bindLimits($node, $annotation); break; +// case 'After': +// $this->bindAfter($node, $annotation); +// break; } } @@ -158,6 +161,38 @@ class Annotation extends \Snowflake\Annotation\Annotation $node->addInterceptor($this->pop($this->getName(...$params))); } } +// +// +// /** +// * @param Node $node +// * @param $annotation +// * @throws +// */ +// private function bindAfter($node, $annotation) +// { +// if (!isset($annotation[1][2])) { +// return; +// } +// +// $explode = explode(',', $annotation[1][2]); +// +// [$keyName, $matchs] = $annotation; +// foreach ($explode as $middleware) { +// $middleware = 'App\Http\Interceptor\\' . $middleware; +// if (!class_exists($middleware)) { +// continue; +// } +// $middleware = Snowflake::createObject($middleware); +// if (!($middleware instanceof Interceptor)) { +// continue; +// } +// $node->addAfter([$middleware, 'Interceptor']); +// continue; +// +// $params = [$keyName, [$matchs[0], $matchs[1], $middleware]]; +// $node->addInterceptor($this->pop($this->getName(...$params))); +// } +// } /** diff --git a/HttpServer/Route/Middleware.php b/HttpServer/Route/Middleware.php index d606c937..add1cfd1 100644 --- a/HttpServer/Route/Middleware.php +++ b/HttpServer/Route/Middleware.php @@ -52,13 +52,22 @@ class Middleware $last = function ($passable) use ($node) { return Dispatch::create($node->handler, $passable)->dispatch(); }; - $middleWares = $this->annotation($node); - $data = array_reduce(array_reverse($middleWares), $this->core(), $last); - $this->middleWares = []; + $data = $this->reduce($last, $this->annotation($node)); return $node->callback = $data; } + /** + * @param $last + * @param $middleWares + * @return mixed|null + */ + private function reduce($last, $middleWares) + { + return array_reduce(array_reverse($middleWares), $this->core(), $last); + } + + /** * @param Node $node * @return array @@ -66,6 +75,7 @@ class Middleware protected function annotation($node) { $middleWares = $this->middleWares; + $this->middleWares = []; if (!$node->hasInterceptor()) { return $this->annotation_limit($node, $middleWares); } diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index 12bb7231..3ee51399 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -39,6 +39,7 @@ class Node extends Application public $callback = []; private $_interceptors = []; + private $_after = []; private $_limits = []; /** @@ -92,6 +93,15 @@ class Node extends Application } + /** + * @return array + */ + public function getAfters() + { + return $this->_after; + } + + /** * @return array @@ -194,6 +204,17 @@ class Node extends Application } + /** + * @param Closure|array|string $handler + * @throws Exception + */ + public function addAfter($handler) + { + $this->_after[] = $handler; + $this->restructure(); + } + + /** * @param Closure|array|string $handler * @throws Exception