Files
kiri-router/src/Handler.php
T

157 lines
3.5 KiB
PHP
Raw Normal View History

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;
2024-08-29 12:00:10 +08:00
use Kiri;
2023-10-17 14:50:46 +08:00
use Kiri\Router\Format\ArrayFormat;
use Kiri\Router\Format\IFormat;
use Kiri\Router\Format\MixedFormat;
2023-11-22 10:19:33 +08:00
use Kiri\Router\Format\NoBody;
2023-10-17 14:50:46 +08:00
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-12-19 16:26:23 +08:00
use Psr\Container\ContainerExceptionInterface;
2023-11-22 09:26:18 +08:00
use Psr\Container\ContainerInterface;
2023-12-19 16:26:23 +08:00
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;
2023-12-19 16:43:42 +08:00
use ReflectionFunction;
2023-12-19 16:26:23 +08:00
use ReflectionMethod;
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
2024-08-29 12:00:10 +08:00
/**
* @var array
*/
2023-11-22 10:35:50 +08:00
protected array $methods = [];
2023-11-22 09:26:18 +08:00
/**
* @var ContainerInterface
*/
protected ContainerInterface $container;
2024-08-29 12:00:10 +08:00
/**
* @var array
*/
2023-12-19 16:43:42 +08:00
protected array $parameters;
2023-11-22 09:26:18 +08:00
2023-09-13 12:30:57 +08:00
/**
* @param array|Closure $handler
2023-12-19 16:43:42 +08:00
* @param ReflectionMethod|ReflectionFunction $parameter
2023-09-13 12:30:57 +08:00
*/
2024-08-29 12:00:10 +08:00
public function __construct(public array|Closure $handler, ReflectionMethod|ReflectionFunction $parameter)
2023-09-13 12:30:57 +08:00
{
2024-08-29 12:00:10 +08:00
if ($parameter->getReturnType() != null) {
$this->format = Kiri::getDi()->get($this->returnType($parameter));
2023-11-07 14:28:57 +08:00
} else {
2024-08-29 12:00:10 +08:00
$this->format = Kiri::getDi()->get(MixedFormat::class);
2023-11-07 14:28:57 +08:00
}
2024-08-29 12:00:10 +08:00
$this->parameters = Kiri::getDi()->getMethodParams($parameter);
2023-11-22 09:26:18 +08:00
}
/**
2024-08-29 12:00:10 +08:00
* @param ReflectionMethod $reflectionType
2023-11-22 09:26:18 +08:00
* @return string
*/
2023-12-20 21:42:57 +08:00
protected function returnType(ReflectionMethod $reflectionType): string
2023-11-22 09:26:18 +08:00
{
2023-12-20 21:42:57 +08:00
return match ($reflectionType->getReturnType()->getName()) {
2023-11-22 09:26:18 +08:00
'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
}
2023-11-22 10:35:50 +08:00
/**
* @param string $method
* @return void
2023-12-12 15:35:35 +08:00
* @throws
2023-11-22 10:35:50 +08:00
*/
public function setRequestMethod(string $method): void
{
if ($method == 'HEAD') {
2024-08-29 12:00:10 +08:00
$this->format = Kiri::getDi()->get(NoBody::class);
2023-11-22 10:35:50 +08:00
}
}
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
{
2024-08-29 12:00:10 +08:00
if (!$this->handler instanceof Closure) {
2023-09-13 12:30:57 +08:00
return $this->handler[0] instanceof $interface;
}
return false;
}
/**
* @return string|null
*/
public function getClass(): ?string
{
2024-08-29 12:00:10 +08:00
if ($this->handler instanceof Closure) {
2023-09-13 12:30:57 +08:00
return null;
}
2024-08-29 12:00:10 +08:00
return $this->handler[0];
2023-09-13 12:30:57 +08:00
}
/**
* @return string|null
*/
public function getMethod(): ?string
{
2024-08-29 12:00:10 +08:00
if ($this->handler instanceof Closure) {
2023-09-13 12:30:57 +08:00
return null;
}
return $this->handler[1];
}
/**
* @param ServerRequestInterface $request
* @return ResponseInterface
2023-12-12 15:35:35 +08:00
* @throws
2023-09-13 12:30:57 +08:00
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
2024-08-29 12:00:10 +08:00
$controller = Kiri::getDi()->get($this->handler[0]);
$data = call_user_func([$controller, $this->handler[1]], ...$this->parameters);
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
}