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-09-13 12:30:57 +08:00
|
|
|
use Kiri\Router\Constrict\Stream;
|
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;
|
2023-09-13 16:02:07 +08:00
|
|
|
use ReflectionIntersectionType;
|
2023-09-13 12:30:57 +08:00
|
|
|
use ReflectionNamedType;
|
|
|
|
|
use ReflectionUnionType;
|
2023-04-15 23:31:16 +08:00
|
|
|
|
|
|
|
|
class Handler implements RequestHandlerInterface
|
2023-04-15 23:29:27 +08:00
|
|
|
{
|
|
|
|
|
|
2023-09-13 12:30:57 +08:00
|
|
|
/**
|
|
|
|
|
* @param array|Closure $handler
|
|
|
|
|
* @param array $parameter
|
2023-09-13 16:02:07 +08:00
|
|
|
* @param ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null $responseType
|
2023-09-13 12:30:57 +08:00
|
|
|
*/
|
2023-09-13 16:02:07 +08:00
|
|
|
public function __construct(public array|Closure $handler, public array $parameter, public ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null $responseType)
|
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
|
|
|
|
|
{
|
|
|
|
|
if (!$this->isClosure()) {
|
|
|
|
|
return $this->handler[0] instanceof $interface;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ServerRequestInterface $request
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement handle() method.
|
|
|
|
|
if ($this->responseType->getName() !== 'void') {
|
|
|
|
|
return $this->typeEncode();
|
|
|
|
|
}
|
|
|
|
|
call_user_func($this->handler, ...$this->parameter);
|
|
|
|
|
return response();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
protected function typeEncode(): ResponseInterface
|
|
|
|
|
{
|
|
|
|
|
$result = call_user_func($this->handler, ...$this->parameter);
|
|
|
|
|
if ($result instanceof ResponseInterface) {
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
if (is_object($result)) {
|
|
|
|
|
$result = '[object]';
|
|
|
|
|
} else if (is_array($result)) {
|
|
|
|
|
$result = json_encode($result, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
}
|
|
|
|
|
return response()->withBody(new Stream($result));
|
|
|
|
|
}
|
2023-04-15 23:31:16 +08:00
|
|
|
|
2023-04-15 23:29:27 +08:00
|
|
|
}
|