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-19 16:26:23 +08:00
|
|
|
return new Handler($method, $reflection);
|
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)) {
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 12:00:10 +08:00
|
|
|
return new Handler([$class::class, $reflectionMethod->getName()], $reflectionMethod);
|
2023-09-13 12:30:57 +08:00
|
|
|
}
|
2023-04-15 23:29:27 +08:00
|
|
|
|
|
|
|
|
}
|