This commit is contained in:
2023-04-19 13:20:12 +08:00
parent 2789d4a98b
commit c4290b3a43
+27 -5
View File
@@ -3,6 +3,9 @@ declare(strict_types=1);
namespace Kiri\Router;
use Closure;
use Kiri\Di\Context;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
@@ -13,11 +16,18 @@ class Handler implements RequestHandlerInterface
/**
* @param array|\Closure $handler
* @param array $parameter
* @var RequestInterface|mixed|null
*/
public function __construct(public array|\Closure $handler, public array $parameter)
private RequestInterface $request;
/**
* @param array|Closure $handler
* @param array $parameter
* @throws
*/
public function __construct(public array|Closure $handler, public array $parameter)
{
$this->request = \Kiri::service()->get('request');
}
@@ -26,7 +36,7 @@ class Handler implements RequestHandlerInterface
*/
public function isClosure(): bool
{
return $this->handler instanceof \Closure;
return $this->handler instanceof Closure;
}
@@ -62,7 +72,19 @@ class Handler implements RequestHandlerInterface
public function handle(ServerRequestInterface $request): ResponseInterface
{
// TODO: Implement handle() method.
return call_user_func($this->handler, ...$this->parameter);
if ($this->handler instanceof Closure) {
return call_user_func($this->handler, ...$this->parameter);
}
[$controller, $action] = $this->handler;
if ($controller->beforeAction($this->request)) {
$response = call_user_func($this->handler, ...$this->parameter);
$controller->afterAction($response);
} else {
/** @var Response $response */
$response = \Kiri::service()->get('response');
$response->withStatus(500,'BeforeAction error');
}
return $response;
}
}