Files
kiri-router/src/Handler.php
T

83 lines
1.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;
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;
class Handler implements RequestHandlerInterface
2023-04-15 23:29:27 +08:00
{
2023-04-19 13:20:12 +08:00
/**
* @param array|Closure $handler
2023-04-15 23:31:16 +08:00
* @param array $parameter
2023-04-19 13:20:12 +08:00
* @throws
2023-04-15 23:31:16 +08:00
*/
2023-04-19 13:20:12 +08:00
public function __construct(public array|Closure $handler, public array $parameter)
2023-04-15 23:31:16 +08:00
{
}
2023-04-16 12:44:43 +08:00
/**
* @return bool
*/
public function isClosure(): bool
{
2023-04-19 13:20:12 +08:00
return $this->handler instanceof Closure;
2023-04-16 12:44:43 +08:00
}
2023-04-22 02:04:31 +08:00
/**
* @param string $interface
* @return bool
*/
public function implement(string $interface): bool
{
if (!$this->isClosure()) {
return $this->handler[0] instanceof $interface;
}
return false;
}
2023-04-16 12:44:43 +08:00
/**
* @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];
}
2023-04-15 23:31:16 +08:00
/**
* @param ServerRequestInterface $request
* @return ResponseInterface
* @throws ReflectionException
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
// TODO: Implement handle() method.
2023-04-19 13:23:58 +08:00
return call_user_func($this->handler, ...$this->parameter);
2023-04-15 23:31:16 +08:00
}
2023-04-15 23:29:27 +08:00
}