Compare commits
No commits in common. "ec250be35b7152a362a1d0ea4e878e790333f82d" and "fd95e8db91fda3162c6c7682383440802d301b8b" have entirely different histories.
ec250be35b
...
fd95e8db91
@ -1,77 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Picea\Picea;
|
|
||||||
use function DI\autowire, DI\create, DI\get;
|
use function DI\autowire, DI\create, DI\get;
|
||||||
|
|
||||||
use Lean\{ Lean, Routing, Event\RoutingCompileRoutes, Event\RoutingMapRoutes };
|
use Lean\Lean;
|
||||||
use Mcnd\Event;
|
use Mcnd\Event;
|
||||||
use Taxus\Taxus;
|
|
||||||
use Storage\Session;
|
|
||||||
use Notes\Route\Attribute\Method\Route;
|
|
||||||
use Notes\Security\SecurityHandler;
|
|
||||||
use Psr\Container\ContainerInterface;
|
|
||||||
use Psr\Http\Message\{ ResponseInterface, ServerRequestInterface };
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
Event\EventManager::class => autowire(Event\EventManager::class),
|
Event\EventManager::class => autowire(Event\EventManager::class),
|
||||||
|
|
||||||
Event\EventMiddleware::class => function($c) {
|
Event\EventMiddleware::class => function($c) {
|
||||||
$mw = new Event\EventMiddleware($c, $c->get(Event\EventManager::class));
|
$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;
|
return $mw->fromAttributes($c->get(Notes\Event\EventFetcher::class));
|
||||||
},
|
|
||||||
|
|
||||||
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) {
|
Notes\Event\EventFetcher::class => function($c) {
|
||||||
|
1
skeleton/meta/definitions/env/prod.php
vendored
1
skeleton/meta/definitions/env/prod.php
vendored
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use Picea\Picea;
|
use Picea\Picea;
|
||||||
|
|
||||||
|
#use CSLSJ\Debogueur\DebogueurMiddleware;
|
||||||
use Negundo\Client\{ NegundoMiddleware, SoftwareConfig };
|
use Negundo\Client\{ NegundoMiddleware, SoftwareConfig };
|
||||||
|
|
||||||
use Laminas\Diactoros\Response\HtmlResponse;
|
use Laminas\Diactoros\Response\HtmlResponse;
|
||||||
|
@ -29,6 +29,8 @@ new class(dirname(__DIR__)) extends \Lean\Kernel {
|
|||||||
|
|
||||||
protected function serviceContainer() : self
|
protected function serviceContainer() : self
|
||||||
{
|
{
|
||||||
|
# $this->container->get(ConnectionAdapter::class);
|
||||||
|
|
||||||
return parent::serviceContainer();
|
return parent::serviceContainer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,7 +92,7 @@ trait ControllerTrait {
|
|||||||
return new HtmlResponse($html, $code, $headers);
|
return new HtmlResponse($html, $code, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function renderJson(mixed $data, int $code = 200, array $headers = []) : ResponseInterface
|
public static function renderJson(/*\JsonSerializable|array*/ $data, int $code = 200, array $headers = []) : ResponseInterface
|
||||||
{
|
{
|
||||||
return new JsonResponse($data, $code, $headers);
|
return new JsonResponse($data, $code, $headers);
|
||||||
}
|
}
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
<?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;
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
<?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;
|
|
||||||
}
|
|
@ -24,34 +24,58 @@ use Picea\Picea,
|
|||||||
use Storage\Cookie,
|
use Storage\Cookie,
|
||||||
Storage\Session;
|
Storage\Session;
|
||||||
|
|
||||||
use Mcnd\Event\EventManager;
|
|
||||||
|
|
||||||
use function DI\autowire, DI\create;
|
use function DI\autowire, DI\create;
|
||||||
|
|
||||||
class Routing {
|
class Routing {
|
||||||
|
|
||||||
public Annotation $selectedRoute;
|
public Annotation $selectedRoute;
|
||||||
|
|
||||||
public ResponseInterface $response;
|
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 function __construct(
|
public function __construct(
|
||||||
public Session $session,
|
Session $session,
|
||||||
public Cookie $cookie,
|
Cookie $cookie,
|
||||||
public UrlExtension $extension,
|
UrlExtension $extension,
|
||||||
public Router $router,
|
Router $router,
|
||||||
public RouteFetcher $fetcher,
|
RouteFetcher $routeFetcher,
|
||||||
public SecurityHandler $security,
|
SecurityHandler $security,
|
||||||
public LanguageHandler $language,
|
LanguageHandler $language,
|
||||||
public Taxus $taxus,
|
Taxus $taxus
|
||||||
public EventManager $eventManager,
|
)
|
||||||
) { }
|
{
|
||||||
|
$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 function registerRoute(ContainerInterface $container, string $urlBase) {
|
public function registerRoute(ContainerInterface $container, string $urlBase) {
|
||||||
$this->router->group(rtrim($urlBase, "/"), function (RouteGroup $route) use ($container) {
|
$this->router->group(rtrim($urlBase, "/"), function (RouteGroup $route) use ($container) {
|
||||||
foreach($this->fetcher->compile() as $annotation) {
|
|
||||||
$this->eventManager->execute(Event\RoutingCompileRoutes::class, $this, $annotation);
|
|
||||||
|
|
||||||
/* @deprecated annotation->method will become standard when using native attributes */
|
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) {
|
foreach((array) ( $annotation->method ?? $annotation->methods ) as $method) {
|
||||||
# Mapping every URLs from annotations in searched folders (Api, Controller, etc...)
|
# Mapping every URLs from annotations in searched folders (Api, Controller, etc...)
|
||||||
$route->map(strtoupper($method), $annotation->getRoute(), function (ServerRequestInterface $request, array $arguments) use (
|
$route->map(strtoupper($method), $annotation->getRoute(), function (ServerRequestInterface $request, array $arguments) use (
|
||||||
@ -60,13 +84,34 @@ class Routing {
|
|||||||
{
|
{
|
||||||
$class = $annotation->class;
|
$class = $annotation->class;
|
||||||
$method = $annotation->classMethod;
|
$method = $annotation->classMethod;
|
||||||
|
|
||||||
|
# $container->set($class, autowire($class)->method($method, $request));
|
||||||
|
|
||||||
$object = $container->get($class);
|
$object = $container->get($class);
|
||||||
|
|
||||||
$this->eventManager->execute(Event\RoutingMapRoutes::class, $this, $container, $request, $annotation);
|
# 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);
|
||||||
|
|
||||||
$container->set(ServerRequestInterface::class, $request);
|
$container->set(ServerRequestInterface::class, $request);
|
||||||
|
|
||||||
return $this->response ?? $object->$method($request, $arguments);
|
return $object->$method($request, $arguments);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user