This commit is contained in:
xl
2024-08-29 12:00:10 +08:00
parent 6efd221585
commit 1795b59b88
4 changed files with 37 additions and 25 deletions
+22 -15
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Kiri\Router;
use Closure;
use Kiri;
use Kiri\Router\Format\ArrayFormat;
use Kiri\Router\Format\IFormat;
use Kiri\Router\Format\MixedFormat;
@@ -29,6 +30,9 @@ class Handler implements RequestHandlerInterface
protected mixed $format;
/**
* @var array
*/
protected array $methods = [];
@@ -37,29 +41,30 @@ class Handler implements RequestHandlerInterface
*/
protected ContainerInterface $container;
/**
* @var array
*/
protected array $parameters;
/**
* @param array|Closure $handler
* @param ReflectionMethod|ReflectionFunction $parameter
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __construct(public array|Closure $handler, public ReflectionMethod|ReflectionFunction $parameter)
public function __construct(public array|Closure $handler, ReflectionMethod|ReflectionFunction $parameter)
{
$this->container = \Kiri::getDi();
if ($this->parameter->getReturnType() != null) {
$this->format = $this->container->get($this->returnType($parameter));
if ($parameter->getReturnType() != null) {
$this->format = Kiri::getDi()->get($this->returnType($parameter));
} else {
$this->format = $this->container->get(MixedFormat::class);
$this->format = Kiri::getDi()->get(MixedFormat::class);
}
$this->parameters = $this->container->getMethodParams($this->parameter);
$this->parameters = Kiri::getDi()->getMethodParams($parameter);
}
/**
* @param $reflectionType
* @param ReflectionMethod $reflectionType
* @return string
*/
protected function returnType(ReflectionMethod $reflectionType): string
@@ -82,7 +87,7 @@ class Handler implements RequestHandlerInterface
public function setRequestMethod(string $method): void
{
if ($method == 'HEAD') {
$this->format = $this->container->get(NoBody::class);
$this->format = Kiri::getDi()->get(NoBody::class);
}
}
@@ -102,7 +107,7 @@ class Handler implements RequestHandlerInterface
*/
public function implement(string $interface): bool
{
if (!$this->isClosure()) {
if (!$this->handler instanceof Closure) {
return $this->handler[0] instanceof $interface;
}
return false;
@@ -114,10 +119,10 @@ class Handler implements RequestHandlerInterface
*/
public function getClass(): ?string
{
if ($this->isClosure()) {
if ($this->handler instanceof Closure) {
return null;
}
return $this->handler[0]::class;
return $this->handler[0];
}
@@ -126,7 +131,7 @@ class Handler implements RequestHandlerInterface
*/
public function getMethod(): ?string
{
if ($this->isClosure()) {
if ($this->handler instanceof Closure) {
return null;
}
return $this->handler[1];
@@ -140,7 +145,9 @@ class Handler implements RequestHandlerInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$data = call_user_func($this->handler, ...$this->parameters);
$controller = Kiri::getDi()->get($this->handler[0]);
$data = call_user_func([$controller, $this->handler[1]], ...$this->parameters);
/** 根据返回类型 */
return $this->format->call($data);