Files
kiri-router/src/Handler.php
T

223 lines
4.2 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;
2026-06-12 23:57:20 +08:00
use Kiri\Router\Annotate\Defer;
2023-10-17 14:50:46 +08:00
use Kiri\Router\Format\IFormat;
use Kiri\Router\Format\MixedFormat;
2023-11-22 10:19:33 +08:00
use Kiri\Router\Format\NoBody;
2023-04-15 23:31:16 +08:00
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
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 = [];
2025-12-30 20:00:50 +08:00
/**
* @var array
*/
protected array $middlewares = [];
2026-06-12 23:57:20 +08:00
/**
* @var Defer[]
*/
protected array $deferred = [];
2026-04-17 16:30:52 +08:00
protected ?string $sourceFile = null;
protected string $sourceKind = 'attribute';
2023-09-13 12:30:57 +08:00
/**
* @param array|Closure $handler
2024-08-29 13:43:45 +08:00
* @param array $parameters
* @param string|null $parameter
2023-09-13 12:30:57 +08:00
*/
2024-08-29 13:43:45 +08:00
public function __construct(public array|Closure $handler, public array $parameters, ?string $parameter)
2023-09-13 12:30:57 +08:00
{
2024-08-29 13:43:45 +08:00
if ($parameter !== null) {
$this->format = Kiri::getDi()->get($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
}
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
{
2024-12-16 16:29:35 +08:00
if ($method == 'HEAD' || $method == 'OPTIONS') {
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];
}
2025-12-30 20:00:50 +08:00
/**
* @param array $middlewares
* @return void
*/
public function setMiddlewares(array $middlewares): void
{
$this->middlewares = $middlewares;
}
/**
* @return array
*/
public function getMiddlewares(): array
{
return $this->middlewares;
}
2023-09-13 12:30:57 +08:00
2026-04-17 16:30:52 +08:00
public function setSourceFile(?string $sourceFile): void
{
$this->sourceFile = $sourceFile;
}
public function getSourceFile(): ?string
{
return $this->sourceFile;
}
public function setSourceKind(string $sourceKind): void
{
$this->sourceKind = $sourceKind;
}
public function getSourceKind(): string
{
return $this->sourceKind;
}
2026-06-12 23:57:20 +08:00
/**
* @param Defer[] $deferred
* @return void
*/
public function setDeferred(array $deferred): void
{
$this->deferred = $deferred;
}
/**
* @return Defer[]
*/
public function getDeferred(): array
{
return $this->deferred;
}
2023-09-13 12:30:57 +08:00
/**
* @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
2026-06-12 23:57:20 +08:00
$this->executeDeferred();
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
2026-06-12 23:57:20 +08:00
/**
* @return void
*/
private function executeDeferred(): void
{
foreach ($this->deferred as $defer) {
try {
$callback = $defer->callback;
$params = $defer->params;
if (is_array($callback)) {
[$class, $method] = $callback;
$instance = Kiri::getDi()->get($class);
call_user_func([$instance, $method], ...$params);
} else {
$instance = Kiri::getDi()->get($callback);
call_user_func([$instance, '__invoke'], ...$params);
}
} catch (\Throwable $throwable) {
\Kiri::getLogger()->error('Defer callback failed: ' . $throwable->getMessage());
}
}
}
2023-04-15 23:29:27 +08:00
}