From be1badfa5c8c87ebe2503a1518abe96a2aeee98f Mon Sep 17 00:00:00 2001 From: xl Date: Thu, 29 Aug 2024 13:43:45 +0800 Subject: [PATCH] eee --- src/ControllerInterpreter.php | 36 ++++++++++++++++++++++++++++++++--- src/Handler.php | 32 +++++-------------------------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/ControllerInterpreter.php b/src/ControllerInterpreter.php index d64d868..a5d30a6 100644 --- a/src/ControllerInterpreter.php +++ b/src/ControllerInterpreter.php @@ -5,8 +5,15 @@ namespace Kiri\Router; use Closure; use Exception; +use Kiri; +use Kiri\Router\Format\ArrayFormat; +use Kiri\Router\Format\MixedFormat; +use Kiri\Router\Format\OtherFormat; +use Kiri\Router\Format\ResponseFormat; +use Kiri\Router\Format\VoidFormat; use Psr\Container\ContainerInterface; use ReflectionClass; +use ReflectionFunction; use ReflectionMethod; class ControllerInterpreter @@ -44,9 +51,13 @@ class ControllerInterpreter */ public function addRouteByClosure(Closure $method): Handler { - $reflection = new \ReflectionFunction($method); + $reflection = new ReflectionFunction($method); - return new Handler($method, $reflection); + $parameters = Kiri::getDi()->getFunctionParams($method); + + $returnType = $this->getReturnType($reflection); + + return new Handler($method, $parameters, $returnType); } @@ -79,7 +90,26 @@ class ControllerInterpreter $reflectionMethod = $reflectionClass->getMethod($reflectionMethod); } - return new Handler([$class::class, $reflectionMethod->getName()], $reflectionMethod); + $parameters = Kiri::getDi()->getMethodParams($reflectionMethod); + $returnType = $this->getReturnType($reflectionMethod); + + return new Handler([$class::class, $reflectionMethod->getName()], $parameters, $returnType); + } + + + /** + * @param ReflectionMethod|ReflectionFunction $reflectionMethod + * @return string + */ + protected function getReturnType(ReflectionMethod|ReflectionFunction $reflectionMethod): string + { + return match ($reflectionMethod->getReturnType()?->getName()) { + 'array' => ArrayFormat::class, + 'mixed', 'object' => MixedFormat::class, + 'int', 'string', 'bool' => OtherFormat::class, + 'void' => VoidFormat::class, + default => ResponseFormat::class + }; } } diff --git a/src/Handler.php b/src/Handler.php index 12d4485..5262934 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -42,40 +42,18 @@ class Handler implements RequestHandlerInterface protected ContainerInterface $container; - /** - * @var array - */ - protected array $parameters; - - /** * @param array|Closure $handler - * @param ReflectionMethod|ReflectionFunction $parameter + * @param array $parameters + * @param string|null $parameter */ - public function __construct(public array|Closure $handler, ReflectionMethod|ReflectionFunction $parameter) + public function __construct(public array|Closure $handler, public array $parameters, ?string $parameter) { - if ($parameter->getReturnType() != null) { - $this->format = Kiri::getDi()->get($this->returnType($parameter)); + if ($parameter !== null) { + $this->format = Kiri::getDi()->get($parameter); } else { $this->format = Kiri::getDi()->get(MixedFormat::class); } - $this->parameters = Kiri::getDi()->getMethodParams($parameter); - } - - - /** - * @param ReflectionMethod $reflectionType - * @return string - */ - protected function returnType(ReflectionMethod $reflectionType): string - { - return match ($reflectionType->getReturnType()->getName()) { - 'array' => ArrayFormat::class, - 'mixed', 'object' => MixedFormat::class, - 'int', 'string', 'bool' => OtherFormat::class, - 'void' => VoidFormat::class, - default => ResponseFormat::class - }; }