Files
kiri-router/src/Handler.php
T

75 lines
1.3 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-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-15 23:31:16 +08:00
/**
* @param array|\Closure $handler
* @param array $parameter
*/
public function __construct(public array|\Closure $handler, public array $parameter)
{
}
2023-04-16 12:44:43 +08:00
/**
* @return bool
*/
public function isClosure(): bool
{
return $this->handler instanceof \Closure;
}
/**
* @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.
$result = call_user_func($this->handler, ...$this->parameter);
if ($result instanceof ResponseInterface) {
return $result;
} else {
$response = \Kiri::getDi()->get(ResponseInterface::class);
return $response->rewrite();
}
}
2023-04-15 23:29:27 +08:00
}