This commit is contained in:
2021-09-24 18:39:46 +08:00
parent b153ea4aaa
commit e5efe8eb84
2 changed files with 15 additions and 6 deletions
+6 -3
View File
@@ -28,8 +28,6 @@ abstract class Handler implements RequestHandlerInterface
public function __construct(public CHl $handler, public ?Iterator $middlewares) public function __construct(public CHl $handler, public ?Iterator $middlewares)
{ {
$this->aspectProxy = Kiri::getDi()->get(AspectProxy::class); $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 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); return $this->dispatcher($request);
} }
$middleware = $this->middlewares->current();
if (!($middleware instanceof MiddlewareInterface)) { if (!($middleware instanceof MiddlewareInterface)) {
throw new \Exception('get_implements_class($middleware) not found method process.'); 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 protected function dispatcher(ServerRequestInterface $request): mixed
{ {
if ($this->middlewares->count() > 0) {
$this->middlewares->rewind();
}
$response = $this->aspectProxy->proxy($this->handler); $response = $this->aspectProxy->proxy($this->handler);
if (!($response instanceof ResponseInterface)) { if (!($response instanceof ResponseInterface)) {
$response = $this->transferToResponse($response); $response = $this->transferToResponse($response);
+9 -3
View File
@@ -56,10 +56,16 @@ class MiddlewareManager extends BaseObject
*/ */
public static function get($handler): mixed public static function get($handler): mixed
{ {
if ($handler instanceof Closure || !isset(static::$_middlewares[$handler[0]])) { if (!($handler instanceof Closure)) {
return null; 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();
} }