fix my code
This commit is contained in:
parent
af5b329878
commit
7cccb8f7f8
3 changed files with 42 additions and 26 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue