apply php styler

Signed-off-by: lubiana <lubiana123@gmail.com>
This commit is contained in:
lubiana 2023-09-26 18:17:46 +02:00 committed by lubiana
parent 1667fd3253
commit 8b353ec62a
No known key found for this signature in database
14 changed files with 69 additions and 67 deletions

View file

@ -13,18 +13,19 @@ use Symfony\Component\HttpFoundation\Request;
use function assert;
require_once __DIR__ . '/../vendor/autoload.php';
$cached = false;
$container = (new ContainerBuilder)->addDefinitions([
$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::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();

View file

@ -10,7 +10,7 @@ use Lubian\AttributeMagic\Listener\LauschEvent;
final readonly class Lol
{
public function __construct(
private Dispatcher $dispatcher,
private Dispatcher $dispatcher
) {
}

View file

@ -13,7 +13,7 @@ final readonly class AsListener
*/
public function __construct(
public string $eventClass,
public int $priority = 0,
public int $priority = 0
) {
}
}

View file

@ -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]);
}
}
}

View file

@ -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));
}

View file

@ -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<T> $attributeClass
* @return Iterator<array{T, class-string, non-empty-string}>
*/
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()];
}
}
}

View file

@ -12,7 +12,7 @@ final readonly class AsHandler
*/
public function __construct(
public HttpMethod $method,
public string $path,
public string $path
) {
}
}

View file

@ -5,5 +5,6 @@ namespace Lubian\AttributeMagic\Infrastructure\Route;
enum HttpMethod: string
{
case GET = 'GET';
case POST = 'POST';
}

View file

@ -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,
) {
}
}

View file

@ -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));
}
}

View file

@ -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
) {
}
}

View file

@ -27,10 +27,12 @@ final readonly class HandlerCaller
}
ob_start();
/** @var mixed $response */
$response = $this->invoker->call(
$response = $this->invoker
->call(
[$event->handler->handlerClass, $event->handler->handlerMethod],
$event->request->attributes->all()
$event->request->attributes->all(),
);
$output = (string) ob_get_clean();

View file

@ -61,17 +61,13 @@ final readonly class HandlerResolver
/** @var array<array-key, mixed> $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 {
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]);
$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

View file

@ -11,14 +11,7 @@ use function array_values;
* @param callable(mixed, mixed=):scalar $callable
* @return array<int, T>
*/
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));
}