From 84f4bf14e0d5f2044ed2b73cc04a25c843f36ed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 20 Apr 2021 12:07:46 +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/Node.php | 34 ++++++++++++++++------------------ HttpServer/Route/Reduce.php | 7 ++++++- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index e7a35616..21a11f37 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -113,9 +113,7 @@ class Node extends HttpService $this->annotationInject(get_class($controller), $action); } if (!empty($this->handler)) { - $this->callback = Reduce::reduce(function () { - return call_user_func($this->handler, func_get_args()); - }, $this->annotation()); + $this->callback = Reduce::reduce($this->createDispatch(), $this->annotation()); } return $this; } @@ -127,7 +125,7 @@ class Node extends HttpService public function createDispatch(): Closure { return function () { - return call_user_func($this->handler, func_get_args()); + return call_user_func($this->handler, ...func_get_args()); }; } @@ -534,10 +532,17 @@ class Node extends HttpService $dispatchParams = [\request()]; } if ($this->handler instanceof Closure) { - return call_user_func($this->handler, $dispatchParams); + return call_user_func($this->handler, ...$dispatchParams); } - return $this->runWith($this->callback, $dispatchParams); - return $this->runFilter(...$dispatchParams); + + $validator = $this->getValidator(); + if (!($validator instanceof Validator)) { + return call_user_func($this->callback, ...$dispatchParams); + } + if ($validator->validation()) { + return call_user_func($this->callback, ...$dispatchParams); + } + return Json::to(5005, $validator->getError()); } catch (Throwable $throwable) { $this->addError($throwable, 'throwable'); @@ -549,21 +554,14 @@ class Node extends HttpService /** - * @return mixed - * @throws \Exception + * @return Validator + * @throws Exception */ - private function runFilter(): mixed + private function getValidator(): Validator { /** @var HttpFilter $filter */ $filter = Snowflake::app()->get('filter'); - $validator = $filter->check(get_class($this->handler[0]), $this->handler[1]); - if (!($validator instanceof Validator)) { - return $this->runWith($this->callback, func_get_args()); - } - if (!$validator->validation()) { - return Json::to(5005, $validator->getError()); - } - return $this->runWith($this->callback, func_get_args()); + return $filter->check(get_class($this->handler[0]), $this->handler[1]); } diff --git a/HttpServer/Route/Reduce.php b/HttpServer/Route/Reduce.php index 010dd67c..b8b81452 100644 --- a/HttpServer/Route/Reduce.php +++ b/HttpServer/Route/Reduce.php @@ -6,6 +6,7 @@ namespace HttpServer\Route; use Closure; use HttpServer\IInterface\After; +use HttpServer\IInterface\Middleware; use Snowflake\Core\Json; class Reduce @@ -52,7 +53,11 @@ class Reduce { return function ($stack, $pipe) { return function ($passable) use ($stack, $pipe) { - return call_user_func($pipe, $passable, $stack); + if ($pipe instanceof Middleware) { + return $pipe->onHandler($passable, $stack); + } else { + return call_user_func($pipe, $passable, $stack); + } }; }; }