eee
This commit is contained in:
@@ -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
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-27
@@ -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
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user