This commit is contained in:
2023-09-13 12:30:57 +08:00
parent 03c9ef26e8
commit f1d1ff1f4f
2 changed files with 147 additions and 123 deletions
+66 -67
View File
@@ -15,81 +15,80 @@ class ControllerInterpreter
{
/**
* @param object $class
* @param string|ReflectionMethod $method
* @param ReflectionClass|null $reflection
* @return Handler
* @throws ReflectionException
*/
public function addRouteByString(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
{
if (is_null($reflection)) {
$reflection = \Kiri::getDi()->getReflectionClass($class::class);
}
return $this->resolveMethod($class, $method, $reflection);
}
/**
* @param object $class
* @param string|ReflectionMethod $method
* @param ReflectionClass|null $reflection
* @return Handler
* @throws ReflectionException
*/
public function addRouteByString(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
{
if (is_null($reflection)) {
$reflection = \Kiri::getDi()->getReflectionClass($class::class);
}
return $this->resolveMethod($class, $method, $reflection);
}
/**
* @param Closure $method
* @return Handler
* @throws ReflectionException
* @throws Exception
*/
public function addRouteByClosure(Closure $method): Handler
{
$reflection = new \ReflectionFunction($method);
if ($reflection->getReturnType()->getName() !== 'Psr\Http\Message\ResponseInterface') {
die('Request Handler returns must implements on Psr\Http\Message\ResponseInterface');
}
$params = \Kiri::getDi()->resolveMethodParams($reflection);
return new Handler($method, $params);
}
/**
* @param Closure $method
* @return Handler
* @throws ReflectionException
* @throws Exception
*/
public function addRouteByClosure(Closure $method): Handler
{
$reflection = new \ReflectionFunction($method);
$params = \Kiri::getDi()->resolveMethodParams($reflection);
return new Handler($method, $params, $reflection->getReturnType());
}
/**
* @param object $class
* @param string|ReflectionMethod $method
* @param ReflectionClass|null $reflection
* @return Handler
* @throws ReflectionException
*/
public function addRouteByObject(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
{
if (is_null($reflection)) {
$reflection = \Kiri::getDi()->getReflectionClass($class::class);
}
return $this->resolveMethod($class, $method, $reflection);
}
/**
* @param object $class
* @param string|ReflectionMethod $method
* @param ReflectionClass|null $reflection
* @return Handler
* @throws ReflectionException
*/
public function addRouteByObject(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
{
if (is_null($reflection)) {
$reflection = \Kiri::getDi()->getReflectionClass($class::class);
}
return $this->resolveMethod($class, $method, $reflection);
}
/**
* @param object $class
* @param string|ReflectionMethod $reflectionMethod
* @param ReflectionClass $reflectionClass
* @return Handler
* @throws ReflectionException
* @throws Exception
*/
public function resolveMethod(object $class, string|\ReflectionMethod $reflectionMethod, ReflectionClass $reflectionClass): Handler
{
if (empty($reflectionMethod)) {
return new Handler([$class, $reflectionMethod], []);
}
if (is_string($reflectionMethod)) {
$reflectionMethod = $reflectionClass->getMethod($reflectionMethod);
}
/**
* @param object $class
* @param string|ReflectionMethod $reflectionMethod
* @param ReflectionClass $reflectionClass
* @return Handler
* @throws ReflectionException
* @throws Exception
*/
public function resolveMethod(object $class, string|\ReflectionMethod $reflectionMethod, ReflectionClass $reflectionClass): Handler
{
if (empty($reflectionMethod)) {
return new Handler([$class, $reflectionMethod], [], $reflectionMethod->getReturnType());
}
if (is_string($reflectionMethod)) {
$reflectionMethod = $reflectionClass->getMethod($reflectionMethod);
}
$returnType = $reflectionMethod->getReturnType();
if (method_exists($returnType, 'getName') && $returnType->getName() !== 'Psr\Http\Message\ResponseInterface') {
die('Request Handler<' . $class::class . '::' . $reflectionMethod->getName() . '> returns must implements on Psr\Http\Message\ResponseInterface');
}
$returnType = $reflectionMethod->getReturnType();
if (method_exists($returnType, 'getName') && $returnType->getName() !== 'Psr\Http\Message\ResponseInterface') {
die('Request Handler<' . $class::class . '::' . $reflectionMethod->getName() . '> returns must implements on Psr\Http\Message\ResponseInterface');
}
$container = \Kiri::getDi();
$parameters = $container->getMethodParams($reflectionMethod);
$container = \Kiri::getDi();
$parameters = $container->getMethodParams($reflectionMethod);
return new Handler([$class, $reflectionMethod->getName()], $parameters);
}
return new Handler([$class, $reflectionMethod->getName()], $parameters, $reflectionMethod->getReturnType());
}
}