From 7cccb8f7f8c5214b51b25c8e46747268d95ec73d Mon Sep 17 00:00:00 2001 From: lubiana Date: Thu, 26 Jan 2023 21:49:37 +0100 Subject: [PATCH] fix my code --- composer.lock | 5 +- phpstan-baseline.neon | 5 ++ .../WebApp/Route/HandlerResolver.php | 58 +++++++++++-------- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/composer.lock b/composer.lock index a66483c..f6da26a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "940ccbcb1b206dcb38091502bcc75e12", + "content-hash": "79651834eb50af774229c9ce1083564d", "packages": [ { "name": "amphp/amp", @@ -2963,7 +2963,8 @@ "prefer-stable": true, "prefer-lowest": false, "platform": { - "php": ">=8.2" + "php": ">=8.2", + "ext-apcu": "*" }, "platform-dev": [], "plugin-api-version": "2.3.0" diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 87e8207..f474e5c 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -4,3 +4,8 @@ parameters: message: "#^Parameter \\#1 \\$data of function unserialize expects string, string\\|false given\\.$#" count: 1 path: src/Infrastructure/WebApp/Route/CachedRouteCollector.php + + - + message: "#^Method Lubian\\\\AttributeMagic\\\\Infrastructure\\\\WebApp\\\\Route\\\\HandlerResolver\\:\\:getRouteInfo\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/Infrastructure/WebApp/Route/HandlerResolver.php diff --git a/src/Infrastructure/WebApp/Route/HandlerResolver.php b/src/Infrastructure/WebApp/Route/HandlerResolver.php index de8ca28..d8228a2 100644 --- a/src/Infrastructure/WebApp/Route/HandlerResolver.php +++ b/src/Infrastructure/WebApp/Route/HandlerResolver.php @@ -8,6 +8,7 @@ use Lubian\AttributeMagic\Infrastructure\Event\Dispatcher; use Lubian\AttributeMagic\Infrastructure\Route\Handler; use Lubian\AttributeMagic\Infrastructure\Route\HttpMethod; use Lubian\AttributeMagic\Infrastructure\WebApp\Request\RequestEvent; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use function count; @@ -26,27 +27,7 @@ final readonly class HandlerResolver #[AsListener(RequestEvent::class, -90)] public function resolveHandler(RequestEvent $event): void { - $routesEvent = new CollectRoutes($this->cached); - $this->dispatcher->dispatch($routesEvent); - $dispatcher = $this->dispatcher; - - $routeDispatcher = cachedDispatcher(static function (RouteCollector $r) use ( - $dispatcher, - $routesEvent - ): void { - $dispatcher->dispatch($routesEvent); - foreach ($routesEvent->routes as $h) { - $r->addRoute($h->method->value, $h->path, [$h->handlerClass, $h->handlerMethod]); - } - }, [ - 'cacheFile' => __DIR__ . '/../../../../var/route.cache', - 'cacheDisabled' => ! $this->cached, - ]); - - $routeInfo = $routeDispatcher->dispatch( - $event->request->getMethod(), - $event->request->getPathInfo(), - ); + $routeInfo = $this->getRouteInfo($event->request); if ($routeInfo[0] === \FastRoute\Dispatcher::NOT_FOUND) { $this->notFound($event); @@ -60,11 +41,11 @@ final readonly class HandlerResolver } if ( - ! is_array($routeInfo[1]) + is_array($routeInfo[1]) === false || count($routeInfo[1]) !== 2 - || ! is_string($routeInfo[1][0]) + || is_string($routeInfo[1][0]) === false || $routeInfo[1][0] === '' - || ! is_string($routeInfo[1][1]) + || is_string($routeInfo[1][1]) === false || $routeInfo[1][1] === '' ) { $this->notFound($event); @@ -84,6 +65,35 @@ final readonly class HandlerResolver $event->request->attributes->add($args); } + private function getRouteInfo(Request $request): array + { + $dispatcher = $this->createRouteDispatcher(); + return $dispatcher->dispatch( + $request->getMethod(), + $request->getPathInfo(), + ); + } + + private function createRouteDispatcher(): \FastRoute\Dispatcher + { + $routesEvent = new CollectRoutes($this->cached); + $this->dispatcher->dispatch($routesEvent); + $dispatcher = $this->dispatcher; + + return cachedDispatcher(static function (RouteCollector $r) use ( + $dispatcher, + $routesEvent + ): void { + $dispatcher->dispatch($routesEvent); + foreach ($routesEvent->routes as $h) { + $r->addRoute($h->method->value, $h->path, [$h->handlerClass, $h->handlerMethod]); + } + }, [ + 'cacheFile' => __DIR__ . '/../../../../var/route.cache', + 'cacheDisabled' => ! $this->cached, + ]); + } + private function notFound(RequestEvent $event): void { $event->response = new Response('Not Found', 404);