This commit is contained in:
xl
2024-08-29 12:00:10 +08:00
parent 6efd221585
commit 1795b59b88
4 changed files with 37 additions and 25 deletions
+1 -1
View File
@@ -79,7 +79,7 @@ class ControllerInterpreter
$reflectionMethod = $reflectionClass->getMethod($reflectionMethod); $reflectionMethod = $reflectionClass->getMethod($reflectionMethod);
} }
return new Handler([$class, $reflectionMethod->getName()], $reflectionMethod); return new Handler([$class::class, $reflectionMethod->getName()], $reflectionMethod);
} }
} }
+22 -15
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Kiri\Router; namespace Kiri\Router;
use Closure; use Closure;
use Kiri;
use Kiri\Router\Format\ArrayFormat; use Kiri\Router\Format\ArrayFormat;
use Kiri\Router\Format\IFormat; use Kiri\Router\Format\IFormat;
use Kiri\Router\Format\MixedFormat; use Kiri\Router\Format\MixedFormat;
@@ -29,6 +30,9 @@ class Handler implements RequestHandlerInterface
protected mixed $format; protected mixed $format;
/**
* @var array
*/
protected array $methods = []; protected array $methods = [];
@@ -37,29 +41,30 @@ class Handler implements RequestHandlerInterface
*/ */
protected ContainerInterface $container; protected ContainerInterface $container;
/**
* @var array
*/
protected array $parameters; protected array $parameters;
/** /**
* @param array|Closure $handler * @param array|Closure $handler
* @param ReflectionMethod|ReflectionFunction $parameter * @param ReflectionMethod|ReflectionFunction $parameter
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
public function __construct(public array|Closure $handler, public ReflectionMethod|ReflectionFunction $parameter) public function __construct(public array|Closure $handler, ReflectionMethod|ReflectionFunction $parameter)
{ {
$this->container = \Kiri::getDi(); if ($parameter->getReturnType() != null) {
if ($this->parameter->getReturnType() != null) { $this->format = Kiri::getDi()->get($this->returnType($parameter));
$this->format = $this->container->get($this->returnType($parameter));
} else { } else {
$this->format = $this->container->get(MixedFormat::class); $this->format = Kiri::getDi()->get(MixedFormat::class);
} }
$this->parameters = $this->container->getMethodParams($this->parameter); $this->parameters = Kiri::getDi()->getMethodParams($parameter);
} }
/** /**
* @param $reflectionType * @param ReflectionMethod $reflectionType
* @return string * @return string
*/ */
protected function returnType(ReflectionMethod $reflectionType): string protected function returnType(ReflectionMethod $reflectionType): string
@@ -82,7 +87,7 @@ class Handler implements RequestHandlerInterface
public function setRequestMethod(string $method): void public function setRequestMethod(string $method): void
{ {
if ($method == 'HEAD') { if ($method == 'HEAD') {
$this->format = $this->container->get(NoBody::class); $this->format = Kiri::getDi()->get(NoBody::class);
} }
} }
@@ -102,7 +107,7 @@ class Handler implements RequestHandlerInterface
*/ */
public function implement(string $interface): bool public function implement(string $interface): bool
{ {
if (!$this->isClosure()) { if (!$this->handler instanceof Closure) {
return $this->handler[0] instanceof $interface; return $this->handler[0] instanceof $interface;
} }
return false; return false;
@@ -114,10 +119,10 @@ class Handler implements RequestHandlerInterface
*/ */
public function getClass(): ?string public function getClass(): ?string
{ {
if ($this->isClosure()) { if ($this->handler instanceof Closure) {
return null; return null;
} }
return $this->handler[0]::class; return $this->handler[0];
} }
@@ -126,7 +131,7 @@ class Handler implements RequestHandlerInterface
*/ */
public function getMethod(): ?string public function getMethod(): ?string
{ {
if ($this->isClosure()) { if ($this->handler instanceof Closure) {
return null; return null;
} }
return $this->handler[1]; return $this->handler[1];
@@ -140,7 +145,9 @@ class Handler implements RequestHandlerInterface
*/ */
public function handle(ServerRequestInterface $request): ResponseInterface public function handle(ServerRequestInterface $request): ResponseInterface
{ {
$data = call_user_func($this->handler, ...$this->parameters); $controller = Kiri::getDi()->get($this->handler[0]);
$data = call_user_func([$controller, $this->handler[1]], ...$this->parameters);
/** 根据返回类型 */ /** 根据返回类型 */
return $this->format->call($data); return $this->format->call($data);
+2 -3
View File
@@ -139,10 +139,9 @@ class Router
/** /**
* @param array|RequestMethod $methods * @param array|RequestMethod $methods
* @param string $route * @param string $route
* @param array|string $handler * @param string $handler
* @throws
*/ */
public static function addRoute(array|RequestMethod $methods, string $route, array|string $handler): void public static function addRoute(array|RequestMethod $methods, string $route, string $handler): void
{ {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
if ($methods instanceof RequestMethod) { if ($methods instanceof RequestMethod) {
+12 -6
View File
@@ -58,6 +58,12 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
protected array $httpHandler = []; protected array $httpHandler = [];
/**
* @var array<string, object>
*/
protected array $controllers = [];
/** /**
* @var Handler * @var Handler
*/ */
@@ -137,11 +143,11 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
{ {
try { try {
$route = $this->_splicing_routing($route); $route = $this->_splicing_routing($route);
if ($closure instanceof Closure) { // if ($closure instanceof Closure) {
$handler = $this->interpreter->addRouteByClosure($closure); // $handler = $this->interpreter->addRouteByClosure($closure);
} else { // } else {
$handler = $this->resolve($closure, $this->interpreter); // }
} $handler = $this->resolve($closure, $this->interpreter);
foreach ($method as $value) { foreach ($method as $value) {
if ($value instanceof RequestMethod) { if ($value instanceof RequestMethod) {
$value = $value->getString(); $value = $value->getString();
@@ -169,7 +175,7 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
foreach ($this->methods as $methodPath => $handler) { foreach ($this->methods as $methodPath => $handler) {
[$path, $method] = explode('_', $methodPath); [$path, $method] = explode('_', $methodPath);
$controller = $handler->isClosure() ? '\Closure' : $handler->getClass() . '::' . $handler->getMethod(); $controller = $handler instanceof Closure ? '\Closure' : $handler->getClass() . '::' . $handler->getMethod();
$array[] = [ $array[] = [
'path' => $path, 'path' => $path,
'method' => $method, 'method' => $method,