diff --git a/meta/definitions/event.php b/meta/definitions/event.php index fa8cb15..581e838 100644 --- a/meta/definitions/event.php +++ b/meta/definitions/event.php @@ -1,17 +1,77 @@ autowire(Event\EventManager::class), Event\EventMiddleware::class => function($c) { $mw = new Event\EventMiddleware($c, $c->get(Event\EventManager::class)); + $mw->fromAttributes($c->get(Notes\Event\EventFetcher::class)); + $mw->fromDefinition($c->get(Event\EventDefinition::class)); - return $mw->fromAttributes($c->get(Notes\Event\EventFetcher::class)); + return $mw; + }, + + Event\EventDefinition::class => function($c) { + return new Event\EventDefinition([ + new class() implements RoutingCompileRoutes { + public function execute(Routing $routing, Route|\Notes\Route\Annotation\Method\Route $attribute) : void + { + if (null !== ($name = $attribute->name ?? null)) { + $routing->extension->registerRoute($name, $attribute->getRoute(), $attribute->class, $attribute->classMethod, $attribute->methods ?? (array)$attribute->method); + } + } + }, + + new class() implements RoutingMapRoutes { + public function execute(Routing $routing, ContainerInterface $container, ServerRequestInterface & $request, Route|\Notes\Route\Annotation\Method\Route $attribute) : void + { + $class = $attribute->class; + $method = $attribute->classMethod; + + $request = $request->withAttribute('lean.route', $attribute); + + # Checking if user needs to be logged + if ( $container->has(SecurityHandler::class) ){ + if ( $redirect = $container->get(SecurityHandler::class)->verify($class, $method) ) { + if ( empty($object->user) || ! $object->user->logged ) { + $routing->session->set('redirectedFrom', (string) $request->getUri()); + $routing->response = $redirect; + + return; + } + } + + if ( $container->has(Taxus::class) ) { + if ( $forbidden = $container->get(SecurityHandler::class)->taxus($class, $method, $object->user ?? null) ) { + $routing->response = $forbidden; + + return; + } + } + } + + if ($container->has(Picea::class)) { + $container->get(Picea::class)->globalVariables['route'] = $attribute; + } + + if ($container->has(Session::class)) { + $container->get(Session::class)->set("lean.route", $attribute); + } + } + }, + ]); }, Notes\Event\EventFetcher::class => function($c) { diff --git a/skeleton/meta/definitions/env/prod.php b/skeleton/meta/definitions/env/prod.php index a4b653e..fea6aa1 100644 --- a/skeleton/meta/definitions/env/prod.php +++ b/skeleton/meta/definitions/env/prod.php @@ -2,11 +2,8 @@ use Picea\Picea; -#use CSLSJ\Debogueur\DebogueurMiddleware; use Negundo\Client\{ NegundoMiddleware, SoftwareConfig }; -use - use Laminas\Diactoros\Response\HtmlResponse; use Psr\Http\Server\MiddlewareInterface, diff --git a/skeleton/src/Kernel.php b/skeleton/src/Kernel.php index adff807..7bfccd4 100644 --- a/skeleton/src/Kernel.php +++ b/skeleton/src/Kernel.php @@ -29,8 +29,6 @@ new class(dirname(__DIR__)) extends \Lean\Kernel { protected function serviceContainer() : self { - # $this->container->get(ConnectionAdapter::class); - return parent::serviceContainer(); } diff --git a/skeleton/var/tmp/.gitkeep b/skeleton/var/tmp/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/ControllerTrait.php b/src/ControllerTrait.php index fdca39b..9f866c6 100644 --- a/src/ControllerTrait.php +++ b/src/ControllerTrait.php @@ -92,7 +92,7 @@ trait ControllerTrait { return new HtmlResponse($html, $code, $headers); } - public static function renderJson(/*\JsonSerializable|array*/ $data, int $code = 200, array $headers = []) : ResponseInterface + public static function renderJson(mixed $data, int $code = 200, array $headers = []) : ResponseInterface { return new JsonResponse($data, $code, $headers); } diff --git a/src/Event/RoutingCompileRoutes.php b/src/Event/RoutingCompileRoutes.php new file mode 100644 index 0000000..5eda957 --- /dev/null +++ b/src/Event/RoutingCompileRoutes.php @@ -0,0 +1,10 @@ +session = $session; - $this->cookie = $cookie; - $this->extension = $extension; - $this->fetcher = $routeFetcher; - $this->security = $security; - $this->language = $language; - $this->router = $router; - $this->taxus = $taxus; - } + public Session $session, + public Cookie $cookie, + public UrlExtension $extension, + public Router $router, + public RouteFetcher $fetcher, + public SecurityHandler $security, + public LanguageHandler $language, + public Taxus $taxus, + public EventManager $eventManager, + ) { } public function registerRoute(ContainerInterface $container, string $urlBase) { $this->router->group(rtrim($urlBase, "/"), function (RouteGroup $route) use ($container) { - foreach($this->fetcher->compile() as $annotation) { - # Register routes to the UrlExtension from picea (handling url, route and asset extensions) - if ( null !== ( $name = $annotation->name ?? null ) ) { - $this->extension->registerRoute($name, $annotation->getRoute(), $annotation->class, $annotation->classMethod, $annotation->methods ?? (array) $annotation->method); - } + $this->eventManager->execute(Event\RoutingCompileRoutes::class, $this, $annotation); + /* @deprecated annotation->method will become standard when using native attributes */ foreach((array) ( $annotation->method ?? $annotation->methods ) as $method) { # Mapping every URLs from annotations in searched folders (Api, Controller, etc...) $route->map(strtoupper($method), $annotation->getRoute(), function (ServerRequestInterface $request, array $arguments) use ( @@ -84,34 +60,13 @@ class Routing { { $class = $annotation->class; $method = $annotation->classMethod; - - # $container->set($class, autowire($class)->method($method, $request)); - $object = $container->get($class); - # Checking if user needs to be logged - if ( ( $redirect = $this->security->verify($class, $method) ) && ( empty($object->user) || ! $object->user->logged ) ) { - $this->session->redirectedFrom = (string) $request->getUri(); - - return $redirect; - } - - if ( $forbidden = $this->security->taxus($class, $method, $object->user ?? null) ) { - return $forbidden; - } - - # @TODO of course, this as to go ; moving to a simple callback method soon which can then be fed by Picea - if ( $container->has(Picea::class) ) { - $container->get(Picea::class)->globalVariables['route'] = $annotation; - } - - $request = $request->withAttribute('lean.route', $annotation); - - $this->session->set("lean.route", $annotation); + $this->eventManager->execute(Event\RoutingMapRoutes::class, $this, $container, $request, $annotation); $container->set(ServerRequestInterface::class, $request); - return $object->$method($request, $arguments); + return $this->response ?? $object->$method($request, $arguments); }); } }