diff --git a/src/Bootstrap.php b/src/Bootstrap.php index e8d10d3..5045350 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -13,18 +13,19 @@ use Symfony\Component\HttpFoundation\Request; use function assert; require_once __DIR__ . '/../vendor/autoload.php'; - $cached = false; -$container = (new ContainerBuilder)->addDefinitions([ - Finder::class => static fn (): Finder => new Finder(__DIR__, [], $cached), - Dispatcher::class => static fn (DispatcherFactory $f): Dispatcher => $f->getDispatcher($cached), - HandlerResolver::class => static fn (Dispatcher $d): HandlerResolver => new HandlerResolver($d, $cached), -])->build(); +$container = (new ContainerBuilder)->addDefinitions( + [ + Finder::class => static fn (): Finder => new Finder(__DIR__, [], $cached), + Dispatcher::class => static fn (DispatcherFactory $f): Dispatcher + => $f->getDispatcher($cached), + HandlerResolver::class => static fn (Dispatcher $d): HandlerResolver + => new HandlerResolver($d, $cached), + ], +)->build(); $dispatcher = $container->get(Dispatcher::class); assert($dispatcher instanceof Dispatcher); $request = new RequestEvent(Request::createFromGlobals()); - $dispatcher->dispatch($request); - $request->response?->send(); -exit; +exit(); diff --git a/src/Handler/Lol.php b/src/Handler/Lol.php index a9dc7ae..9fe9a4d 100644 --- a/src/Handler/Lol.php +++ b/src/Handler/Lol.php @@ -10,7 +10,7 @@ use Lubian\AttributeMagic\Listener\LauschEvent; final readonly class Lol { public function __construct( - private Dispatcher $dispatcher, + private Dispatcher $dispatcher ) { } diff --git a/src/Infrastructure/Event/AsListener.php b/src/Infrastructure/Event/AsListener.php index 8d372da..0f04636 100644 --- a/src/Infrastructure/Event/AsListener.php +++ b/src/Infrastructure/Event/AsListener.php @@ -13,7 +13,7 @@ final readonly class AsListener */ public function __construct( public string $eventClass, - public int $priority = 0, + public int $priority = 0 ) { } } diff --git a/src/Infrastructure/Event/Dispatcher.php b/src/Infrastructure/Event/Dispatcher.php index 1a186e6..c2ebef8 100644 --- a/src/Infrastructure/Event/Dispatcher.php +++ b/src/Infrastructure/Event/Dispatcher.php @@ -40,7 +40,10 @@ final class Dispatcher public function getSortedListenerForEvent(GenericEvent $event): array { $filtered = $this->getListenerForEvent($event); - usort($filtered, static fn (Listener $a, Listener $b): int => $a->priority <=> $b->priority); + usort( + $filtered, + static fn (Listener $a, Listener $b): int => $a->priority <=> $b->priority, + ); return $filtered; } @@ -51,10 +54,8 @@ final class Dispatcher return; } - $this->invoker->call( - [$listener->listenerClass, $listener->listenerMethod], - [$event], - ); + $this->invoker + ->call([$listener->listenerClass, $listener->listenerMethod], [$event]); } } } diff --git a/src/Infrastructure/Event/DispatcherFactory.php b/src/Infrastructure/Event/DispatcherFactory.php index 3dcac19..b7b8062 100644 --- a/src/Infrastructure/Event/DispatcherFactory.php +++ b/src/Infrastructure/Event/DispatcherFactory.php @@ -17,7 +17,7 @@ final readonly class DispatcherFactory public function __construct( private Finder $finder, - private InvokerInterface $invoker + private InvokerInterface $invoker, ) { } @@ -33,6 +33,7 @@ final readonly class DispatcherFactory { if ($cached === true && file_exists(self::CACHE_FILE)) { $data = file_get_contents(self::CACHE_FILE); + if ($data === false) { return []; } @@ -43,6 +44,7 @@ final readonly class DispatcherFactory } $listeners = $this->finder->getListeners(); + if ($cached === true) { file_put_contents(self::CACHE_FILE, serialize($listeners)); } diff --git a/src/Infrastructure/Finder.php b/src/Infrastructure/Finder.php index a752e81..f7ad532 100644 --- a/src/Infrastructure/Finder.php +++ b/src/Infrastructure/Finder.php @@ -44,7 +44,8 @@ final class Finder { $this->populateClassnames(); return array_map( - static fn (array $h): Listener => new Listener($h[0]->eventClass, $h[0]->priority, $h[1], $h[2]), + static fn (array $h): Listener + => new Listener($h[0]->eventClass, $h[0]->priority, $h[1], $h[2]), iterator_to_array($this->getAttributes(AsListener::class)), ); } @@ -56,7 +57,8 @@ final class Finder { $this->populateClassnames(); return array_map( - static fn (array $h): Handler => new Handler($h[0]->method, $h[0]->path, $h[1], $h[2]), + static fn (array $h): Handler + => new Handler($h[0]->method, $h[0]->path, $h[1], $h[2]), iterator_to_array($this->getAttributes(AsHandler::class)), ); } @@ -69,6 +71,7 @@ final class Finder if ($this->cached === true && file_exists(self::CACHE_FILE)) { $data = file_get_contents(self::CACHE_FILE); + if ($data === false) { return; } @@ -79,7 +82,10 @@ final class Finder return; } - $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->path)); + $it = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($this->path), + ); + /** @var SplFileInfo $file */ foreach ($it as $file) { if (! str_ends_with((string) $file, '.php')) { @@ -89,7 +95,10 @@ final class Finder $classesBeforeLoad = get_declared_classes(); require_once (string) $file; $classesAfterLoad = get_declared_classes(); - $this->classNames = [...$this->classNames, ...array_diff($classesAfterLoad, $classesBeforeLoad)]; + $this->classNames = [ + ...$this->classNames, + ...array_diff($classesAfterLoad, $classesBeforeLoad), + ]; } if ($this->cached === true) { @@ -102,18 +111,14 @@ final class Finder * @param class-string $attributeClass * @return Iterator */ - private function getAttributes( - string $attributeClass - ): Iterator { + private function getAttributes(string $attributeClass): Iterator + { foreach ($this->classNames as $class) { $reflectionClass = new ReflectionClass($class); + foreach ($reflectionClass->getMethods() as $method) { foreach ($method->getAttributes($attributeClass) as $attribute) { - yield [ - $attribute->newInstance(), - $class, - $method->getName(), - ]; + yield [$attribute->newInstance(), $class, $method->getName()]; } } } diff --git a/src/Infrastructure/Route/AsHandler.php b/src/Infrastructure/Route/AsHandler.php index d0f2723..0e42d7f 100644 --- a/src/Infrastructure/Route/AsHandler.php +++ b/src/Infrastructure/Route/AsHandler.php @@ -12,7 +12,7 @@ final readonly class AsHandler */ public function __construct( public HttpMethod $method, - public string $path, + public string $path ) { } } diff --git a/src/Infrastructure/Route/HttpMethod.php b/src/Infrastructure/Route/HttpMethod.php index 1e575b3..4ed0769 100644 --- a/src/Infrastructure/Route/HttpMethod.php +++ b/src/Infrastructure/Route/HttpMethod.php @@ -5,5 +5,6 @@ namespace Lubian\AttributeMagic\Infrastructure\Route; enum HttpMethod: string { case GET = 'GET'; + case POST = 'POST'; } diff --git a/src/Infrastructure/WebApp/Request/RequestEvent.php b/src/Infrastructure/WebApp/Request/RequestEvent.php index 2a0e954..592359e 100644 --- a/src/Infrastructure/WebApp/Request/RequestEvent.php +++ b/src/Infrastructure/WebApp/Request/RequestEvent.php @@ -12,7 +12,7 @@ final class RequestEvent extends GenericEvent public function __construct( public Request $request, public Response|null $response = null, - public Handler|null $handler = null + public Handler|null $handler = null, ) { } } diff --git a/src/Infrastructure/WebApp/Route/CachedRouteCollector.php b/src/Infrastructure/WebApp/Route/CachedRouteCollector.php index 4055844..f5201b9 100644 --- a/src/Infrastructure/WebApp/Route/CachedRouteCollector.php +++ b/src/Infrastructure/WebApp/Route/CachedRouteCollector.php @@ -40,9 +40,6 @@ final class CachedRouteCollector return; } - file_put_contents( - self::ROUTES_DIR, - serialize($event->routes), - ); + file_put_contents(self::ROUTES_DIR, serialize($event->routes)); } } diff --git a/src/Infrastructure/WebApp/Route/CollectRoutes.php b/src/Infrastructure/WebApp/Route/CollectRoutes.php index 9ed4f7b..15b8c13 100644 --- a/src/Infrastructure/WebApp/Route/CollectRoutes.php +++ b/src/Infrastructure/WebApp/Route/CollectRoutes.php @@ -13,7 +13,7 @@ final class CollectRoutes extends GenericEvent public array $routes = []; public function __construct( - public readonly bool $cached = false, + public readonly bool $cached = false ) { } } diff --git a/src/Infrastructure/WebApp/Route/HandlerCaller.php b/src/Infrastructure/WebApp/Route/HandlerCaller.php index 4ecaa9a..88c20fe 100644 --- a/src/Infrastructure/WebApp/Route/HandlerCaller.php +++ b/src/Infrastructure/WebApp/Route/HandlerCaller.php @@ -27,11 +27,13 @@ final readonly class HandlerCaller } ob_start(); + /** @var mixed $response */ - $response = $this->invoker->call( - [$event->handler->handlerClass, $event->handler->handlerMethod], - $event->request->attributes->all() - ); + $response = $this->invoker + ->call( + [$event->handler->handlerClass, $event->handler->handlerMethod], + $event->request->attributes->all(), + ); $output = (string) ob_get_clean(); if ($response instanceof Response) { diff --git a/src/Infrastructure/WebApp/Route/HandlerResolver.php b/src/Infrastructure/WebApp/Route/HandlerResolver.php index d8228a2..8638ef5 100644 --- a/src/Infrastructure/WebApp/Route/HandlerResolver.php +++ b/src/Infrastructure/WebApp/Route/HandlerResolver.php @@ -61,17 +61,13 @@ final readonly class HandlerResolver /** @var array $args */ $args = $routeInfo[2]; - $event->request->attributes->add($args); } private function getRouteInfo(Request $request): array { $dispatcher = $this->createRouteDispatcher(); - return $dispatcher->dispatch( - $request->getMethod(), - $request->getPathInfo(), - ); + return $dispatcher->dispatch($request->getMethod(), $request->getPathInfo()); } private function createRouteDispatcher(): \FastRoute\Dispatcher @@ -79,19 +75,23 @@ final readonly class HandlerResolver $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); - 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, - ]); + 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 diff --git a/src/Infrastructure/functions.php b/src/Infrastructure/functions.php index ea2e694..114513f 100644 --- a/src/Infrastructure/functions.php +++ b/src/Infrastructure/functions.php @@ -11,14 +11,7 @@ use function array_values; * @param callable(mixed, mixed=):scalar $callable * @return array */ -function arrayFilter( - array $input, - callable $callable -): array { - return array_values( - array_filter( - $input, - $callable, - ) - ); +function arrayFilter(array $input, callable $callable): array +{ + return array_values(array_filter($input, $callable)); }