111
This commit is contained in:
@@ -23,31 +23,17 @@ 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -58,17 +44,16 @@ abstract class Handler implements RequestHandlerInterface
|
|||||||
*/
|
*/
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user