From bfd9f077809cdb8d8b7d653297c1bd34a2cf3abb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 24 Sep 2021 18:46:25 +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/MiddlewareManager.php | 17 +++-------------- http-server/Service/Http.php | 10 ++++++---- 2 files changed, 9 insertions(+), 18 deletions(-) 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); }