Files
kiri-router/src/ControllerInterpreter.php
T

108 lines
3.1 KiB
PHP
Raw Normal View History

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
2023-04-15 23:31:16 +08:00
namespace Kiri\Router;
2023-04-15 23:29:27 +08:00
2023-04-15 23:31:16 +08:00
use Closure;
2023-04-19 12:35:39 +08:00
use Exception;
2024-08-29 13:43:45 +08:00
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;
2023-12-01 22:57:25 +08:00
use Psr\Container\ContainerInterface;
2023-04-15 23:29:27 +08:00
use ReflectionClass;
2024-08-29 13:43:45 +08:00
use ReflectionFunction;
2023-04-15 23:29:27 +08:00
use ReflectionMethod;
class ControllerInterpreter
{
2023-09-13 12:30:57 +08:00
/**
* @param object $class
* @param string|ReflectionMethod $method
* @param ReflectionClass|null $reflection
* @return Handler
2023-12-12 15:35:35 +08:00
* @throws
2023-09-13 12:30:57 +08:00
*/
public function addRouteByString(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
{
if (is_null($reflection)) {
2024-08-29 17:01:08 +08:00
$reflection = Kiri::getDi()->getReflectionClass($class::class);
2023-09-13 12:30:57 +08:00
}
return $this->resolveMethod($class, $method, $reflection);
}
/**
* @param Closure $method
* @return Handler
2023-12-12 15:35:35 +08:00
* @throws
2023-09-13 12:30:57 +08:00
*/
public function addRouteByClosure(Closure $method): Handler
{
2024-08-29 13:43:45 +08:00
$reflection = new ReflectionFunction($method);
2023-09-13 12:30:57 +08:00
2024-08-29 13:43:45 +08:00
$parameters = Kiri::getDi()->getFunctionParams($method);
$returnType = $this->getReturnType($reflection);
return new Handler($method, $parameters, $returnType);
2023-09-13 12:30:57 +08:00
}
/**
* @param object $class
* @param string|ReflectionMethod $method
* @param ReflectionClass|null $reflection
* @return Handler
2023-12-12 15:35:35 +08:00
* @throws
2023-09-13 12:30:57 +08:00
*/
public function addRouteByObject(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
{
if (is_null($reflection)) {
2024-08-29 17:01:08 +08:00
$reflection = Kiri::getDi()->getReflectionClass($class::class);
2023-09-13 12:30:57 +08:00
}
return $this->resolveMethod($class, $method, $reflection);
}
/**
* @param object $class
* @param string|ReflectionMethod $reflectionMethod
* @param ReflectionClass $reflectionClass
* @return Handler
2023-12-12 15:35:35 +08:00
* @throws
2023-09-13 12:30:57 +08:00
*/
public function resolveMethod(object $class, string|\ReflectionMethod $reflectionMethod, ReflectionClass $reflectionClass): Handler
{
2023-10-17 15:59:43 +08:00
if (is_string($reflectionMethod)) {
$reflectionMethod = $reflectionClass->getMethod($reflectionMethod);
}
2024-08-29 13:43:45 +08:00
$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
};
2023-09-13 12:30:57 +08:00
}
2023-04-15 23:29:27 +08:00
}