Skip to content

Commit

Permalink
feat(Redirections): Added cache adapter to look for redirection query
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jun 15, 2023
1 parent 3a6a38b commit 18da77c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lib/RoadizCoreBundle/src/Routing/NodesSourcesPathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function resolvePath(
}

if ($path === '/') {
$this->stopwatch->start('parseRootPath');
$this->stopwatch->start('parseRootPath', 'routing');
$translation = $this->parseTranslation();
$nodeSource = $this->getHome($translation);
$this->stopwatch->stop('parseRootPath');
Expand Down Expand Up @@ -96,13 +96,13 @@ public function resolvePath(
}
}

$this->stopwatch->start('parseTranslation');
$this->stopwatch->start('parseTranslation', 'routing');
$translation = $this->parseTranslation($tokens);
$this->stopwatch->stop('parseTranslation');
/*
* Try with URL Aliases OR nodeName
*/
$this->stopwatch->start('parseFromIdentifier');
$this->stopwatch->start('parseFromIdentifier', 'routing');
$nodeSource = $this->parseFromIdentifier($tokens, $translation, $allowNonReachableNodes);
$this->stopwatch->stop('parseFromIdentifier');
}
Expand Down
24 changes: 5 additions & 19 deletions lib/RoadizCoreBundle/src/Routing/RedirectionMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,51 @@

namespace RZ\Roadiz\CoreBundle\Routing;

use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use RZ\Roadiz\CoreBundle\Controller\RedirectionController;
use RZ\Roadiz\CoreBundle\Entity\Redirection;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Stopwatch\Stopwatch;

/**
* UrlMatcher which tries to grab Node and Translation
* information for a route.
*/
final class RedirectionMatcher extends UrlMatcher
{
private ManagerRegistry $managerRegistry;
private Stopwatch $stopwatch;
private LoggerInterface $logger;
private RedirectionPathResolver $pathResolver;

/**
* @param RequestContext $context
* @param ManagerRegistry $managerRegistry
* @param Stopwatch $stopwatch
* @param LoggerInterface $logger
*/
public function __construct(
RequestContext $context,
ManagerRegistry $managerRegistry,
Stopwatch $stopwatch,
RedirectionPathResolver $pathResolver,
LoggerInterface $logger
) {
parent::__construct(new RouteCollection(), $context);
$this->stopwatch = $stopwatch;
$this->logger = $logger;
$this->managerRegistry = $managerRegistry;
$this->pathResolver = $pathResolver;
}

/**
* {@inheritdoc}
*/
public function match(string $pathinfo): array
{
$this->stopwatch->start('findRedirection');
$decodedUrl = rawurldecode($pathinfo);

/*
* Try nodes routes
*/
if (null !== $redirection = $this->matchRedirection($decodedUrl)) {
$this->logger->debug('Matched redirection.', ['query' => $redirection->getQuery()]);
$this->stopwatch->stop('findRedirection');
return [
'_controller' => RedirectionController::class . '::redirectAction',
'redirection' => $redirection,
'_route' => null,
];
}
$this->stopwatch->stop('findRedirection');

throw new ResourceNotFoundException(sprintf('%s did not match any Doctrine Redirection', $pathinfo));
}
Expand All @@ -74,6 +59,7 @@ public function match(string $pathinfo): array
*/
protected function matchRedirection(string $decodedUrl): ?Redirection
{
return $this->managerRegistry->getRepository(Redirection::class)->findOneByQuery($decodedUrl);
$resource = $this->pathResolver->resolvePath($decodedUrl)->getResource();
return $resource instanceof Redirection ? $resource : null;
}
}
51 changes: 44 additions & 7 deletions lib/RoadizCoreBundle/src/Routing/RedirectionPathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@
namespace RZ\Roadiz\CoreBundle\Routing;

use Doctrine\Persistence\ManagerRegistry;
use Psr\Cache\CacheItemPoolInterface;
use RZ\Roadiz\CoreBundle\Entity\Redirection;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Stopwatch\Stopwatch;

final class RedirectionPathResolver implements PathResolverInterface
{
private ManagerRegistry $managerRegistry;
private Stopwatch $stopwatch;
private CacheItemPoolInterface $cacheAdapter;

public function __construct(ManagerRegistry $managerRegistry)
{
public const CACHE_KEY = 'redirection_path_resolver_cache';

public function __construct(
ManagerRegistry $managerRegistry,
CacheItemPoolInterface $cacheAdapter,
Stopwatch $stopwatch,
) {
$this->managerRegistry = $managerRegistry;
$this->stopwatch = $stopwatch;
$this->cacheAdapter = $cacheAdapter;
}

public function resolvePath(
Expand All @@ -23,15 +34,41 @@ public function resolvePath(
bool $allowRootPaths = false,
bool $allowNonReachableNodes = true
): ResourceInfo {
$redirection = $this->managerRegistry
->getRepository(Redirection::class)
->findOneByQuery($path);
$this->stopwatch->start('findRedirection', 'routing');
$cacheItem = $this->cacheAdapter->getItem(self::CACHE_KEY);
if (!$cacheItem->isHit()) {
// Populate cache item
/** @var array[] $redirections */
$redirections = $this->managerRegistry
->getRepository(Redirection::class)
->createQueryBuilder('r')
->select(['r.id', 'r.query'])
->getQuery()
->getArrayResult();
$redirections = array_combine(
array_column($redirections, 'query'),
array_column($redirections, 'id')
);
$cacheItem->set($redirections);
$this->cacheAdapter->save($cacheItem);
} else {
/** @var array[] $redirections */
$redirections = $cacheItem->get();
}

/** @var int|null $redirectionId */
$redirectionId = $redirections[$path] ?? null;
$this->stopwatch->stop('findRedirection');

if (null === $redirection) {
if (null === $redirectionId) {
throw new ResourceNotFoundException();
}

return (new ResourceInfo())
->setResource($redirection);
->setResource(
$this->managerRegistry
->getRepository(Redirection::class)
->find($redirectionId)
);
}
}

0 comments on commit 18da77c

Please sign in to comment.