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;
|
2023-10-17 14:50:46 +08:00
|
|
|
use Kiri\Router\Format\ArrayFormat;
|
|
|
|
|
use Kiri\Router\Format\IFormat;
|
|
|
|
|
use Kiri\Router\Format\MixedFormat;
|
|
|
|
|
use Kiri\Router\Format\OtherFormat;
|
2023-10-17 15:54:46 +08:00
|
|
|
use Kiri\Router\Format\ResponseFormat;
|
2023-10-17 14:50:46 +08:00
|
|
|
use Kiri\Router\Format\VoidFormat;
|
2023-11-22 09:26:18 +08:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
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;
|
2023-09-13 12:30:57 +08:00
|
|
|
use ReflectionNamedType;
|
2023-04-15 23:31:16 +08:00
|
|
|
|
|
|
|
|
class Handler implements RequestHandlerInterface
|
2023-04-15 23:29:27 +08:00
|
|
|
{
|
|
|
|
|
|
2023-10-17 14:50:46 +08:00
|
|
|
/**
|
|
|
|
|
* @var IFormat
|
|
|
|
|
*/
|
2023-10-17 15:55:36 +08:00
|
|
|
protected mixed $format;
|
2023-10-17 14:50:46 +08:00
|
|
|
|
|
|
|
|
|
2023-11-22 09:26:18 +08:00
|
|
|
/**
|
|
|
|
|
* @var ContainerInterface
|
|
|
|
|
*/
|
|
|
|
|
protected ContainerInterface $container;
|
|
|
|
|
|
|
|
|
|
|
2023-09-13 12:30:57 +08:00
|
|
|
/**
|
|
|
|
|
* @param array|Closure $handler
|
|
|
|
|
* @param array $parameter
|
2023-11-07 14:28:57 +08:00
|
|
|
* @param ReflectionNamedType|null $reflectionType
|
2023-10-17 14:50:46 +08:00
|
|
|
* @throws ReflectionException
|
2023-11-22 09:26:18 +08:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
2023-09-13 12:30:57 +08:00
|
|
|
*/
|
2023-11-07 14:28:57 +08:00
|
|
|
public function __construct(public array|Closure $handler, public array $parameter, public ?ReflectionNamedType $reflectionType)
|
2023-09-13 12:30:57 +08:00
|
|
|
{
|
2023-11-22 09:26:18 +08:00
|
|
|
$this->container = \Kiri::getDi();
|
|
|
|
|
if ($this->reflectionType != null) {
|
|
|
|
|
$this->format = $this->container->get($this->returnType());
|
2023-11-07 14:28:57 +08:00
|
|
|
} else {
|
2023-11-22 09:26:18 +08:00
|
|
|
$this->format = $this->container->get(MixedFormat::class);
|
2023-11-07 14:28:57 +08:00
|
|
|
}
|
2023-11-22 09:26:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function returnType(): string
|
|
|
|
|
{
|
|
|
|
|
return match ($this->reflectionType->getName()) {
|
|
|
|
|
'array' => ArrayFormat::class,
|
|
|
|
|
'mixed', 'object' => MixedFormat::class,
|
|
|
|
|
'int', 'string', 'bool' => OtherFormat::class,
|
|
|
|
|
'void' => VoidFormat::class,
|
|
|
|
|
default => ResponseFormat::class
|
|
|
|
|
};
|
2023-09-13 12:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isClosure(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->handler instanceof Closure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $interface
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function implement(string $interface): bool
|
|
|
|
|
{
|
|
|
|
|
if (!$this->isClosure()) {
|
|
|
|
|
return $this->handler[0] instanceof $interface;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ServerRequestInterface $request
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface
|
|
|
|
|
{
|
2023-10-17 14:50:46 +08:00
|
|
|
$data = call_user_func($this->handler, ...$this->parameter);
|
2023-10-18 10:58:24 +08:00
|
|
|
|
|
|
|
|
/** 根据返回类型 */
|
2023-10-17 16:55:34 +08:00
|
|
|
return $this->format->call($data);
|
2023-09-13 12:30:57 +08:00
|
|
|
}
|
2023-04-15 23:31:16 +08:00
|
|
|
|
2023-04-15 23:29:27 +08:00
|
|
|
}
|