From e5efe8eb8494cdb41adc0d26a7fdbb087cd877bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 24 Sep 2021 18:39: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 --- http-handler/Abstracts/Handler.php | 9 ++++++--- http-handler/Abstracts/MiddlewareManager.php | 12 +++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/http-handler/Abstracts/Handler.php b/http-handler/Abstracts/Handler.php index 39e0bb01..eb54e5b3 100644 --- a/http-handler/Abstracts/Handler.php +++ b/http-handler/Abstracts/Handler.php @@ -28,8 +28,6 @@ abstract class Handler implements RequestHandlerInterface public function __construct(public CHl $handler, public ?Iterator $middlewares) { $this->aspectProxy = Kiri::getDi()->get(AspectProxy::class); - - $this->middlewares->rewind(); } @@ -40,9 +38,11 @@ abstract class Handler implements RequestHandlerInterface */ protected function execute(ServerRequestInterface $request): ResponseInterface { - if (empty($this->middlewares) || !($middleware = $this->middlewares->current())) { + if ($this->middlewares->count() < 1 || !$this->middlewares->valid()) { return $this->dispatcher($request); } + + $middleware = $this->middlewares->current(); if (!($middleware instanceof MiddlewareInterface)) { throw new \Exception('get_implements_class($middleware) not found method process.'); } @@ -60,6 +60,9 @@ abstract class Handler implements RequestHandlerInterface */ protected function dispatcher(ServerRequestInterface $request): mixed { + if ($this->middlewares->count() > 0) { + $this->middlewares->rewind(); + } $response = $this->aspectProxy->proxy($this->handler); if (!($response instanceof ResponseInterface)) { $response = $this->transferToResponse($response); diff --git a/http-handler/Abstracts/MiddlewareManager.php b/http-handler/Abstracts/MiddlewareManager.php index 317f86e1..7dc6f4c9 100644 --- a/http-handler/Abstracts/MiddlewareManager.php +++ b/http-handler/Abstracts/MiddlewareManager.php @@ -56,10 +56,16 @@ class MiddlewareManager extends BaseObject */ public static function get($handler): mixed { - if ($handler instanceof Closure || !isset(static::$_middlewares[$handler[0]])) { - return null; + if (!($handler instanceof Closure)) { + if (!isset(static::$_middlewares[$handler[0]])) { + static::$_middlewares[$handler[0]] = []; + } + if (!isset(static::$_middlewares[$handler[0]][$handler[1]])) { + static::$_middlewares[$handler[0]][$handler[1]] = new Iterator(); + } + return static::$_middlewares[$handler[0]][$handler[1]]; } - return static::$_middlewares[$handler[0]][$handler[1]] ?? null; + return new Iterator(); }