Files
kiri-router/src/ControllerInterpreter.php
T

79 lines
1.9 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-15 23:29:27 +08:00
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
class ControllerInterpreter
{
/**
2023-04-15 23:31:16 +08:00
* @param object $class
2023-04-15 23:29:27 +08:00
* @param string|ReflectionMethod $method
* @param ReflectionClass|null $reflection
2023-04-15 23:31:16 +08:00
* @return Handler
2023-04-15 23:29:27 +08:00
* @throws ReflectionException
*/
2023-04-15 23:31:16 +08:00
public function addRouteByString(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
2023-04-15 23:29:27 +08:00
{
if (is_null($reflection)) {
2023-04-15 23:31:16 +08:00
$reflection = \Kiri::getDi()->getReflectionClass($class::class);
2023-04-15 23:29:27 +08:00
}
2023-04-15 23:31:16 +08:00
return $this->resolveMethod($method, $reflection);
2023-04-15 23:29:27 +08:00
}
/**
2023-04-15 23:31:16 +08:00
* @param Closure $method
* @return Handler
2023-04-15 23:29:27 +08:00
* @throws ReflectionException
*/
2023-04-15 23:31:16 +08:00
public function addRouteByClosure(Closure $method): Handler
2023-04-15 23:29:27 +08:00
{
2023-04-15 23:31:16 +08:00
$reflection = \Kiri::getDi()->getFunctionParams($method);
return new Handler($method, $reflection);
2023-04-15 23:29:27 +08:00
}
/**
* @param object $class
2023-04-15 23:31:16 +08:00
* @param string|ReflectionMethod $method
* @param ReflectionClass|null $reflection
* @return Handler
2023-04-15 23:29:27 +08:00
* @throws ReflectionException
*/
2023-04-15 23:31:16 +08:00
public function addRouteByObject(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
2023-04-15 23:29:27 +08:00
{
2023-04-15 23:31:16 +08:00
if (is_null($reflection)) {
$reflection = \Kiri::getDi()->getReflectionClass($class::class);
2023-04-15 23:29:27 +08:00
}
2023-04-15 23:31:16 +08:00
return $this->resolveMethod($method, $reflection);
2023-04-15 23:29:27 +08:00
}
/**
2023-04-15 23:31:16 +08:00
* @param string|ReflectionMethod $reflectionMethod
2023-04-15 23:29:27 +08:00
* @param ReflectionClass $reflectionClass
2023-04-15 23:31:16 +08:00
* @return Handler
* @throws ReflectionException
2023-04-15 23:29:27 +08:00
*/
2023-04-15 23:31:16 +08:00
public function resolveMethod(string|\ReflectionMethod $reflectionMethod, ReflectionClass $reflectionClass): Handler
2023-04-15 23:29:27 +08:00
{
2023-04-15 23:31:16 +08:00
if (is_string($reflectionMethod)) {
$reflectionMethod = $reflectionClass->getMethod($reflectionMethod);
2023-04-15 23:29:27 +08:00
}
2023-04-15 23:31:16 +08:00
$container = \Kiri::getDi();
$parameters = $container->getMethodParams($reflectionMethod);
2023-04-15 23:29:27 +08:00
2023-04-16 02:58:01 +08:00
return new Handler([$container->get($reflectionClass->getName()), $reflectionMethod->getName()], $parameters);
2023-04-15 23:29:27 +08:00
}
}