111
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Http\Handler\Abstracts;
|
namespace Http\Handler\Abstracts;
|
||||||
|
|
||||||
|
use Annotation\Inject;
|
||||||
use Http\Handler\Handler as CHl;
|
use Http\Handler\Handler as CHl;
|
||||||
use Http\Message\ServerRequest;
|
use Http\Message\ServerRequest;
|
||||||
use Kiri\Core\Help;
|
use Kiri\Core\Help;
|
||||||
@@ -19,20 +20,27 @@ abstract class Handler implements RequestHandlerInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
#[Inject(AspectProxy::class)]
|
||||||
protected AspectProxy $aspectProxy;
|
protected AspectProxy $aspectProxy;
|
||||||
|
|
||||||
|
public CHl $handler;
|
||||||
|
public ?Iterator $middlewares = null;
|
||||||
|
|
||||||
protected int $offset = 0;
|
/**
|
||||||
|
* @param \Http\Handler\Handler $handler
|
||||||
|
*/
|
||||||
|
public function setHandler(CHl $handler): void
|
||||||
|
{
|
||||||
|
$this->handler = $handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
* @param \Swoole\Coroutine\Iterator|null $middlewares
|
||||||
* @param CHl $handler
|
*/
|
||||||
* @param null|Iterator $middlewares
|
public function setMiddlewares(?Iterator $middlewares): void
|
||||||
*/
|
{
|
||||||
public function __construct(public CHl $handler, public ?Iterator $middlewares)
|
$this->middlewares = $middlewares;
|
||||||
{
|
}
|
||||||
$this->aspectProxy = Kiri::getDi()->get(AspectProxy::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,16 +50,16 @@ abstract class Handler implements RequestHandlerInterface
|
|||||||
*/
|
*/
|
||||||
protected function execute(ServerRequestInterface $request): ResponseInterface
|
protected function execute(ServerRequestInterface $request): ResponseInterface
|
||||||
{
|
{
|
||||||
if ($this->middlewares->count() < 1) {
|
if (empty($this->middlewares) || !$this->middlewares->valid()) {
|
||||||
return $this->dispatcher($request);
|
return $this->dispatcher($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
$middleware = $this->middlewares->offsetGet($this->offset);
|
$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.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->offset++;
|
$this->middlewares->next();
|
||||||
|
|
||||||
return $middleware->process($request, $this);
|
return $middleware->process($request, $this);
|
||||||
}
|
}
|
||||||
@@ -62,7 +70,7 @@ abstract class Handler implements RequestHandlerInterface
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
protected function dispatcher(ServerRequestInterface $request): mixed
|
public function dispatcher(ServerRequestInterface $request): mixed
|
||||||
{
|
{
|
||||||
$response = $this->aspectProxy->proxy($this->handler);
|
$response = $this->aspectProxy->proxy($this->handler);
|
||||||
if (!($response instanceof ResponseInterface)) {
|
if (!($response instanceof ResponseInterface)) {
|
||||||
|
|||||||
@@ -47,6 +47,9 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
|
|||||||
public Router $router;
|
public Router $router;
|
||||||
|
|
||||||
|
|
||||||
|
public Dispatcher $dispatcher;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ExceptionHandlerInterface
|
* @var ExceptionHandlerInterface
|
||||||
*/
|
*/
|
||||||
@@ -64,6 +67,7 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
|
|||||||
}
|
}
|
||||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||||
$this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class);
|
$this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class);
|
||||||
|
$this->dispatcher = Kiri::getDi()->get(Dispatcher::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -113,12 +117,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) {
|
$this->dispatcher->setHandler($handler);
|
||||||
$dispatcher = new Dispatcher($handler, $middlewares);
|
if (!empty($middlewares)) {
|
||||||
} else {
|
$this->dispatcher->setMiddlewares($middlewares);
|
||||||
$dispatcher = new Dispatcher($handler, new Iterator());
|
}
|
||||||
}
|
return $this->dispatcher->handle($PsrRequest);
|
||||||
return $dispatcher->handle($PsrRequest);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user