fix my code

This commit is contained in:
lubiana 2023-01-26 21:49:37 +01:00
parent af5b329878
commit 7cccb8f7f8
No known key found for this signature in database
3 changed files with 42 additions and 26 deletions

5
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "940ccbcb1b206dcb38091502bcc75e12", "content-hash": "79651834eb50af774229c9ce1083564d",
"packages": [ "packages": [
{ {
"name": "amphp/amp", "name": "amphp/amp",
@ -2963,7 +2963,8 @@
"prefer-stable": true, "prefer-stable": true,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": ">=8.2" "php": ">=8.2",
"ext-apcu": "*"
}, },
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.3.0" "plugin-api-version": "2.3.0"

View file

@ -4,3 +4,8 @@ parameters:
message: "#^Parameter \\#1 \\$data of function unserialize expects string, string\\|false given\\.$#" message: "#^Parameter \\#1 \\$data of function unserialize expects string, string\\|false given\\.$#"
count: 1 count: 1
path: src/Infrastructure/WebApp/Route/CachedRouteCollector.php 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

View file

@ -8,6 +8,7 @@ use Lubian\AttributeMagic\Infrastructure\Event\Dispatcher;
use Lubian\AttributeMagic\Infrastructure\Route\Handler; use Lubian\AttributeMagic\Infrastructure\Route\Handler;
use Lubian\AttributeMagic\Infrastructure\Route\HttpMethod; use Lubian\AttributeMagic\Infrastructure\Route\HttpMethod;
use Lubian\AttributeMagic\Infrastructure\WebApp\Request\RequestEvent; use Lubian\AttributeMagic\Infrastructure\WebApp\Request\RequestEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use function count; use function count;
@ -26,27 +27,7 @@ final readonly class HandlerResolver
#[AsListener(RequestEvent::class, -90)] #[AsListener(RequestEvent::class, -90)]
public function resolveHandler(RequestEvent $event): void public function resolveHandler(RequestEvent $event): void
{ {
$routesEvent = new CollectRoutes($this->cached); $routeInfo = $this->getRouteInfo($event->request);
$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(),
);
if ($routeInfo[0] === \FastRoute\Dispatcher::NOT_FOUND) { if ($routeInfo[0] === \FastRoute\Dispatcher::NOT_FOUND) {
$this->notFound($event); $this->notFound($event);
@ -60,11 +41,11 @@ final readonly class HandlerResolver
} }
if ( if (
! is_array($routeInfo[1]) is_array($routeInfo[1]) === false
|| count($routeInfo[1]) !== 2 || count($routeInfo[1]) !== 2
|| ! is_string($routeInfo[1][0]) || is_string($routeInfo[1][0]) === false
|| $routeInfo[1][0] === '' || $routeInfo[1][0] === ''
|| ! is_string($routeInfo[1][1]) || is_string($routeInfo[1][1]) === false
|| $routeInfo[1][1] === '' || $routeInfo[1][1] === ''
) { ) {
$this->notFound($event); $this->notFound($event);
@ -84,6 +65,35 @@ final readonly class HandlerResolver
$event->request->attributes->add($args); $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 private function notFound(RequestEvent $event): void
{ {
$event->response = new Response('Not Found', 404); $event->response = new Response('Not Found', 404);