eee
This commit is contained in:
@@ -46,9 +46,7 @@ class ControllerInterpreter
|
|||||||
{
|
{
|
||||||
$reflection = new \ReflectionFunction($method);
|
$reflection = new \ReflectionFunction($method);
|
||||||
|
|
||||||
$params = $this->container->resolveMethodParams($reflection);
|
return new Handler($method, $reflection);
|
||||||
|
|
||||||
return new Handler($method, $params, $reflection->getReturnType());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -81,14 +79,7 @@ class ControllerInterpreter
|
|||||||
$reflectionMethod = $reflectionClass->getMethod($reflectionMethod);
|
$reflectionMethod = $reflectionClass->getMethod($reflectionMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
$returnType = $reflectionMethod->getReturnType();
|
return new Handler([$class, $reflectionMethod->getName()], $reflectionMethod);
|
||||||
if ($returnType instanceof \ReflectionUnionType) {
|
|
||||||
throw new Exception("Return type error, cannot be multi type.");
|
|
||||||
}
|
|
||||||
|
|
||||||
$parameters = $this->container->getMethodParams($reflectionMethod);
|
|
||||||
|
|
||||||
return new Handler([$class, $reflectionMethod->getName()], $parameters, $returnType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-8
@@ -11,10 +11,13 @@ use Kiri\Router\Format\NoBody;
|
|||||||
use Kiri\Router\Format\OtherFormat;
|
use Kiri\Router\Format\OtherFormat;
|
||||||
use Kiri\Router\Format\ResponseFormat;
|
use Kiri\Router\Format\ResponseFormat;
|
||||||
use Kiri\Router\Format\VoidFormat;
|
use Kiri\Router\Format\VoidFormat;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
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 ReflectionMethod;
|
||||||
use ReflectionNamedType;
|
use ReflectionNamedType;
|
||||||
|
|
||||||
class Handler implements RequestHandlerInterface
|
class Handler implements RequestHandlerInterface
|
||||||
@@ -37,15 +40,14 @@ class Handler implements RequestHandlerInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|Closure $handler
|
* @param array|Closure $handler
|
||||||
* @param array $parameter
|
* @param ReflectionMethod $parameter
|
||||||
* @param ReflectionNamedType|null $reflectionType
|
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function __construct(public array|Closure $handler, public array $parameter, public ?ReflectionNamedType $reflectionType)
|
public function __construct(public array|Closure $handler, public ReflectionMethod|\ReflectionFunction $parameter)
|
||||||
{
|
{
|
||||||
$this->container = \Kiri::getDi();
|
$this->container = \Kiri::getDi();
|
||||||
if ($this->reflectionType != null) {
|
if ($this->parameter->getReturnType() != null) {
|
||||||
$this->format = $this->container->get($this->returnType());
|
$this->format = $this->container->get($this->returnType($parameter));
|
||||||
} else {
|
} else {
|
||||||
$this->format = $this->container->get(MixedFormat::class);
|
$this->format = $this->container->get(MixedFormat::class);
|
||||||
}
|
}
|
||||||
@@ -53,11 +55,12 @@ class Handler implements RequestHandlerInterface
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param $reflectionType
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function returnType(): string
|
protected function returnType($reflectionType): string
|
||||||
{
|
{
|
||||||
return match ($this->reflectionType->getName()) {
|
return match ($reflectionType->getName()) {
|
||||||
'array' => ArrayFormat::class,
|
'array' => ArrayFormat::class,
|
||||||
'mixed', 'object' => MixedFormat::class,
|
'mixed', 'object' => MixedFormat::class,
|
||||||
'int', 'string', 'bool' => OtherFormat::class,
|
'int', 'string', 'bool' => OtherFormat::class,
|
||||||
@@ -133,7 +136,9 @@ class Handler implements RequestHandlerInterface
|
|||||||
*/
|
*/
|
||||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||||
{
|
{
|
||||||
$data = call_user_func($this->handler, ...$this->parameter);
|
$parameter = $this->container->getMethodParams($this->parameter);
|
||||||
|
|
||||||
|
$data = call_user_func($this->handler, ...$parameter);
|
||||||
|
|
||||||
/** 根据返回类型 */
|
/** 根据返回类型 */
|
||||||
return $this->format->call($data);
|
return $this->format->call($data);
|
||||||
|
|||||||
+1
-1
@@ -176,7 +176,7 @@ class Router
|
|||||||
{
|
{
|
||||||
$container = Kiri::getDi();
|
$container = Kiri::getDi();
|
||||||
$scanner = $container->get(Kiri\Di\Scanner::class);
|
$scanner = $container->get(Kiri\Di\Scanner::class);
|
||||||
$scanner->load_directory(APP_PATH . 'app/');
|
$scanner->load_directory(APP_PATH . 'app/Controller');
|
||||||
|
|
||||||
$this->read_dir_file(APP_PATH . 'routes');
|
$this->read_dir_file(APP_PATH . 'routes');
|
||||||
$this->reset($container);
|
$this->reset($container);
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
|
|||||||
$found = di(NotFoundController::class);
|
$found = di(NotFoundController::class);
|
||||||
|
|
||||||
$reflection = new ReflectionMethod($found, 'fail');
|
$reflection = new ReflectionMethod($found, 'fail');
|
||||||
$this->found = new Handler([$found, 'fail'], [], $reflection->getReturnType());
|
$this->found = new Handler([$found, 'fail'], $reflection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user