Files
kiri-router/src/ControllerInterpreter.php
T

104 lines
2.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-19 12:35:39 +08:00
use Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
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-16 23:19:36 +08:00
return $this->resolveMethod($class, $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-19 12:35:39 +08:00
* @throws Exception
2023-04-15 23:29:27 +08:00
*/
2023-04-15 23:31:16 +08:00
public function addRouteByClosure(Closure $method): Handler
2023-04-15 23:29:27 +08:00
{
2023-04-19 12:35:39 +08:00
$reflection = new \ReflectionFunction($method);
if ($reflection->getReturnType()->getName() !== 'Psr\Http\Message\ResponseInterface') {
throw new Exception('Request Handler returns must implements on Psr\Http\Message\ResponseInterface');
}
$params = \Kiri::getDi()->resolveMethodParams($reflection);
return new Handler($method, $params);
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-16 23:19:36 +08:00
return $this->resolveMethod($class, $method, $reflection);
2023-04-15 23:29:27 +08:00
}
/**
2023-04-16 23:19:36 +08:00
* @param object $class
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-19 12:35:39 +08:00
* @throws Exception
2023-04-15 23:29:27 +08:00
*/
2023-04-16 23:19:36 +08:00
public function resolveMethod(object $class, 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-19 12:35:39 +08:00
if ($reflectionMethod->getReturnType()->getName() !== 'Psr\Http\Message\ResponseInterface') {
throw new Exception('Request Handler returns must implements on Psr\Http\Message\ResponseInterface');
}
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-19 12:35:39 +08:00
$method = $reflectionMethod->getName();
2023-04-19 12:36:11 +08:00
/** @var ResponseInterface $response */
$response = \Kiri::service()->get('response');
$call = static function (RequestInterface $request) use ($response, $class, $method, $parameters) {
2023-04-19 12:35:39 +08:00
if (!$class->beforeAction($request)) {
return $response->withStatus(500);
}
$response = call_user_func([$class, $method], $parameters);
$class->afterAction($response);
return $response;
};
2023-04-19 12:37:58 +08:00
return new Handler($call, [\Kiri::service()->get('request')]);
2023-04-15 23:29:27 +08:00
}
}