Files
kiri-router/src/ControllerInterpreter.php
T

95 lines
2.5 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;
2023-12-01 22:57:25 +08:00
use Psr\Container\ContainerInterface;
2023-04-15 23:29:27 +08:00
use ReflectionClass;
use ReflectionMethod;
class ControllerInterpreter
{
2023-12-01 22:57:25 +08:00
/**
* @param ContainerInterface $container
*/
public function __construct(public ContainerInterface $container)
{
}
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)) {
2023-12-01 22:57:25 +08:00
$reflection = $this->container->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
{
$reflection = new \ReflectionFunction($method);
2023-12-01 22:57:25 +08:00
$params = $this->container->resolveMethodParams($reflection);
2023-09-13 12:30:57 +08:00
return new Handler($method, $params, $reflection->getReturnType());
}
/**
* @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)) {
2023-12-01 22:57:25 +08:00
$reflection = $this->container->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);
}
2023-10-17 14:50:46 +08:00
$returnType = $reflectionMethod->getReturnType();
if ($returnType instanceof \ReflectionUnionType) {
throw new Exception("Return type error, cannot be multi type.");
}
2023-09-13 12:30:57 +08:00
2023-12-01 22:57:25 +08:00
$parameters = $this->container->getMethodParams($reflectionMethod);
2023-09-13 12:30:57 +08:00
2023-10-17 14:50:46 +08:00
return new Handler([$class, $reflectionMethod->getName()], $parameters, $returnType);
2023-09-13 12:30:57 +08:00
}
2023-04-15 23:29:27 +08:00
}