eee
This commit is contained in:
@@ -15,81 +15,80 @@ class ControllerInterpreter
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param object $class
|
* @param object $class
|
||||||
* @param string|ReflectionMethod $method
|
* @param string|ReflectionMethod $method
|
||||||
* @param ReflectionClass|null $reflection
|
* @param ReflectionClass|null $reflection
|
||||||
* @return Handler
|
* @return Handler
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
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 = \Kiri::getDi()->getReflectionClass($class::class);
|
||||||
}
|
}
|
||||||
return $this->resolveMethod($class, $method, $reflection);
|
return $this->resolveMethod($class, $method, $reflection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Closure $method
|
* @param Closure $method
|
||||||
* @return Handler
|
* @return Handler
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function addRouteByClosure(Closure $method): Handler
|
public function addRouteByClosure(Closure $method): Handler
|
||||||
{
|
{
|
||||||
$reflection = new \ReflectionFunction($method);
|
$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);
|
||||||
}
|
|
||||||
$params = \Kiri::getDi()->resolveMethodParams($reflection);
|
return new Handler($method, $params, $reflection->getReturnType());
|
||||||
return new Handler($method, $params);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param object $class
|
* @param object $class
|
||||||
* @param string|ReflectionMethod $method
|
* @param string|ReflectionMethod $method
|
||||||
* @param ReflectionClass|null $reflection
|
* @param ReflectionClass|null $reflection
|
||||||
* @return Handler
|
* @return Handler
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
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 = \Kiri::getDi()->getReflectionClass($class::class);
|
||||||
}
|
}
|
||||||
return $this->resolveMethod($class, $method, $reflection);
|
return $this->resolveMethod($class, $method, $reflection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param object $class
|
* @param object $class
|
||||||
* @param string|ReflectionMethod $reflectionMethod
|
* @param string|ReflectionMethod $reflectionMethod
|
||||||
* @param ReflectionClass $reflectionClass
|
* @param ReflectionClass $reflectionClass
|
||||||
* @return Handler
|
* @return Handler
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function resolveMethod(object $class, string|\ReflectionMethod $reflectionMethod, ReflectionClass $reflectionClass): Handler
|
public function resolveMethod(object $class, string|\ReflectionMethod $reflectionMethod, ReflectionClass $reflectionClass): Handler
|
||||||
{
|
{
|
||||||
if (empty($reflectionMethod)) {
|
if (empty($reflectionMethod)) {
|
||||||
return new Handler([$class, $reflectionMethod], []);
|
return new Handler([$class, $reflectionMethod], [], $reflectionMethod->getReturnType());
|
||||||
}
|
}
|
||||||
if (is_string($reflectionMethod)) {
|
if (is_string($reflectionMethod)) {
|
||||||
$reflectionMethod = $reflectionClass->getMethod($reflectionMethod);
|
$reflectionMethod = $reflectionClass->getMethod($reflectionMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
$returnType = $reflectionMethod->getReturnType();
|
$returnType = $reflectionMethod->getReturnType();
|
||||||
if (method_exists($returnType, 'getName') && $returnType->getName() !== 'Psr\Http\Message\ResponseInterface') {
|
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');
|
die('Request Handler<' . $class::class . '::' . $reflectionMethod->getName() . '> returns must implements on Psr\Http\Message\ResponseInterface');
|
||||||
}
|
}
|
||||||
|
|
||||||
$container = \Kiri::getDi();
|
$container = \Kiri::getDi();
|
||||||
$parameters = $container->getMethodParams($reflectionMethod);
|
$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;
|
namespace Kiri\Router;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
|
use Kiri\Router\Constrict\Stream;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
|
use ReflectionNamedType;
|
||||||
|
use ReflectionUnionType;
|
||||||
|
|
||||||
class Handler implements RequestHandlerInterface
|
class Handler implements RequestHandlerInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|Closure $handler
|
* @param array|Closure $handler
|
||||||
* @param array $parameter
|
* @param array $parameter
|
||||||
* @throws
|
* @param ReflectionNamedType|ReflectionUnionType|null $responseType
|
||||||
*/
|
*/
|
||||||
public function __construct(public array|Closure $handler, public array $parameter)
|
public function __construct(public array|Closure $handler, public array $parameter, public ReflectionNamedType|ReflectionUnionType|null $responseType)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isClosure(): bool
|
public function isClosure(): bool
|
||||||
{
|
{
|
||||||
return $this->handler instanceof Closure;
|
return $this->handler instanceof Closure;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $interface
|
* @param string $interface
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function implement(string $interface): bool
|
public function implement(string $interface): bool
|
||||||
{
|
{
|
||||||
if (!$this->isClosure()) {
|
if (!$this->isClosure()) {
|
||||||
return $this->handler[0] instanceof $interface;
|
return $this->handler[0] instanceof $interface;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getClass(): ?string
|
public function getClass(): ?string
|
||||||
{
|
{
|
||||||
if ($this->isClosure()) {
|
if ($this->isClosure()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return $this->handler[0]::class;
|
return $this->handler[0]::class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getMethod(): ?string
|
public function getMethod(): ?string
|
||||||
{
|
{
|
||||||
if ($this->isClosure()) {
|
if ($this->isClosure()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return $this->handler[1];
|
return $this->handler[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ServerRequestInterface $request
|
* @param ServerRequestInterface $request
|
||||||
* @return ResponseInterface
|
* @return ResponseInterface
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||||
{
|
{
|
||||||
// TODO: Implement handle() method.
|
// TODO: Implement handle() method.
|
||||||
return call_user_func($this->handler, ...$this->parameter);
|
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