apply php styler
Signed-off-by: lubiana <lubiana123@gmail.com>
This commit is contained in:
parent
1667fd3253
commit
8b353ec62a
14 changed files with 69 additions and 67 deletions
|
@ -13,18 +13,19 @@ use Symfony\Component\HttpFoundation\Request;
|
||||||
use function assert;
|
use function assert;
|
||||||
|
|
||||||
require_once __DIR__ . '/../vendor/autoload.php';
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
$cached = false;
|
$cached = false;
|
||||||
$container = (new ContainerBuilder)->addDefinitions([
|
$container = (new ContainerBuilder)->addDefinitions(
|
||||||
|
[
|
||||||
Finder::class => static fn (): Finder => new Finder(__DIR__, [], $cached),
|
Finder::class => static fn (): Finder => new Finder(__DIR__, [], $cached),
|
||||||
Dispatcher::class => static fn (DispatcherFactory $f): Dispatcher => $f->getDispatcher($cached),
|
Dispatcher::class => static fn (DispatcherFactory $f): Dispatcher
|
||||||
HandlerResolver::class => static fn (Dispatcher $d): HandlerResolver => new HandlerResolver($d, $cached),
|
=> $f->getDispatcher($cached),
|
||||||
])->build();
|
HandlerResolver::class => static fn (Dispatcher $d): HandlerResolver
|
||||||
|
=> new HandlerResolver($d, $cached),
|
||||||
|
],
|
||||||
|
)->build();
|
||||||
$dispatcher = $container->get(Dispatcher::class);
|
$dispatcher = $container->get(Dispatcher::class);
|
||||||
assert($dispatcher instanceof Dispatcher);
|
assert($dispatcher instanceof Dispatcher);
|
||||||
$request = new RequestEvent(Request::createFromGlobals());
|
$request = new RequestEvent(Request::createFromGlobals());
|
||||||
|
|
||||||
$dispatcher->dispatch($request);
|
$dispatcher->dispatch($request);
|
||||||
|
|
||||||
$request->response?->send();
|
$request->response?->send();
|
||||||
exit;
|
exit();
|
||||||
|
|
|
@ -10,7 +10,7 @@ use Lubian\AttributeMagic\Listener\LauschEvent;
|
||||||
final readonly class Lol
|
final readonly class Lol
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private Dispatcher $dispatcher,
|
private Dispatcher $dispatcher
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ final readonly class AsListener
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string $eventClass,
|
public string $eventClass,
|
||||||
public int $priority = 0,
|
public int $priority = 0
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,10 @@ final class Dispatcher
|
||||||
public function getSortedListenerForEvent(GenericEvent $event): array
|
public function getSortedListenerForEvent(GenericEvent $event): array
|
||||||
{
|
{
|
||||||
$filtered = $this->getListenerForEvent($event);
|
$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;
|
return $filtered;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,10 +54,8 @@ final class Dispatcher
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->invoker->call(
|
$this->invoker
|
||||||
[$listener->listenerClass, $listener->listenerMethod],
|
->call([$listener->listenerClass, $listener->listenerMethod], [$event]);
|
||||||
[$event],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ final readonly class DispatcherFactory
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private Finder $finder,
|
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)) {
|
if ($cached === true && file_exists(self::CACHE_FILE)) {
|
||||||
$data = file_get_contents(self::CACHE_FILE);
|
$data = file_get_contents(self::CACHE_FILE);
|
||||||
|
|
||||||
if ($data === false) {
|
if ($data === false) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
@ -43,6 +44,7 @@ final readonly class DispatcherFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
$listeners = $this->finder->getListeners();
|
$listeners = $this->finder->getListeners();
|
||||||
|
|
||||||
if ($cached === true) {
|
if ($cached === true) {
|
||||||
file_put_contents(self::CACHE_FILE, serialize($listeners));
|
file_put_contents(self::CACHE_FILE, serialize($listeners));
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,8 @@ final class Finder
|
||||||
{
|
{
|
||||||
$this->populateClassnames();
|
$this->populateClassnames();
|
||||||
return array_map(
|
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)),
|
iterator_to_array($this->getAttributes(AsListener::class)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -56,7 +57,8 @@ final class Finder
|
||||||
{
|
{
|
||||||
$this->populateClassnames();
|
$this->populateClassnames();
|
||||||
return array_map(
|
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)),
|
iterator_to_array($this->getAttributes(AsHandler::class)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -69,6 +71,7 @@ final class Finder
|
||||||
|
|
||||||
if ($this->cached === true && file_exists(self::CACHE_FILE)) {
|
if ($this->cached === true && file_exists(self::CACHE_FILE)) {
|
||||||
$data = file_get_contents(self::CACHE_FILE);
|
$data = file_get_contents(self::CACHE_FILE);
|
||||||
|
|
||||||
if ($data === false) {
|
if ($data === false) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -79,7 +82,10 @@ final class Finder
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->path));
|
$it = new RecursiveIteratorIterator(
|
||||||
|
new RecursiveDirectoryIterator($this->path),
|
||||||
|
);
|
||||||
|
|
||||||
/** @var SplFileInfo $file */
|
/** @var SplFileInfo $file */
|
||||||
foreach ($it as $file) {
|
foreach ($it as $file) {
|
||||||
if (! str_ends_with((string) $file, '.php')) {
|
if (! str_ends_with((string) $file, '.php')) {
|
||||||
|
@ -89,7 +95,10 @@ final class Finder
|
||||||
$classesBeforeLoad = get_declared_classes();
|
$classesBeforeLoad = get_declared_classes();
|
||||||
require_once (string) $file;
|
require_once (string) $file;
|
||||||
$classesAfterLoad = get_declared_classes();
|
$classesAfterLoad = get_declared_classes();
|
||||||
$this->classNames = [...$this->classNames, ...array_diff($classesAfterLoad, $classesBeforeLoad)];
|
$this->classNames = [
|
||||||
|
...$this->classNames,
|
||||||
|
...array_diff($classesAfterLoad, $classesBeforeLoad),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->cached === true) {
|
if ($this->cached === true) {
|
||||||
|
@ -102,18 +111,14 @@ final class Finder
|
||||||
* @param class-string<T> $attributeClass
|
* @param class-string<T> $attributeClass
|
||||||
* @return Iterator<array{T, class-string, non-empty-string}>
|
* @return Iterator<array{T, class-string, non-empty-string}>
|
||||||
*/
|
*/
|
||||||
private function getAttributes(
|
private function getAttributes(string $attributeClass): Iterator
|
||||||
string $attributeClass
|
{
|
||||||
): Iterator {
|
|
||||||
foreach ($this->classNames as $class) {
|
foreach ($this->classNames as $class) {
|
||||||
$reflectionClass = new ReflectionClass($class);
|
$reflectionClass = new ReflectionClass($class);
|
||||||
|
|
||||||
foreach ($reflectionClass->getMethods() as $method) {
|
foreach ($reflectionClass->getMethods() as $method) {
|
||||||
foreach ($method->getAttributes($attributeClass) as $attribute) {
|
foreach ($method->getAttributes($attributeClass) as $attribute) {
|
||||||
yield [
|
yield [$attribute->newInstance(), $class, $method->getName()];
|
||||||
$attribute->newInstance(),
|
|
||||||
$class,
|
|
||||||
$method->getName(),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ final readonly class AsHandler
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public HttpMethod $method,
|
public HttpMethod $method,
|
||||||
public string $path,
|
public string $path
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,5 +5,6 @@ namespace Lubian\AttributeMagic\Infrastructure\Route;
|
||||||
enum HttpMethod: string
|
enum HttpMethod: string
|
||||||
{
|
{
|
||||||
case GET = 'GET';
|
case GET = 'GET';
|
||||||
|
|
||||||
case POST = 'POST';
|
case POST = 'POST';
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ final class RequestEvent extends GenericEvent
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public Request $request,
|
public Request $request,
|
||||||
public Response|null $response = null,
|
public Response|null $response = null,
|
||||||
public Handler|null $handler = null
|
public Handler|null $handler = null,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,9 +40,6 @@ final class CachedRouteCollector
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents(
|
file_put_contents(self::ROUTES_DIR, serialize($event->routes));
|
||||||
self::ROUTES_DIR,
|
|
||||||
serialize($event->routes),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ final class CollectRoutes extends GenericEvent
|
||||||
public array $routes = [];
|
public array $routes = [];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public readonly bool $cached = false,
|
public readonly bool $cached = false
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,10 +27,12 @@ final readonly class HandlerCaller
|
||||||
}
|
}
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|
||||||
/** @var mixed $response */
|
/** @var mixed $response */
|
||||||
$response = $this->invoker->call(
|
$response = $this->invoker
|
||||||
|
->call(
|
||||||
[$event->handler->handlerClass, $event->handler->handlerMethod],
|
[$event->handler->handlerClass, $event->handler->handlerMethod],
|
||||||
$event->request->attributes->all()
|
$event->request->attributes->all(),
|
||||||
);
|
);
|
||||||
$output = (string) ob_get_clean();
|
$output = (string) ob_get_clean();
|
||||||
|
|
||||||
|
|
|
@ -61,17 +61,13 @@ final readonly class HandlerResolver
|
||||||
|
|
||||||
/** @var array<array-key, mixed> $args */
|
/** @var array<array-key, mixed> $args */
|
||||||
$args = $routeInfo[2];
|
$args = $routeInfo[2];
|
||||||
|
|
||||||
$event->request->attributes->add($args);
|
$event->request->attributes->add($args);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getRouteInfo(Request $request): array
|
private function getRouteInfo(Request $request): array
|
||||||
{
|
{
|
||||||
$dispatcher = $this->createRouteDispatcher();
|
$dispatcher = $this->createRouteDispatcher();
|
||||||
return $dispatcher->dispatch(
|
return $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());
|
||||||
$request->getMethod(),
|
|
||||||
$request->getPathInfo(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createRouteDispatcher(): \FastRoute\Dispatcher
|
private function createRouteDispatcher(): \FastRoute\Dispatcher
|
||||||
|
@ -79,19 +75,23 @@ final readonly class HandlerResolver
|
||||||
$routesEvent = new CollectRoutes($this->cached);
|
$routesEvent = new CollectRoutes($this->cached);
|
||||||
$this->dispatcher->dispatch($routesEvent);
|
$this->dispatcher->dispatch($routesEvent);
|
||||||
$dispatcher = $this->dispatcher;
|
$dispatcher = $this->dispatcher;
|
||||||
|
return cachedDispatcher(
|
||||||
return cachedDispatcher(static function (RouteCollector $r) use (
|
static function (RouteCollector $r) use ($dispatcher, $routesEvent): void {
|
||||||
$dispatcher,
|
|
||||||
$routesEvent
|
|
||||||
): void {
|
|
||||||
$dispatcher->dispatch($routesEvent);
|
$dispatcher->dispatch($routesEvent);
|
||||||
|
|
||||||
foreach ($routesEvent->routes as $h) {
|
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',
|
'cacheFile' => __DIR__ . '/../../../../var/route.cache',
|
||||||
'cacheDisabled' => ! $this->cached,
|
'cacheDisabled' => ! $this->cached,
|
||||||
]);
|
],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function notFound(RequestEvent $event): void
|
private function notFound(RequestEvent $event): void
|
||||||
|
|
|
@ -11,14 +11,7 @@ use function array_values;
|
||||||
* @param callable(mixed, mixed=):scalar $callable
|
* @param callable(mixed, mixed=):scalar $callable
|
||||||
* @return array<int, T>
|
* @return array<int, T>
|
||||||
*/
|
*/
|
||||||
function arrayFilter(
|
function arrayFilter(array $input, callable $callable): array
|
||||||
array $input,
|
{
|
||||||
callable $callable
|
return array_values(array_filter($input, $callable));
|
||||||
): array {
|
|
||||||
return array_values(
|
|
||||||
array_filter(
|
|
||||||
$input,
|
|
||||||
$callable,
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue