eee
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+81
-56
@@ -4,79 +4,104 @@ declare(strict_types=1);
|
||||
namespace Kiri\Router;
|
||||
|
||||
use Closure;
|
||||
use Kiri\Router\Constrict\Stream;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use ReflectionException;
|
||||
use ReflectionNamedType;
|
||||
use ReflectionUnionType;
|
||||
|
||||
class Handler implements RequestHandlerInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array|Closure $handler
|
||||
* @param array $parameter
|
||||
* @throws
|
||||
*/
|
||||
public function __construct(public array|Closure $handler, public array $parameter)
|
||||
{
|
||||
}
|
||||
/**
|
||||
* @param array|Closure $handler
|
||||
* @param array $parameter
|
||||
* @param ReflectionNamedType|ReflectionUnionType|null $responseType
|
||||
*/
|
||||
public function __construct(public array|Closure $handler, public array $parameter, public ReflectionNamedType|ReflectionUnionType|null $responseType)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isClosure(): bool
|
||||
{
|
||||
return $this->handler instanceof Closure;
|
||||
}
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isClosure(): bool
|
||||
{
|
||||
return $this->handler instanceof Closure;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $interface
|
||||
* @return bool
|
||||
*/
|
||||
public function implement(string $interface): bool
|
||||
{
|
||||
if (!$this->isClosure()) {
|
||||
return $this->handler[0] instanceof $interface;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @param string $interface
|
||||
* @return bool
|
||||
*/
|
||||
public function implement(string $interface): bool
|
||||
{
|
||||
if (!$this->isClosure()) {
|
||||
return $this->handler[0] instanceof $interface;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getClass(): ?string
|
||||
{
|
||||
if ($this->isClosure()) {
|
||||
return null;
|
||||
}
|
||||
return $this->handler[0]::class;
|
||||
}
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getClass(): ?string
|
||||
{
|
||||
if ($this->isClosure()) {
|
||||
return null;
|
||||
}
|
||||
return $this->handler[0]::class;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getMethod(): ?string
|
||||
{
|
||||
if ($this->isClosure()) {
|
||||
return null;
|
||||
}
|
||||
return $this->handler[1];
|
||||
}
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getMethod(): ?string
|
||||
{
|
||||
if ($this->isClosure()) {
|
||||
return null;
|
||||
}
|
||||
return $this->handler[1];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
// TODO: Implement handle() method.
|
||||
return call_user_func($this->handler, ...$this->parameter);
|
||||
}
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
// TODO: Implement handle() method.
|
||||
if ($this->responseType->getName() !== 'void') {
|
||||
return $this->typeEncode();
|
||||
}
|
||||
call_user_func($this->handler, ...$this->parameter);
|
||||
return response();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
protected function typeEncode(): ResponseInterface
|
||||
{
|
||||
$result = call_user_func($this->handler, ...$this->parameter);
|
||||
if ($result instanceof ResponseInterface) {
|
||||
return $result;
|
||||
}
|
||||
if (is_object($result)) {
|
||||
$result = '[object]';
|
||||
} else if (is_array($result)) {
|
||||
$result = json_encode($result, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
return response()->withBody(new Stream($result));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user