From 5f2fab79e20cacf61471c7f7c8e1b44f3d00e5d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 24 Sep 2021 18:42:30 +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 | 1 + http-handler/Abstracts/MiddlewareManager.php | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/http-handler/Abstracts/Handler.php b/http-handler/Abstracts/Handler.php index eb54e5b3..b74197a1 100644 --- a/http-handler/Abstracts/Handler.php +++ b/http-handler/Abstracts/Handler.php @@ -5,6 +5,7 @@ namespace Http\Handler\Abstracts; use Http\Handler\Handler as CHl; use Http\Message\ServerRequest; use Kiri\Core\Help; +use Kiri\Events\EventDispatch; use Kiri\Kiri; use Kiri\Proxy\AspectProxy; use Psr\Http\Message\ResponseInterface; diff --git a/http-handler/Abstracts/MiddlewareManager.php b/http-handler/Abstracts/MiddlewareManager.php index 7dc6f4c9..845fb54a 100644 --- a/http-handler/Abstracts/MiddlewareManager.php +++ b/http-handler/Abstracts/MiddlewareManager.php @@ -16,6 +16,10 @@ use Kiri\Abstracts\BaseObject; class MiddlewareManager extends BaseObject { + + /** + * @var array + */ private static array $_middlewares = []; @@ -54,7 +58,7 @@ class MiddlewareManager extends BaseObject * @param $handler * @return mixed */ - public static function get($handler): mixed + public static function get($handler): Iterator { if (!($handler instanceof Closure)) { if (!isset(static::$_middlewares[$handler[0]])) { @@ -63,9 +67,14 @@ class MiddlewareManager extends BaseObject if (!isset(static::$_middlewares[$handler[0]][$handler[1]])) { static::$_middlewares[$handler[0]][$handler[1]] = new Iterator(); } - return static::$_middlewares[$handler[0]][$handler[1]]; + /** @var Iterator $iterator */ + $iterator = static::$_middlewares[$handler[0]][$handler[1]]; + if ($iterator->count() > 0 && !$iterator->valid()) { + $iterator->rewind(); + } + return $iterator; } - return new Iterator(); + return di(Iterator::class); }