From eea6399eef1dfef2ef242d1237fd4a539e97172f Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Wed, 24 Jan 2024 10:36:08 +0100 Subject: [PATCH] feat(CoreBundle): Refactored node routing --- .env | 2 +- docker-compose.yml | 32 ++------ .../src/Routing/ThemeAwareNodeRouter.php | 26 +++--- .../NodesSourcesPathGeneratingEvent.php | 56 +++---------- .../NodeSourcePathSubscriber.php | 13 +-- .../src/Routing/DeferredRouteCollection.php | 29 ------- .../src/Routing/DynamicUrlMatcher.php | 20 +---- .../src/Routing/NodeRouter.php | 79 +++++-------------- .../src/Routing/NodeUrlMatcher.php | 46 +++++------ .../src/Routing/NodesSourcesPathResolver.php | 24 ++---- .../src/Routing/NodesSourcesUrlGenerator.php | 27 +------ .../src/Routing/NullLoader.php | 28 ++----- ...timizedNodesSourcesGraphPathAggregator.php | 23 +++--- .../src/Routing/RedirectionMatcher.php | 9 +-- .../src/Routing/RedirectionPathResolver.php | 13 +-- .../src/Routing/RedirectionRouter.php | 25 +----- .../src/Routing/StaticRouter.php | 52 ------------ 17 files changed, 107 insertions(+), 397 deletions(-) delete mode 100644 lib/RoadizCoreBundle/src/Routing/DeferredRouteCollection.php delete mode 100644 lib/RoadizCoreBundle/src/Routing/StaticRouter.php diff --git a/.env b/.env index 1994475b..3387bd10 100644 --- a/.env +++ b/.env @@ -50,7 +50,7 @@ HOSTNAME_PMA=`pma.roadiz-core-app.test`,`pma.roadiz-core-app.local` HOSTNAME_SOLR=`solr.roadiz-core-app.test`,`solr.roadiz-core-app.local` HOSTNAME_MAILER=`mail.roadiz-core-app.test`,`mail.roadiz-core-app.local` -DEFAULT_GATEWAY=172.58.0.1 +DEFAULT_GATEWAY=172.58.0.0 ###> rezozero/intervention-request-bundle ### IR_DEFAULT_QUALITY=90 IR_MAX_PIXEL_SIZE=2500 diff --git a/docker-compose.yml b/docker-compose.yml index f01c7c75..b449e900 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -58,12 +58,8 @@ services: links: - db:db networks: - frontproxynet: - aliases: - - ${APP_NAMESPACE}_pma - default: - aliases: - - pma + - frontproxynet + - default labels: - "traefik.enable=true" - "traefik.http.services.${APP_NAMESPACE}_pma.loadbalancer.server.scheme=http" @@ -91,12 +87,8 @@ services: - solr-precreate - ${SOLR_CORE_NAME} networks: - frontproxynet: - aliases: - - ${APP_NAMESPACE}_solr - default: - aliases: - - solr + - frontproxynet + - default labels: - "traefik.enable=true" - "traefik.http.services.${APP_NAMESPACE}_solr.loadbalancer.server.scheme=http" @@ -174,12 +166,8 @@ services: volumes: - ./:/var/www/html:cached networks: - frontproxynet: - aliases: - - ${APP_NAMESPACE}_nginx - default: - aliases: - - nginx + - frontproxynet + - default labels: - "traefik.enable=true" - "traefik.http.services.${APP_NAMESPACE}.loadbalancer.server.scheme=http" @@ -205,12 +193,8 @@ services: - ${PUBLIC_MAILER_PORT}:8025/tcp - ${SMTP_MAILER_PORT}:1025/tcp networks: - frontproxynet: - aliases: - - ${APP_NAMESPACE}_mailer - default: - aliases: - - mailer + - frontproxynet + - default labels: - "traefik.enable=true" - "traefik.http.services.${APP_NAMESPACE}_mailer.loadbalancer.server.scheme=http" diff --git a/lib/RoadizCompatBundle/src/Routing/ThemeAwareNodeRouter.php b/lib/RoadizCompatBundle/src/Routing/ThemeAwareNodeRouter.php index ad74858f..9857a841 100644 --- a/lib/RoadizCompatBundle/src/Routing/ThemeAwareNodeRouter.php +++ b/lib/RoadizCompatBundle/src/Routing/ThemeAwareNodeRouter.php @@ -4,6 +4,7 @@ namespace RZ\Roadiz\CompatBundle\Routing; +use Psr\Cache\InvalidArgumentException; use RZ\Roadiz\CompatBundle\Theme\ThemeResolverInterface; use RZ\Roadiz\CoreBundle\Routing\NodeRouter; use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface; @@ -15,17 +16,14 @@ final class ThemeAwareNodeRouter implements RouterInterface, RequestMatcherInterface, VersatileGeneratorInterface { - private ThemeResolverInterface $themeResolver; - private NodeRouter $innerRouter; - /** * @param ThemeResolverInterface $themeResolver * @param NodeRouter $innerRouter */ - public function __construct(ThemeResolverInterface $themeResolver, NodeRouter $innerRouter) - { - $this->themeResolver = $themeResolver; - $this->innerRouter = $innerRouter; + public function __construct( + private readonly ThemeResolverInterface $themeResolver, + private readonly NodeRouter $innerRouter + ) { } public function setContext(RequestContext $context): void @@ -48,6 +46,10 @@ public function getRouteCollection(): RouteCollection return $this->innerRouter->getRouteCollection(); } + /** + * @inheritDoc + * @throws InvalidArgumentException + */ public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string { $this->innerRouter->setTheme($this->themeResolver->findTheme($this->getContext()->getHost())); @@ -59,12 +61,10 @@ public function match(string $pathinfo): array return $this->innerRouter->match($pathinfo); } - public function supports(string $name): bool - { - return $this->innerRouter->supports($name); - } - - public function getRouteDebugMessage(string $name, array $parameters = []): string + /** + * @inheritDoc + */ + public function getRouteDebugMessage(mixed $name, array $parameters = []): string { return $this->innerRouter->getRouteDebugMessage($name, $parameters); } diff --git a/lib/RoadizCoreBundle/src/Event/NodesSources/NodesSourcesPathGeneratingEvent.php b/lib/RoadizCoreBundle/src/Event/NodesSources/NodesSourcesPathGeneratingEvent.php index b2cd984b..2efc28d3 100644 --- a/lib/RoadizCoreBundle/src/Event/NodesSources/NodesSourcesPathGeneratingEvent.php +++ b/lib/RoadizCoreBundle/src/Event/NodesSources/NodesSourcesPathGeneratingEvent.php @@ -11,46 +11,14 @@ final class NodesSourcesPathGeneratingEvent extends Event { - /** - * @var bool - */ - protected $forceLocaleWithUrlAlias; - /** - * @var Theme|null - */ - private $theme; - /** - * @var NodesSources|null - */ - private $nodeSource; - /** - * @var array|null - */ - private $parameters; - /** - * @var RequestContext|null - */ - private $requestContext; - /** - * @var bool - */ - private $forceLocale = false; - /** - * @var string|null - */ - private $path; + private ?string $path; /** * @var bool Tells Node Router to prepend request context information to path or not. */ - private $isComplete = false; - /** - * @var bool - */ - protected $containsScheme = false; + private bool $isComplete = false; + protected bool $containsScheme = false; /** - * NodesSourcesPathGeneratingEvent constructor. - * * @param Theme|null $theme * @param NodesSources|null $nodeSource * @param RequestContext|null $requestContext @@ -59,19 +27,13 @@ final class NodesSourcesPathGeneratingEvent extends Event * @param bool $forceLocaleWithUrlAlias */ public function __construct( - ?Theme $theme, - ?NodesSources $nodeSource, - ?RequestContext $requestContext, - array $parameters = [], - bool $forceLocale = false, - bool $forceLocaleWithUrlAlias = false + private readonly ?Theme $theme, + private ?NodesSources $nodeSource, + private readonly ?RequestContext $requestContext, + private array $parameters = [], + private readonly bool $forceLocale = false, + private bool $forceLocaleWithUrlAlias = false ) { - $this->theme = $theme; - $this->nodeSource = $nodeSource; - $this->requestContext = $requestContext; - $this->forceLocale = $forceLocale; - $this->parameters = $parameters; - $this->forceLocaleWithUrlAlias = $forceLocaleWithUrlAlias; } /** diff --git a/lib/RoadizCoreBundle/src/EventSubscriber/NodeSourcePathSubscriber.php b/lib/RoadizCoreBundle/src/EventSubscriber/NodeSourcePathSubscriber.php index 82dbaccf..c7f2b9f9 100644 --- a/lib/RoadizCoreBundle/src/EventSubscriber/NodeSourcePathSubscriber.php +++ b/lib/RoadizCoreBundle/src/EventSubscriber/NodeSourcePathSubscriber.php @@ -11,14 +11,9 @@ class NodeSourcePathSubscriber implements EventSubscriberInterface { - protected NodesSourcesPathAggregator $pathAggregator; - - /** - * @param NodesSourcesPathAggregator $pathAggregator - */ - public function __construct(NodesSourcesPathAggregator $pathAggregator) - { - $this->pathAggregator = $pathAggregator; + public function __construct( + protected readonly NodesSourcesPathAggregator $pathAggregator + ) { } /** @@ -28,7 +23,6 @@ public static function getSubscribedEvents(): array { return [ NodesSourcesPathGeneratingEvent::class => [['onNodesSourcesPath', -100]], - '\RZ\Roadiz\Core\Events\NodesSources\NodesSourcesPathGeneratingEvent' => [['onNodesSourcesPath', -100]], ]; } @@ -39,7 +33,6 @@ public function onNodesSourcesPath(NodesSourcesPathGeneratingEvent $event): void { $urlGenerator = new NodesSourcesUrlGenerator( $this->pathAggregator, - null, $event->getNodeSource(), $event->isForceLocale(), $event->isForceLocaleWithUrlAlias() diff --git a/lib/RoadizCoreBundle/src/Routing/DeferredRouteCollection.php b/lib/RoadizCoreBundle/src/Routing/DeferredRouteCollection.php deleted file mode 100644 index 4597717c..00000000 --- a/lib/RoadizCoreBundle/src/Routing/DeferredRouteCollection.php +++ /dev/null @@ -1,29 +0,0 @@ -stopwatch = $stopwatch; - $this->logger = $logger ?? new NullLogger(); - $this->previewResolver = $previewResolver; } } diff --git a/lib/RoadizCoreBundle/src/Routing/NodeRouter.php b/lib/RoadizCoreBundle/src/Routing/NodeRouter.php index c9f89099..4d839b48 100644 --- a/lib/RoadizCoreBundle/src/Routing/NodeRouter.php +++ b/lib/RoadizCoreBundle/src/Routing/NodeRouter.php @@ -5,6 +5,7 @@ namespace RZ\Roadiz\CoreBundle\Routing; use Psr\Cache\CacheItemPoolInterface; +use Psr\Cache\InvalidArgumentException; use Psr\Log\LoggerInterface; use RZ\Roadiz\CoreBundle\Bag\Settings; use RZ\Roadiz\CoreBundle\Entity\NodesSources; @@ -15,7 +16,6 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Routing\Exception\InvalidParameterException; use Symfony\Component\Routing\Exception\RouteNotFoundException; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RouteCollection; @@ -28,24 +28,12 @@ class NodeRouter extends Router implements VersatileGeneratorInterface */ public const NO_CACHE_PARAMETER = '_no_cache'; private ?Theme $theme = null; - private CacheItemPoolInterface $nodeSourceUrlCacheAdapter; - private Settings $settingsBag; - private EventDispatcherInterface $eventDispatcher; - /** - * @param NodeUrlMatcherInterface $matcher - * @param Settings $settingsBag - * @param EventDispatcherInterface $eventDispatcher - * @param CacheItemPoolInterface $nodeSourceUrlCacheAdapter - * @param array $options - * @param RequestContext $context - * @param LoggerInterface $logger - */ public function __construct( NodeUrlMatcherInterface $matcher, - Settings $settingsBag, - EventDispatcherInterface $eventDispatcher, - CacheItemPoolInterface $nodeSourceUrlCacheAdapter, + protected readonly Settings $settingsBag, + protected readonly EventDispatcherInterface $eventDispatcher, + protected readonly CacheItemPoolInterface $nodeSourceUrlCacheAdapter, RequestContext $context, LoggerInterface $logger, array $options = [] @@ -57,10 +45,7 @@ public function __construct( $context, $logger ); - $this->settingsBag = $settingsBag; - $this->eventDispatcher = $eventDispatcher; $this->matcher = $matcher; - $this->nodeSourceUrlCacheAdapter = $nodeSourceUrlCacheAdapter; } /** @@ -81,22 +66,6 @@ public function getMatcher(): UrlMatcherInterface return $this->matcher; } - /** - * No generator for a node router. - */ - public function getGenerator(): UrlGeneratorInterface - { - throw new \BadMethodCallException(get_class($this) . ' does not support path generation.'); - } - - /** - * @inheritDoc - */ - public function supports(mixed $name): bool - { - return ($name instanceof NodesSources || $name === RouteObjectInterface::OBJECT_BASED_ROUTE_NAME); - } - /** * @return Theme|null */ @@ -116,24 +85,11 @@ public function setTheme(?Theme $theme): NodeRouter } /** - * Convert a route identifier (name, content object etc) into a string - * usable for logging and other debug/error messages - * - * @param mixed $name - * @param array $parameters which should contain a content field containing - * a RouteReferrersReadInterface object - * - * @return string + * @inheritDoc */ - public function getRouteDebugMessage($name, array $parameters = []): string + public function getRouteDebugMessage(string $name, array $parameters = []): string { - if ($name instanceof NodesSources) { - @trigger_error('Passing an object as route name is deprecated since version 1.5. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT` resp the content id with content_id.', E_USER_DEPRECATED); - return '[' . $name->getTranslation()->getLocale() . ']' . - $name->getTitle() . ' - ' . - $name->getNode()->getNodeName() . - '[' . $name->getNode()->getId() . ']'; - } elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name) { + if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name) { if ( array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof NodesSources @@ -150,19 +106,20 @@ public function getRouteDebugMessage($name, array $parameters = []): string /** * {@inheritdoc} + * @throws InvalidArgumentException */ public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string { - if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name) { - if ( - array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && - $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof NodesSources - ) { - $route = $parameters[RouteObjectInterface::ROUTE_OBJECT]; - unset($parameters[RouteObjectInterface::ROUTE_OBJECT]); - } else { - $route = null; - } + if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME !== $name) { + throw new RouteNotFoundException(); + } + + if ( + array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && + $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof NodesSources + ) { + $route = $parameters[RouteObjectInterface::ROUTE_OBJECT]; + unset($parameters[RouteObjectInterface::ROUTE_OBJECT]); } else { $route = null; } diff --git a/lib/RoadizCoreBundle/src/Routing/NodeUrlMatcher.php b/lib/RoadizCoreBundle/src/Routing/NodeUrlMatcher.php index 1e1f7b0a..743e405f 100644 --- a/lib/RoadizCoreBundle/src/Routing/NodeUrlMatcher.php +++ b/lib/RoadizCoreBundle/src/Routing/NodeUrlMatcher.php @@ -15,16 +15,28 @@ use Symfony\Component\Stopwatch\Stopwatch; /** - * UrlMatcher which tries to grab Node and Translation - * information for a route. + * UrlMatcher which tries to grab Node and Translation information for a route. */ final class NodeUrlMatcher extends DynamicUrlMatcher implements NodeUrlMatcherInterface { - protected PathResolverInterface $pathResolver; /** - * @var class-string + * @param PathResolverInterface $pathResolver + * @param RequestContext $context + * @param PreviewResolverInterface $previewResolver + * @param Stopwatch $stopwatch + * @param LoggerInterface $logger + * @param class-string $defaultControllerClass */ - private string $defaultControllerClass; + public function __construct( + private readonly PathResolverInterface $pathResolver, + RequestContext $context, + PreviewResolverInterface $previewResolver, + Stopwatch $stopwatch, + LoggerInterface $logger, + private readonly string $defaultControllerClass + ) { + parent::__construct($context, $previewResolver, $stopwatch, $logger); + } /** * @return array @@ -43,28 +55,7 @@ public function getDefaultSupportedFormatExtension(): string } /** - * @param PathResolverInterface $pathResolver - * @param RequestContext $context - * @param PreviewResolverInterface $previewResolver - * @param Stopwatch $stopwatch - * @param LoggerInterface $logger - * @param class-string $defaultControllerClass - */ - public function __construct( - PathResolverInterface $pathResolver, - RequestContext $context, - PreviewResolverInterface $previewResolver, - Stopwatch $stopwatch, - LoggerInterface $logger, - string $defaultControllerClass - ) { - parent::__construct($context, $previewResolver, $stopwatch, $logger); - $this->pathResolver = $pathResolver; - $this->defaultControllerClass = $defaultControllerClass; - } - - /** - * {@inheritdoc} + * @inheritDoc */ public function match(string $pathinfo): array { @@ -90,7 +81,6 @@ protected function getNodeRouteHelper(NodesSources $nodeSource, ?Theme $theme): * @param string $decodedUrl * @param Theme|null $theme * @return array - * @throws \ReflectionException */ public function matchNode(string $decodedUrl, ?Theme $theme): array { diff --git a/lib/RoadizCoreBundle/src/Routing/NodesSourcesPathResolver.php b/lib/RoadizCoreBundle/src/Routing/NodesSourcesPathResolver.php index cfefb3dd..c7082597 100644 --- a/lib/RoadizCoreBundle/src/Routing/NodesSourcesPathResolver.php +++ b/lib/RoadizCoreBundle/src/Routing/NodesSourcesPathResolver.php @@ -20,28 +20,16 @@ final class NodesSourcesPathResolver implements PathResolverInterface { - private ManagerRegistry $managerRegistry; - private Stopwatch $stopwatch; private static string $nodeNamePattern = '[a-zA-Z0-9\-\_\.]+'; - private PreviewResolverInterface $previewResolver; - private Settings $settingsBag; - private RequestStack $requestStack; - private bool $useAcceptLanguageHeader; public function __construct( - ManagerRegistry $managerRegistry, - PreviewResolverInterface $previewResolver, - Stopwatch $stopwatch, - Settings $settingsBag, - RequestStack $requestStack, - bool $useAcceptLanguageHeader + private readonly ManagerRegistry $managerRegistry, + private readonly PreviewResolverInterface $previewResolver, + private readonly Stopwatch $stopwatch, + private readonly Settings $settingsBag, + private readonly RequestStack $requestStack, + private readonly bool $useAcceptLanguageHeader ) { - $this->stopwatch = $stopwatch; - $this->previewResolver = $previewResolver; - $this->managerRegistry = $managerRegistry; - $this->settingsBag = $settingsBag; - $this->requestStack = $requestStack; - $this->useAcceptLanguageHeader = $useAcceptLanguageHeader; } /** diff --git a/lib/RoadizCoreBundle/src/Routing/NodesSourcesUrlGenerator.php b/lib/RoadizCoreBundle/src/Routing/NodesSourcesUrlGenerator.php index 293d5e5e..943aa29e 100644 --- a/lib/RoadizCoreBundle/src/Routing/NodesSourcesUrlGenerator.php +++ b/lib/RoadizCoreBundle/src/Routing/NodesSourcesUrlGenerator.php @@ -13,31 +13,12 @@ */ final class NodesSourcesUrlGenerator { - protected ?Request $request; - protected ?NodesSources $nodeSource; - protected bool $forceLocale; - protected bool $forceLocaleWithUrlAlias; - protected NodesSourcesPathAggregator $pathAggregator; - - /** - * @param NodesSourcesPathAggregator $pathAggregator - * @param Request|null $request - * @param NodesSources|null $nodeSource - * @param bool $forceLocale - * @param bool $forceLocaleWithUrlAlias - */ public function __construct( - NodesSourcesPathAggregator $pathAggregator, - Request $request = null, - NodesSources $nodeSource = null, - bool $forceLocale = false, - bool $forceLocaleWithUrlAlias = false + private readonly NodesSourcesPathAggregator $pathAggregator, + private readonly ?NodesSources $nodeSource = null, + private readonly bool $forceLocale = false, + private readonly bool $forceLocaleWithUrlAlias = false ) { - $this->pathAggregator = $pathAggregator; - $this->request = $request; - $this->nodeSource = $nodeSource; - $this->forceLocale = $forceLocale; - $this->forceLocaleWithUrlAlias = $forceLocaleWithUrlAlias; } /** diff --git a/lib/RoadizCoreBundle/src/Routing/NullLoader.php b/lib/RoadizCoreBundle/src/Routing/NullLoader.php index 70eaacd3..673b621d 100644 --- a/lib/RoadizCoreBundle/src/Routing/NullLoader.php +++ b/lib/RoadizCoreBundle/src/Routing/NullLoader.php @@ -10,36 +10,23 @@ final class NullLoader implements LoaderInterface { /** - * Loads a resource. - * - * @param mixed $resource The resource - * @param string|null $type The resource type or null if unknown - * @return mixed - * - * @throws \Exception If something went wrong + * @inheritDoc */ - public function load($resource, $type = null): mixed + public function load(mixed $resource, string $type = null): mixed { return null; } /** - * Returns whether this class supports the given resource. - * - * @param mixed $resource A resource - * @param string|null $type The resource type or null if unknown - * - * @return bool True if this class supports the given resource, false otherwise + * @inheritDoc */ - public function supports($resource, $type = null): bool + public function supports(mixed $resource, string $type = null): bool { return true; } /** - * Gets the loader resolver. - * - * @return LoaderResolverInterface|null A LoaderResolverInterface instance + * @inheritDoc */ public function getResolver(): ?LoaderResolverInterface { @@ -47,10 +34,7 @@ public function getResolver(): ?LoaderResolverInterface } /** - * Sets the loader resolver. - * - * @param LoaderResolverInterface $resolver - * @return NullLoader + * @inheritDoc */ public function setResolver(LoaderResolverInterface $resolver): self { diff --git a/lib/RoadizCoreBundle/src/Routing/OptimizedNodesSourcesGraphPathAggregator.php b/lib/RoadizCoreBundle/src/Routing/OptimizedNodesSourcesGraphPathAggregator.php index 94af76a0..0d171edf 100644 --- a/lib/RoadizCoreBundle/src/Routing/OptimizedNodesSourcesGraphPathAggregator.php +++ b/lib/RoadizCoreBundle/src/Routing/OptimizedNodesSourcesGraphPathAggregator.php @@ -7,22 +7,16 @@ use Doctrine\ORM\Query; use Doctrine\Persistence\ManagerRegistry; use Psr\Cache\CacheItemPoolInterface; +use Psr\Cache\InvalidArgumentException; use RZ\Roadiz\CoreBundle\Entity\Node; use RZ\Roadiz\CoreBundle\Entity\NodesSources; final class OptimizedNodesSourcesGraphPathAggregator implements NodesSourcesPathAggregator { - private ManagerRegistry $managerRegistry; - private CacheItemPoolInterface $cacheAdapter; - - /** - * @param ManagerRegistry $managerRegistry - * @param CacheItemPoolInterface $cacheAdapter - */ - public function __construct(ManagerRegistry $managerRegistry, CacheItemPoolInterface $cacheAdapter) - { - $this->managerRegistry = $managerRegistry; - $this->cacheAdapter = $cacheAdapter; + public function __construct( + private readonly ManagerRegistry $managerRegistry, + private readonly CacheItemPoolInterface $cacheAdapter + ) { } private function getCacheKey(NodesSources $nodesSources): string @@ -34,6 +28,7 @@ private function getCacheKey(NodesSources $nodesSources): string * @param NodesSources $nodesSources * @param array $parameters * @return string + * @throws InvalidArgumentException */ public function aggregatePath(NodesSources $nodesSources, array $parameters = []): string { @@ -59,7 +54,7 @@ public function aggregatePath(NodesSources $nodesSources, array $parameters = [] * * @return array */ - protected function getParentsIds(Node $parent): array + private function getParentsIds(Node $parent): array { $parentIds = []; while ($parent !== null && !$parent->isHome()) { @@ -72,13 +67,13 @@ protected function getParentsIds(Node $parent): array /** * Get every nodeSource parents identifier from current to - * farest ancestor. + * farthest ancestor. * * @param NodesSources $source * * @return array */ - protected function getIdentifiers(NodesSources $source): array + private function getIdentifiers(NodesSources $source): array { $urlTokens = []; $parents = []; diff --git a/lib/RoadizCoreBundle/src/Routing/RedirectionMatcher.php b/lib/RoadizCoreBundle/src/Routing/RedirectionMatcher.php index 7dcbfe2a..81001bee 100644 --- a/lib/RoadizCoreBundle/src/Routing/RedirectionMatcher.php +++ b/lib/RoadizCoreBundle/src/Routing/RedirectionMatcher.php @@ -18,17 +18,12 @@ */ final class RedirectionMatcher extends UrlMatcher { - private LoggerInterface $logger; - private RedirectionPathResolver $pathResolver; - public function __construct( RequestContext $context, - RedirectionPathResolver $pathResolver, - LoggerInterface $logger + private readonly RedirectionPathResolver $pathResolver, + private readonly LoggerInterface $logger ) { parent::__construct(new RouteCollection(), $context); - $this->logger = $logger; - $this->pathResolver = $pathResolver; } /** diff --git a/lib/RoadizCoreBundle/src/Routing/RedirectionPathResolver.php b/lib/RoadizCoreBundle/src/Routing/RedirectionPathResolver.php index 7842a60d..d6947849 100644 --- a/lib/RoadizCoreBundle/src/Routing/RedirectionPathResolver.php +++ b/lib/RoadizCoreBundle/src/Routing/RedirectionPathResolver.php @@ -12,20 +12,13 @@ final class RedirectionPathResolver implements PathResolverInterface { - private ManagerRegistry $managerRegistry; - private Stopwatch $stopwatch; - private CacheItemPoolInterface $cacheAdapter; - public const CACHE_KEY = 'redirection_path_resolver_cache'; public function __construct( - ManagerRegistry $managerRegistry, - CacheItemPoolInterface $cacheAdapter, - Stopwatch $stopwatch, + private readonly ManagerRegistry $managerRegistry, + private readonly CacheItemPoolInterface $cacheAdapter, + private readonly Stopwatch $stopwatch, ) { - $this->managerRegistry = $managerRegistry; - $this->stopwatch = $stopwatch; - $this->cacheAdapter = $cacheAdapter; } public function resolvePath( diff --git a/lib/RoadizCoreBundle/src/Routing/RedirectionRouter.php b/lib/RoadizCoreBundle/src/Routing/RedirectionRouter.php index 1a5f94a2..1ec14a2e 100644 --- a/lib/RoadizCoreBundle/src/Routing/RedirectionRouter.php +++ b/lib/RoadizCoreBundle/src/Routing/RedirectionRouter.php @@ -7,6 +7,7 @@ use Doctrine\Persistence\ManagerRegistry; use Psr\Log\LoggerInterface; use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface; +use Symfony\Component\Routing\Exception\RouteNotFoundException; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RouteCollection; @@ -15,24 +16,13 @@ class RedirectionRouter extends Router implements VersatileGeneratorInterface { - protected ManagerRegistry $managerRegistry; - protected ?Stopwatch $stopwatch; - - /** - * @param RedirectionMatcher $matcher - * @param ManagerRegistry $managerRegistry - * @param array $options - * @param RequestContext|null $context - * @param LoggerInterface|null $logger - * @param Stopwatch|null $stopwatch - */ public function __construct( RedirectionMatcher $matcher, - ManagerRegistry $managerRegistry, + protected readonly ManagerRegistry $managerRegistry, + protected readonly Stopwatch $stopwatch, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, - Stopwatch $stopwatch = null ) { parent::__construct( new NullLoader(), @@ -41,8 +31,6 @@ public function __construct( $context, $logger ); - $this->stopwatch = $stopwatch; - $this->managerRegistry = $managerRegistry; $this->matcher = $matcher; } @@ -59,7 +47,7 @@ public function getRouteCollection(): RouteCollection */ public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string { - return ''; + throw new RouteNotFoundException(get_class($this) . ' does not support path generation.'); } /** @@ -70,11 +58,6 @@ public function getGenerator(): UrlGeneratorInterface throw new \BadMethodCallException(get_class($this) . ' does not support path generation.'); } - public function supports(mixed $name): bool - { - return false; - } - public function getRouteDebugMessage(mixed $name, array $parameters = []): string { return 'RedirectionRouter does not support path generation.'; diff --git a/lib/RoadizCoreBundle/src/Routing/StaticRouter.php b/lib/RoadizCoreBundle/src/Routing/StaticRouter.php deleted file mode 100644 index 4f9b917c..00000000 --- a/lib/RoadizCoreBundle/src/Routing/StaticRouter.php +++ /dev/null @@ -1,52 +0,0 @@ -routeCollection = $routeCollection; - } - - /** - * @return RouteCollection - */ - public function getRouteCollection(): RouteCollection - { - if (null === $this->collection) { - $this->routeCollection->parseResources(); - $this->collection = $this->routeCollection; - } - return $this->collection; - } -}