From fd8fa7695e333cb235ef986f63fc46b531b97715 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 17 Oct 2024 18:20:02 +0000 Subject: [PATCH] - Fixed overridding of routes definition of multiple route with same names - Corrected a bug withing the Pagination widget if there was only one page of result --- meta/definitions/event.php | 3 +- src/ControllerTrait.php | 6 ++-- src/Routing.php | 55 ++++++++++++++++++++++++++----- view/lean/widget/pagination.phtml | 8 +++-- 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/meta/definitions/event.php b/meta/definitions/event.php index e6c52e0..455890a 100644 --- a/meta/definitions/event.php +++ b/meta/definitions/event.php @@ -60,12 +60,11 @@ return [ } $routing->response = $redirect; - return; } } - if ( $securityHandler->isLocked($class, $method) && $container->has(Taxus::class) ) { + if ( $securityHandler->isLocked($class, $method) && $container->has(Taxus::class) && $container->has(SecurityHandler::class) ) { if ( $forbidden = $container->get(SecurityHandler::class)->taxus($class, $method, $object->user ?? null) ) { $routing->response = $forbidden; diff --git a/src/ControllerTrait.php b/src/ControllerTrait.php index 779ba84..e5b05cc 100644 --- a/src/ControllerTrait.php +++ b/src/ControllerTrait.php @@ -8,20 +8,18 @@ use Notes\Route\Attribute\Object\Route; use Notes\Security\Attribute\Security; use Picea, Picea\Ui\Method\FormContext; use TheBugs\Email\MailerInterface; +use Storage\Session; use League\CommonMark\CommonMarkConverter; -use Storage\{ Session, Cookie }; use Psr\Http\Message\{ ServerRequestInterface, ResponseInterface }; use function file_get_contents; -#[Security(locked: true, realm: "Protected Area")] +#[Security(locked: false, realm: "Protected Area")] #[Route(method: [ "GET", "POST" ])] trait ControllerTrait { public ? \Notes\Breadcrumb\Breadcrumb $breadcrumb; - public ? Cookie $cookie; - public ? Session $session; public ? Picea\Picea $picea; diff --git a/src/Routing.php b/src/Routing.php index 7155aca..98ce4c2 100644 --- a/src/Routing.php +++ b/src/Routing.php @@ -5,6 +5,7 @@ namespace Lean; use League\Route\RouteGroup, League\Route\Router; +use Notes\Route\Attribute\Method\Route; use Psr\Http\Message\ServerRequestInterface, Psr\Http\Message\ResponseInterface, Psr\Container\ContainerInterface; @@ -17,7 +18,7 @@ class Routing { public ResponseInterface $response; - protected array $registeredRoutes; + public array $overriddenRoutes = []; public function __construct( public Router $router, @@ -26,24 +27,24 @@ class Routing { ) { } public function registerRoute(ContainerInterface $container, string $urlBase) { - $this->registeredRoutes[$urlBase] ??= []; + #$this->registeredRoutes ??= []; - $this->router->group(rtrim($urlBase, "/"), function (RouteGroup $route) use ($container, $urlBase) { - foreach(array_reverse($this->fetcher->compile()) as $attribute) { + $this->router->group(rtrim($urlBase, "/"), function (RouteGroup $route) use ($container) { + foreach($this->fetchRoutesAttributes() as $attribute) { $this->eventManager->execute(Event\RoutingCompileRoutes::class, $this, $attribute); # Mapping every URLs from attributes in searched folders (Api, Controller, etc...) foreach((array) $attribute->method as $method) { - if (! empty($this->registeredRoutes[$urlBase][$attribute->getRoute().$method])) { - continue; - } + #if (! empty($this->registeredRoutes[$attribute->getRoute().$method])) { + # continue; + #} - $this->registeredRoutes[$urlBase][$attribute->getRoute().$method] = true; + #$this->registeredRoutes[$attribute->getRoute().$method] = true; $route->map(strtoupper($method), $attribute->getRoute(), function (ServerRequestInterface $request, array $arguments) use ( $container, $route, $attribute ) : ResponseInterface { - $class = $attribute->class; + $class = $attribute->class; $method = $attribute->classMethod; $object = $container->get($class); @@ -57,4 +58,40 @@ class Routing { } }); } + + public function fetchRoutesAttributes() : iterable + { + static $list = []; + + if ($list) { + return $list; + } + + foreach($this->fetcher->compile() as $routeAttribute) { + # Routes with differents methods can have the same name + $key = sprintf('{%s}~{%s}', $routeAttribute->name, $this->serializeMethods((array) $routeAttribute->method)); + + if ( isset($list[$key]) ) { + $this->overriddenRoutes[$key] = $list[$key]; + } + + $list[$key] = $routeAttribute; + } + + return $list; + } + + public function findOverriddenRoute(Route $route) : false|iterable + { + return array_filter($this->overriddenRoutes, fn($e) => $route->name === $e->name); + } + + protected function serializeMethods(array $methods) : string + { + $methods = array_map('strtolower', $methods); + + sort($methods); + + return implode(':', $methods); + } } diff --git a/view/lean/widget/pagination.phtml b/view/lean/widget/pagination.phtml index 2769e45..79aff5f 100644 --- a/view/lean/widget/pagination.phtml +++ b/view/lean/widget/pagination.phtml @@ -6,9 +6,11 @@
  • 1
  • - {% foreach range(2, $pageCount < $maxPage ? $pageCount : $maxPage) as $pageIndex %} -
  • {{ $pageIndex }}
  • - {% endforeach %} + {% if $pageCount > 1%} + {% foreach range(2, $pageCount < $maxPage ? $pageCount : $maxPage) as $pageIndex %} +
  • {{ $pageIndex }}
  • + {% endforeach %} + {% endif %}
  • {% lang "lean.widget.pagination.next" %}