2020-10-20 13:55:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Lean;
|
|
|
|
|
2023-01-26 13:28:36 +00:00
|
|
|
use Notes\Annotation;
|
2021-08-27 18:06:15 +00:00
|
|
|
use Taxus\Taxus;
|
2020-10-20 13:55:48 +00:00
|
|
|
|
|
|
|
use League\Route\RouteGroup,
|
|
|
|
League\Route\Router;
|
|
|
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface,
|
|
|
|
Psr\Http\Message\ResponseInterface,
|
|
|
|
Psr\Container\ContainerInterface;
|
|
|
|
|
|
|
|
use Notes\Route\RouteFetcher;
|
|
|
|
|
|
|
|
use Notes\Security\SecurityHandler;
|
|
|
|
|
|
|
|
use Notes\Tell\LanguageHandler;
|
|
|
|
|
2021-02-16 03:10:04 +00:00
|
|
|
use Picea\Picea,
|
|
|
|
Picea\Extension\UrlExtension;
|
2020-10-20 13:55:48 +00:00
|
|
|
|
|
|
|
use Storage\Cookie,
|
|
|
|
Storage\Session;
|
|
|
|
|
2021-08-27 18:06:15 +00:00
|
|
|
use function DI\autowire, DI\create;
|
|
|
|
|
2020-10-20 13:55:48 +00:00
|
|
|
class Routing {
|
|
|
|
|
2023-01-26 13:28:36 +00:00
|
|
|
public Annotation $selectedRoute;
|
|
|
|
|
2020-10-20 13:55:48 +00:00
|
|
|
protected Session $session;
|
|
|
|
|
|
|
|
protected Cookie $cookie;
|
|
|
|
|
|
|
|
protected UrlExtension $extension;
|
|
|
|
|
|
|
|
protected RouteFetcher $fetcher;
|
|
|
|
|
|
|
|
protected SecurityHandler $security;
|
|
|
|
|
|
|
|
protected LanguageHandler $language;
|
|
|
|
|
2021-08-27 18:06:15 +00:00
|
|
|
protected Taxus $taxus;
|
|
|
|
|
2020-10-20 13:55:48 +00:00
|
|
|
public function __construct(
|
|
|
|
Session $session,
|
|
|
|
Cookie $cookie,
|
|
|
|
UrlExtension $extension,
|
|
|
|
Router $router,
|
|
|
|
RouteFetcher $routeFetcher,
|
|
|
|
SecurityHandler $security,
|
2021-08-27 18:06:15 +00:00
|
|
|
LanguageHandler $language,
|
|
|
|
Taxus $taxus
|
2020-10-20 13:55:48 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
$this->session = $session;
|
|
|
|
$this->cookie = $cookie;
|
|
|
|
$this->extension = $extension;
|
|
|
|
$this->fetcher = $routeFetcher;
|
|
|
|
$this->security = $security;
|
|
|
|
$this->language = $language;
|
|
|
|
$this->router = $router;
|
2021-08-27 18:06:15 +00:00
|
|
|
$this->taxus = $taxus;
|
2020-10-20 14:38:03 +00:00
|
|
|
}
|
2020-10-20 13:55:48 +00:00
|
|
|
|
|
|
|
public function registerRoute(ContainerInterface $container, string $urlBase) {
|
|
|
|
$this->router->group(rtrim($urlBase, "/"), function (RouteGroup $route) use ($container) {
|
2022-11-29 19:53:41 +00:00
|
|
|
|
2020-10-20 13:55:48 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
|
|
|
$container, $route, $annotation
|
|
|
|
) : ResponseInterface
|
|
|
|
{
|
|
|
|
$class = $annotation->class;
|
|
|
|
$method = $annotation->classMethod;
|
|
|
|
|
|
|
|
# $container->set($class, autowire($class)->method($method, $request));
|
|
|
|
|
2021-02-16 03:10:04 +00:00
|
|
|
if ( null !== ( $languageAnnotation = $this->language->verify($class) ) ) {
|
|
|
|
if ( $languageAnnotation->key ) {
|
2020-10-20 13:55:48 +00:00
|
|
|
# TODO !!! $language
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$object = $container->get($class);
|
|
|
|
|
|
|
|
# Checking if user needs to be logged
|
2021-08-27 18:06:15 +00:00
|
|
|
if ( ( $redirect = $this->security->verify($class, $method) ) && ( empty($object->user) || ! $object->user->logged ) ) {
|
2021-02-16 03:10:04 +00:00
|
|
|
$this->session->redirectedFrom = (string) $request->getUri();
|
|
|
|
|
2020-10-20 13:55:48 +00:00
|
|
|
return $redirect;
|
|
|
|
}
|
|
|
|
|
2022-10-10 15:28:38 +00:00
|
|
|
if ( $forbidden = $this->security->taxus($class, $method, $object->user ?? null) ) {
|
2021-08-27 18:06:15 +00:00
|
|
|
return $forbidden;
|
|
|
|
}
|
|
|
|
|
2022-11-29 19:53:41 +00:00
|
|
|
# @TODO of course, this as to go ; moving to a simple callback method soon which can then be fed by Picea
|
2021-02-16 03:10:04 +00:00
|
|
|
if ( $container->has(Picea::class) ) {
|
|
|
|
$container->get(Picea::class)->globalVariables['route'] = $annotation;
|
|
|
|
}
|
|
|
|
|
2023-01-26 13:28:36 +00:00
|
|
|
$request = $request->withAttribute('lean.route', $annotation);
|
|
|
|
|
|
|
|
$this->session->set("lean.route", $annotation);
|
|
|
|
|
|
|
|
$container->set(ServerRequestInterface::class, $request);
|
|
|
|
|
|
|
|
return $object->$method($request, $arguments);
|
2020-10-20 13:55:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|