2023-04-15 23:29:27 +08:00
|
|
|
<?php
|
2023-04-16 01:24:30 +08:00
|
|
|
declare(strict_types=1);
|
2023-04-15 23:29:27 +08:00
|
|
|
|
|
|
|
|
namespace Kiri\Router;
|
|
|
|
|
|
2023-04-19 13:20:12 +08:00
|
|
|
use Closure;
|
|
|
|
|
use Kiri\Di\Context;
|
|
|
|
|
use Psr\Http\Message\RequestInterface;
|
2023-04-15 23:31:16 +08:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
|
use ReflectionException;
|
|
|
|
|
|
|
|
|
|
class Handler implements RequestHandlerInterface
|
2023-04-15 23:29:27 +08:00
|
|
|
{
|
|
|
|
|
|
2023-04-15 23:31:16 +08:00
|
|
|
|
|
|
|
|
/**
|
2023-04-19 13:20:12 +08:00
|
|
|
* @var RequestInterface|mixed|null
|
|
|
|
|
*/
|
|
|
|
|
private RequestInterface $request;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array|Closure $handler
|
2023-04-15 23:31:16 +08:00
|
|
|
* @param array $parameter
|
2023-04-19 13:20:12 +08:00
|
|
|
* @throws
|
2023-04-15 23:31:16 +08:00
|
|
|
*/
|
2023-04-19 13:20:12 +08:00
|
|
|
public function __construct(public array|Closure $handler, public array $parameter)
|
2023-04-15 23:31:16 +08:00
|
|
|
{
|
2023-04-19 13:20:12 +08:00
|
|
|
$this->request = \Kiri::service()->get('request');
|
2023-04-15 23:31:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-16 12:44:43 +08:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isClosure(): bool
|
|
|
|
|
{
|
2023-04-19 13:20:12 +08:00
|
|
|
return $this->handler instanceof Closure;
|
2023-04-16 12:44:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function getClass(): ?string
|
|
|
|
|
{
|
|
|
|
|
if ($this->isClosure()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return $this->handler[0]::class;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function getMethod(): ?string
|
|
|
|
|
{
|
|
|
|
|
if ($this->isClosure()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return $this->handler[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-15 23:31:16 +08:00
|
|
|
/**
|
|
|
|
|
* @param ServerRequestInterface $request
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement handle() method.
|
2023-04-19 13:20:12 +08:00
|
|
|
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;
|
2023-04-15 23:31:16 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-15 23:29:27 +08:00
|
|
|
}
|