diff --git a/http-handler/Abstracts/MiddlewareManager.php b/http-handler/Abstracts/MiddlewareManager.php index 845fb54a..327e2669 100644 --- a/http-handler/Abstracts/MiddlewareManager.php +++ b/http-handler/Abstracts/MiddlewareManager.php @@ -56,23 +56,12 @@ class MiddlewareManager extends BaseObject /** * @param $handler - * @return mixed + * @return Iterator|null */ - public static function get($handler): Iterator + public static function get($handler): ?Iterator { 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(); - } - /** @var Iterator $iterator */ - $iterator = static::$_middlewares[$handler[0]][$handler[1]]; - if ($iterator->count() > 0 && !$iterator->valid()) { - $iterator->rewind(); - } - return $iterator; + return static::$_middlewares[$handler[0]][$handler[1]] ?? null; } return di(Iterator::class); } diff --git a/http-server/Service/Http.php b/http-server/Service/Http.php index b7bc0b59..785f561f 100644 --- a/http-server/Service/Http.php +++ b/http-server/Service/Http.php @@ -4,6 +4,7 @@ namespace Server\Service; use Annotation\Inject; +use Co\Iterator; use Exception; use Http\Handler\Abstracts\HandlerManager; use Http\Handler\Abstracts\MiddlewareManager; @@ -28,7 +29,6 @@ use Server\ExceptionHandlerInterface; use Server\SInterface\OnCloseInterface; use Server\SInterface\OnConnectInterface; use Server\SInterface\OnRequestInterface; -use Swoole\Coroutine\Iterator; use Swoole\Http\Request; use Swoole\Http\Response; use Swoole\Server; @@ -113,9 +113,11 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface protected function handler(Handler $handler, $PsrRequest): \Psr\Http\Message\ResponseInterface { $middlewares = MiddlewareManager::get($handler->callback); - - $dispatcher = new Dispatcher($handler, $middlewares); - + if ($middlewares instanceof Iterator) { + $dispatcher = new Dispatcher($handler, $middlewares); + } else { + $dispatcher = new Dispatcher($handler, new Iterator()); + } return $dispatcher->handle($PsrRequest); }