This commit is contained in:
as2252258@163.com
2021-09-26 00:25:29 +08:00
parent 4ecb6187fa
commit 477de51126
+9 -24
View File
@@ -23,52 +23,37 @@ abstract class Handler implements RequestHandlerInterface
#[Inject(AspectProxy::class)] #[Inject(AspectProxy::class)]
protected AspectProxy $aspectProxy; protected AspectProxy $aspectProxy;
public CHl $handler;
public ?Iterator $middlewares = null;
/** protected int $offset = 0;
* @param \Kiri\Proxy\AspectProxy $aspectProxy
*/
public function setAspectProxy(AspectProxy $aspectProxy): void
{
$this->aspectProxy = $aspectProxy;
}
/** /**
* @param \Http\Handler\Handler $handler * @param \Http\Handler\Handler $handler
* @param array|null $middlewares
*/ */
public function setHandler(CHl $handler): void public function __construct(public CHl $handler, public ?array $middlewares)
{ {
$this->handler = $handler; $this->aspectProxy = Kiri::getDi()->get(AspectProxy::class);
} }
/** /**
* @param \Swoole\Coroutine\Iterator|null $middlewares
*/
public function setMiddlewares(?Iterator $middlewares): void
{
$this->middlewares = $middlewares;
}
/**
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @return ResponseInterface * @return ResponseInterface
* @throws \Exception * @throws \Exception
*/ */
protected function execute(ServerRequestInterface $request): ResponseInterface protected function execute(ServerRequestInterface $request): ResponseInterface
{ {
if (empty($this->middlewares) || !$this->middlewares->valid()) { if (empty($this->middlewares)) {
$this->middlewares->rewind();
return $this->dispatcher($request); return $this->dispatcher($request);
} }
$middleware = $this->middlewares->current(); $middleware = $this->middlewares[$this->offset];
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.');
} }
$this->middlewares->next(); $this->offset++;
return $middleware->process($request, $this); return $middleware->process($request, $this);
} }