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(); }