From ab451a27b35f3a9d06909d7ecbcdc392ed2b84a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 28 Oct 2021 14:02:25 +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 --- http-core/Handler/Abstracts/Handler.php | 8 +++++++- http-core/Handler/Handler.php | 15 +++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/http-core/Handler/Abstracts/Handler.php b/http-core/Handler/Abstracts/Handler.php index c1387782..74bece04 100644 --- a/http-core/Handler/Abstracts/Handler.php +++ b/http-core/Handler/Abstracts/Handler.php @@ -58,7 +58,13 @@ abstract class Handler implements RequestHandlerInterface */ public function dispatcher(ServerRequestInterface $request): mixed { - $response = call_user_func($this->handler->callback, ...$this->handler->params); + if (!empty($this->handler->_aspect)) { + $this->handler->_aspect->before(); + $response = $this->handler->_aspect->invoke($this->handler->callback, $this->handler->params); + $this->handler->_aspect->after($response); + } else { + $response = call_user_func($this->handler->callback, ...$this->handler->params); + } if (!($response instanceof ResponseInterface)) { $response = $this->transferToResponse($response); } diff --git a/http-core/Handler/Handler.php b/http-core/Handler/Handler.php index fdc6f67a..d17dde69 100644 --- a/http-core/Handler/Handler.php +++ b/http-core/Handler/Handler.php @@ -7,6 +7,7 @@ use Closure; use Http\Handler\Abstracts\MiddlewareManager; use Kiri\Di\NoteManager; use Kiri\Events\EventProvider; +use Kiri\IAspect; use Kiri\Kiri; use Server\Events\OnAfterWorkerStart; @@ -23,6 +24,9 @@ class Handler public ?array $params = []; + public IAspect $_aspect; + + public ?array $_middlewares = []; @@ -65,16 +69,7 @@ class Handler if (empty($aspect)) { return; } - $callback = $this->callback; - $params = $this->params; - - $this->params = []; - $this->callback = static function () use ($aspect, $callback, $params) { - $aspect->before(); - $result = $aspect->invoke($callback, $params); - $aspect->after($result); - return $result; - }; + $this->_aspect = $aspect; }