Files
kiri-router/src/Handler.php
T

120 lines
2.4 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\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 = [];
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];
}
/**
* @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
}