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
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user