2020-10-20 13:55:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Lean;
|
|
|
|
|
|
|
|
use function DI\autowire, DI\create;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
use Picea\Extension\UrlExtension;
|
|
|
|
|
|
|
|
use Storage\Cookie,
|
|
|
|
Storage\Session;
|
|
|
|
|
|
|
|
class Routing {
|
|
|
|
|
|
|
|
protected Session $session;
|
|
|
|
|
|
|
|
protected Cookie $cookie;
|
|
|
|
|
|
|
|
protected UrlExtension $extension;
|
|
|
|
|
|
|
|
protected RouteFetcher $fetcher;
|
|
|
|
|
|
|
|
protected SecurityHandler $security;
|
|
|
|
|
|
|
|
protected LanguageHandler $language;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
Session $session,
|
|
|
|
Cookie $cookie,
|
|
|
|
UrlExtension $extension,
|
|
|
|
Router $router,
|
|
|
|
RouteFetcher $routeFetcher,
|
|
|
|
SecurityHandler $security,
|
|
|
|
LanguageHandler $language
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$this->session = $session;
|
|
|
|
$this->cookie = $cookie;
|
|
|
|
$this->extension = $extension;
|
|
|
|
$this->fetcher = $routeFetcher;
|
|
|
|
$this->security = $security;
|
|
|
|
$this->language = $language;
|
|
|
|
$this->router = $router;
|
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) {
|
|
|
|
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));
|
|
|
|
|
|
|
|
if ( null !== ( $annotation = $this->language->verify($class) ) ) {
|
|
|
|
if ( $annotation->key ) {
|
|
|
|
# TODO !!! $language
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$object = $container->get($class);
|
|
|
|
|
|
|
|
# Checking if user needs to be logged
|
|
|
|
if ( ! $object->user->logged && ( $redirect = $this->security->verify($class, $method) ) ) {
|
|
|
|
return $redirect;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $object->$method($request, $arguments);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|