This commit is contained in:
xl
2023-11-22 09:26:18 +08:00
parent 13cd95ad35
commit f3ae6cbe4c
6 changed files with 64 additions and 25 deletions
+30 -10
View File
@@ -10,6 +10,9 @@ use Kiri\Router\Format\MixedFormat;
use Kiri\Router\Format\OtherFormat;
use Kiri\Router\Format\ResponseFormat;
use Kiri\Router\Format\VoidFormat;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
@@ -25,26 +28,43 @@ class Handler implements RequestHandlerInterface
protected mixed $format;
/**
* @var ContainerInterface
*/
protected ContainerInterface $container;
/**
* @param array|Closure $handler
* @param array $parameter
* @param ReflectionNamedType|null $reflectionType
* @throws ReflectionException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __construct(public array|Closure $handler, public array $parameter, public ?ReflectionNamedType $reflectionType)
{
if ($this->reflectionType == null) {
$type = MixedFormat::class;
$this->container = \Kiri::getDi();
if ($this->reflectionType != null) {
$this->format = $this->container->get($this->returnType());
} else {
$type = match ($this->reflectionType->getName()) {
'array' => ArrayFormat::class,
'mixed', 'object' => MixedFormat::class,
'int', 'string', 'bool' => OtherFormat::class,
'void' => VoidFormat::class,
default => ResponseFormat::class
};
$this->format = $this->container->get(MixedFormat::class);
}
$this->format = di($type);
}
/**
* @return string
*/
protected function returnType(): string
{
return match ($this->reflectionType->getName()) {
'array' => ArrayFormat::class,
'mixed', 'object' => MixedFormat::class,
'int', 'string', 'bool' => OtherFormat::class,
'void' => VoidFormat::class,
default => ResponseFormat::class
};
}