From 1795b59b88299416b4563e8626a4503864da169a Mon Sep 17 00:00:00 2001 From: xl Date: Thu, 29 Aug 2024 12:00:10 +0800 Subject: [PATCH] eee --- src/ControllerInterpreter.php | 2 +- src/Handler.php | 37 +++++++++++++++++++++-------------- src/Router.php | 5 ++--- src/RouterCollector.php | 18 +++++++++++------ 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/ControllerInterpreter.php b/src/ControllerInterpreter.php index b5676bf..d64d868 100644 --- a/src/ControllerInterpreter.php +++ b/src/ControllerInterpreter.php @@ -79,7 +79,7 @@ class ControllerInterpreter $reflectionMethod = $reflectionClass->getMethod($reflectionMethod); } - return new Handler([$class, $reflectionMethod->getName()], $reflectionMethod); + return new Handler([$class::class, $reflectionMethod->getName()], $reflectionMethod); } } diff --git a/src/Handler.php b/src/Handler.php index d81c1d5..12d4485 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Kiri\Router; use Closure; +use Kiri; use Kiri\Router\Format\ArrayFormat; use Kiri\Router\Format\IFormat; use Kiri\Router\Format\MixedFormat; @@ -29,6 +30,9 @@ class Handler implements RequestHandlerInterface protected mixed $format; + /** + * @var array + */ protected array $methods = []; @@ -37,29 +41,30 @@ class Handler implements RequestHandlerInterface */ protected ContainerInterface $container; + + /** + * @var array + */ protected array $parameters; /** * @param array|Closure $handler * @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 ($this->parameter->getReturnType() != null) { - $this->format = $this->container->get($this->returnType($parameter)); + if ($parameter->getReturnType() != null) { + $this->format = Kiri::getDi()->get($this->returnType($parameter)); } 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 */ protected function returnType(ReflectionMethod $reflectionType): string @@ -82,7 +87,7 @@ class Handler implements RequestHandlerInterface public function setRequestMethod(string $method): void { 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 { - if (!$this->isClosure()) { + if (!$this->handler instanceof Closure) { return $this->handler[0] instanceof $interface; } return false; @@ -114,10 +119,10 @@ class Handler implements RequestHandlerInterface */ public function getClass(): ?string { - if ($this->isClosure()) { + if ($this->handler instanceof Closure) { return null; } - return $this->handler[0]::class; + return $this->handler[0]; } @@ -126,7 +131,7 @@ class Handler implements RequestHandlerInterface */ public function getMethod(): ?string { - if ($this->isClosure()) { + if ($this->handler instanceof Closure) { return null; } return $this->handler[1]; @@ -140,7 +145,9 @@ class Handler implements RequestHandlerInterface */ 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); diff --git a/src/Router.php b/src/Router.php index 9e253ca..74489da 100644 --- a/src/Router.php +++ b/src/Router.php @@ -139,10 +139,9 @@ class Router /** * @param array|RequestMethod $methods * @param string $route - * @param array|string $handler - * @throws + * @param string $handler */ - 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); if ($methods instanceof RequestMethod) { diff --git a/src/RouterCollector.php b/src/RouterCollector.php index b57f67c..968f65f 100644 --- a/src/RouterCollector.php +++ b/src/RouterCollector.php @@ -58,6 +58,12 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate protected array $httpHandler = []; + /** + * @var array + */ + protected array $controllers = []; + + /** * @var Handler */ @@ -137,11 +143,11 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate { try { $route = $this->_splicing_routing($route); - if ($closure instanceof Closure) { - $handler = $this->interpreter->addRouteByClosure($closure); - } else { - $handler = $this->resolve($closure, $this->interpreter); - } +// if ($closure instanceof Closure) { +// $handler = $this->interpreter->addRouteByClosure($closure); +// } else { +// } + $handler = $this->resolve($closure, $this->interpreter); foreach ($method as $value) { if ($value instanceof RequestMethod) { $value = $value->getString(); @@ -169,7 +175,7 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate foreach ($this->methods as $methodPath => $handler) { [$path, $method] = explode('_', $methodPath); - $controller = $handler->isClosure() ? '\Closure' : $handler->getClass() . '::' . $handler->getMethod(); + $controller = $handler instanceof Closure ? '\Closure' : $handler->getClass() . '::' . $handler->getMethod(); $array[] = [ 'path' => $path, 'method' => $method,