From 431e72421c4bfaa274eef334e7244b940358e019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 27 Sep 2021 16:17:02 +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-handler/Abstracts/Handler.php | 21 ++++++--------------- http-handler/Handler.php | 30 ++++++++++++++++++++++++++++++ kiri-engine/Proxy/AspectProxy.php | 2 +- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/http-handler/Abstracts/Handler.php b/http-handler/Abstracts/Handler.php index 10ff1c75..e58c618b 100644 --- a/http-handler/Abstracts/Handler.php +++ b/http-handler/Abstracts/Handler.php @@ -3,6 +3,7 @@ namespace Http\Handler\Abstracts; use Annotation\Inject; +use Exception; use Http\Handler\Handler as CHl; use Http\Message\ServerRequest; use Kiri\Core\Help; @@ -38,7 +39,7 @@ abstract class Handler implements RequestHandlerInterface /** * @param ServerRequestInterface $request * @return ResponseInterface - * @throws \Exception + * @throws Exception */ protected function execute(ServerRequestInterface $request): ResponseInterface { @@ -48,7 +49,7 @@ abstract class Handler implements RequestHandlerInterface $middleware = $this->middlewares[$this->offset]; if (!($middleware instanceof MiddlewareInterface)) { - throw new \Exception('get_implements_class($middleware) not found method process.'); + throw new Exception('get_implements_class($middleware) not found method process.'); } $this->offset++; @@ -57,24 +58,14 @@ abstract class Handler implements RequestHandlerInterface } - private function redecue() - { - return function ($stack, $pipe) { - return function ($passable) use ($stack, $pipe) { - return ([$pipe, 'process'])($passable, $stack); - }; - }; - } - - /** * @param ServerRequestInterface $request * @return mixed - * @throws \Exception + * @throws Exception */ public function dispatcher(ServerRequestInterface $request): mixed { - $response = $this->aspectProxy->proxy($this->handler); + $response = call_user_func($this->handler->callback, ...$this->handler->params); if (!($response instanceof ResponseInterface)) { $response = $this->transferToResponse($response); } @@ -102,7 +93,7 @@ abstract class Handler implements RequestHandlerInterface /** * @param mixed $responseData * @return \Server\Constrict\ResponseInterface - * @throws \Exception + * @throws Exception */ private function transferToResponse(mixed $responseData): ResponseInterface { diff --git a/http-handler/Handler.php b/http-handler/Handler.php index 080355fb..eaebf4c9 100644 --- a/http-handler/Handler.php +++ b/http-handler/Handler.php @@ -2,8 +2,10 @@ namespace Http\Handler; +use Annotation\Aspect; use Closure; use Http\Handler\Abstracts\MiddlewareManager; +use Kiri\Di\NoteManager; use Kiri\Events\EventProvider; use Kiri\Kiri; use Server\Events\OnAfterWorkerStart; @@ -43,10 +45,38 @@ class Handler return; } $this->_middlewares = MiddlewareManager::get($this->callback); + + $aspect = NoteManager::getSpecify_annotation(Aspect::class, $this->callback[0], $this->callback[1]); + if (!is_null($aspect)) { + $this->recover($aspect); + } }); } + /** + * @param Aspect $aspect + */ + public function recover(Aspect $aspect) + { + $aspect = Kiri::getDi()->get($aspect->aspect); + 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, $callback[1]], $params); + $aspect->after($result); + + return $result; + }; + } + + /** * @param array|Closure $callback * @throws \ReflectionException diff --git a/kiri-engine/Proxy/AspectProxy.php b/kiri-engine/Proxy/AspectProxy.php index 596bf908..f9b2bf30 100644 --- a/kiri-engine/Proxy/AspectProxy.php +++ b/kiri-engine/Proxy/AspectProxy.php @@ -17,7 +17,7 @@ class AspectProxy extends AProxy implements ProxyInterface /** - * @param \Http\Handler\Handler $executor + * @param Handler $executor * @return mixed */ public function proxy(Handler $executor): mixed