Skip to content

Commit

Permalink
feat(CoreBundle): Refactored node routing
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Jan 24, 2024
1 parent 6d4a3c3 commit 35b8bc2
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 359 deletions.
56 changes: 9 additions & 47 deletions src/Event/NodesSources/NodesSourcesPathGeneratingEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
13 changes: 3 additions & 10 deletions src/EventSubscriber/NodeSourcePathSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

/**
Expand All @@ -28,7 +23,6 @@ public static function getSubscribedEvents(): array
{
return [
NodesSourcesPathGeneratingEvent::class => [['onNodesSourcesPath', -100]],
'\RZ\Roadiz\Core\Events\NodesSources\NodesSourcesPathGeneratingEvent' => [['onNodesSourcesPath', -100]],
];
}

Expand All @@ -39,7 +33,6 @@ public function onNodesSourcesPath(NodesSourcesPathGeneratingEvent $event): void
{
$urlGenerator = new NodesSourcesUrlGenerator(
$this->pathAggregator,
null,
$event->getNodeSource(),
$event->isForceLocale(),
$event->isForceLocaleWithUrlAlias()
Expand Down
29 changes: 0 additions & 29 deletions src/Routing/DeferredRouteCollection.php

This file was deleted.

20 changes: 3 additions & 17 deletions src/Routing/DynamicUrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace RZ\Roadiz\CoreBundle\Routing;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
Expand All @@ -18,25 +17,12 @@
*/
abstract class DynamicUrlMatcher extends UrlMatcher
{
protected Stopwatch $stopwatch;
protected LoggerInterface $logger;
protected PreviewResolverInterface $previewResolver;

/**
* @param RequestContext $context
* @param PreviewResolverInterface $previewResolver
* @param Stopwatch $stopwatch
* @param LoggerInterface|null $logger
*/
public function __construct(
RequestContext $context,
PreviewResolverInterface $previewResolver,
Stopwatch $stopwatch,
?LoggerInterface $logger = null
protected readonly PreviewResolverInterface $previewResolver,
protected readonly Stopwatch $stopwatch,
protected readonly LoggerInterface $logger
) {
parent::__construct(new RouteCollection(), $context);
$this->stopwatch = $stopwatch;
$this->logger = $logger ?? new NullLogger();
$this->previewResolver = $previewResolver;
}
}
79 changes: 18 additions & 61 deletions src/Routing/NodeRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 = []
Expand All @@ -57,10 +45,7 @@ public function __construct(
$context,
$logger
);
$this->settingsBag = $settingsBag;
$this->eventDispatcher = $eventDispatcher;
$this->matcher = $matcher;
$this->nodeSourceUrlCacheAdapter = $nodeSourceUrlCacheAdapter;
}

/**
Expand All @@ -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
*/
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
46 changes: 18 additions & 28 deletions src/Routing/NodeUrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbstractController>
* @param PathResolverInterface $pathResolver
* @param RequestContext $context
* @param PreviewResolverInterface $previewResolver
* @param Stopwatch $stopwatch
* @param LoggerInterface $logger
* @param class-string<AbstractController> $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
Expand All @@ -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<AbstractController> $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
{
Expand All @@ -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
{
Expand Down
Loading

0 comments on commit 35b8bc2

Please sign in to comment.