This commit is contained in:
2021-09-24 18:46:25 +08:00
parent 5f2fab79e2
commit bfd9f07780
2 changed files with 9 additions and 18 deletions
+3 -14
View File
@@ -56,23 +56,12 @@ class MiddlewareManager extends BaseObject
/** /**
* @param $handler * @param $handler
* @return mixed * @return Iterator|null
*/ */
public static function get($handler): Iterator public static function get($handler): ?Iterator
{ {
if (!($handler instanceof Closure)) { if (!($handler instanceof Closure)) {
if (!isset(static::$_middlewares[$handler[0]])) { return static::$_middlewares[$handler[0]][$handler[1]] ?? null;
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 di(Iterator::class); return di(Iterator::class);
} }
+6 -4
View File
@@ -4,6 +4,7 @@ namespace Server\Service;
use Annotation\Inject; use Annotation\Inject;
use Co\Iterator;
use Exception; use Exception;
use Http\Handler\Abstracts\HandlerManager; use Http\Handler\Abstracts\HandlerManager;
use Http\Handler\Abstracts\MiddlewareManager; use Http\Handler\Abstracts\MiddlewareManager;
@@ -28,7 +29,6 @@ use Server\ExceptionHandlerInterface;
use Server\SInterface\OnCloseInterface; use Server\SInterface\OnCloseInterface;
use Server\SInterface\OnConnectInterface; use Server\SInterface\OnConnectInterface;
use Server\SInterface\OnRequestInterface; use Server\SInterface\OnRequestInterface;
use Swoole\Coroutine\Iterator;
use Swoole\Http\Request; use Swoole\Http\Request;
use Swoole\Http\Response; use Swoole\Http\Response;
use Swoole\Server; use Swoole\Server;
@@ -113,9 +113,11 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
protected function handler(Handler $handler, $PsrRequest): \Psr\Http\Message\ResponseInterface protected function handler(Handler $handler, $PsrRequest): \Psr\Http\Message\ResponseInterface
{ {
$middlewares = MiddlewareManager::get($handler->callback); $middlewares = MiddlewareManager::get($handler->callback);
if ($middlewares instanceof Iterator) {
$dispatcher = new Dispatcher($handler, $middlewares); $dispatcher = new Dispatcher($handler, $middlewares);
} else {
$dispatcher = new Dispatcher($handler, new Iterator());
}
return $dispatcher->handle($PsrRequest); return $dispatcher->handle($PsrRequest);
} }