- Added an event manager for Lean's route dispatching

This commit is contained in:
Dave Mc Nicoll committed 2023-05-30 15:37:03 -04:00
1 parent f0082bf0a9
commit 8390506361
8 files changed
+102 -70

No files matched your search

+1 -1
View File
@@ -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);
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace Lean\Event;
use Lean\Routing;
use Notes\Route\Attribute\Method\Route;
interface RoutingCompileRoutes {
public function execute(Routing $routing, Route|\Notes\Route\Annotation\Method\Route $attribute) : void;
}
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace Lean\Event;
use Lean\Routing;
use Notes\Route\Attribute\Method\Route;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\{ ResponseInterface, ServerRequestInterface };
interface RoutingMapRoutes {
public function execute(Routing $routing, ContainerInterface $container, ServerRequestInterface & $request, Route|\Notes\Route\Annotation\Method\Route $attribute) : void;
}
+17 -62
View File
@@ -24,58 +24,34 @@ use Picea\Picea,
use Storage\Cookie,
Storage\Session;
use Mcnd\Event\EventManager;
use function DI\autowire, DI\create;
class Routing {
public Annotation $selectedRoute;
protected Session $session;
protected Cookie $cookie;
protected UrlExtension $extension;
protected RouteFetcher $fetcher;
protected SecurityHandler $security;
protected LanguageHandler $language;
protected Taxus $taxus;
protected Router $router;
public ResponseInterface $response;
public function __construct(
Session $session,
Cookie $cookie,
UrlExtension $extension,
Router $router,
RouteFetcher $routeFetcher,
SecurityHandler $security,
LanguageHandler $language,
Taxus $taxus
)
{
$this->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);
});
}
}