This commit is contained in:
2023-12-01 22:57:25 +08:00
parent 58a5127e88
commit b7687f8a79
+13 -5
View File
@@ -6,6 +6,7 @@ namespace Kiri\Router;
use Closure; use Closure;
use Exception; use Exception;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
@@ -15,6 +16,14 @@ class ControllerInterpreter
{ {
/**
* @param ContainerInterface $container
*/
public function __construct(public ContainerInterface $container)
{
}
/** /**
* @param object $class * @param object $class
* @param string|ReflectionMethod $method * @param string|ReflectionMethod $method
@@ -27,7 +36,7 @@ class ControllerInterpreter
public function addRouteByString(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler public function addRouteByString(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
{ {
if (is_null($reflection)) { if (is_null($reflection)) {
$reflection = \Kiri::getDi()->getReflectionClass($class::class); $reflection = $this->container->getReflectionClass($class::class);
} }
return $this->resolveMethod($class, $method, $reflection); return $this->resolveMethod($class, $method, $reflection);
} }
@@ -44,7 +53,7 @@ class ControllerInterpreter
{ {
$reflection = new \ReflectionFunction($method); $reflection = new \ReflectionFunction($method);
$params = \Kiri::getDi()->resolveMethodParams($reflection); $params = $this->container->resolveMethodParams($reflection);
return new Handler($method, $params, $reflection->getReturnType()); return new Handler($method, $params, $reflection->getReturnType());
} }
@@ -62,7 +71,7 @@ class ControllerInterpreter
public function addRouteByObject(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler public function addRouteByObject(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
{ {
if (is_null($reflection)) { if (is_null($reflection)) {
$reflection = \Kiri::getDi()->getReflectionClass($class::class); $reflection = $this->container->getReflectionClass($class::class);
} }
return $this->resolveMethod($class, $method, $reflection); return $this->resolveMethod($class, $method, $reflection);
} }
@@ -89,8 +98,7 @@ class ControllerInterpreter
throw new Exception("Return type error, cannot be multi type."); throw new Exception("Return type error, cannot be multi type.");
} }
$container = \Kiri::getDi(); $parameters = $this->container->getMethodParams($reflectionMethod);
$parameters = $container->getMethodParams($reflectionMethod);
return new Handler([$class, $reflectionMethod->getName()], $parameters, $returnType); return new Handler([$class, $reflectionMethod->getName()], $parameters, $returnType);
} }