diff --git a/core/core.services.yml b/core/core.services.yml index 298ccd7..5e43cf9 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -681,11 +681,17 @@ services: arguments: ['@route_filter.lazy_collector'] tags: - { name: event_subscriber } - url_generator: + url_generator.uncached: class: Drupal\Core\Routing\UrlGenerator + public: false arguments: ['@router.route_provider', '@path_processor_manager', '@route_processor_manager', '@config.factory', '@logger.channel.default', '@request_stack'] calls: - [setContext, ['@?router.request_context']] + url_generator: + class: Drupal\Core\Routing\CachedUrlGenerator + arguments: ['@url_generator.uncached', '@cache.url_generator', '@router.route_provider', '@request_stack'] + tags: + - { name: needs_destruction } redirect.destination: class: Drupal\Core\Routing\RedirectDestination arguments: ['@request_stack', '@url_generator'] diff --git a/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php b/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php index 805c32b..d30b156 100644 --- a/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php +++ b/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php @@ -24,7 +24,7 @@ class CachedUrlGenerator implements DestructableInterface, CachedUrlGeneratorInterface { /** - * The wrapped URL generator + * The wrapped URL generator. * * @var \Drupal\Core\Routing\UrlGeneratorInterface */ @@ -81,12 +81,12 @@ class CachedUrlGenerator implements DestructableInterface, CachedUrlGeneratorInt * Constructs a \Drupal\Core\Routing\CachedUrlGenerator. * * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator - * The wrapped URL generator + * The wrapped URL generator. * @param \Drupal\Core\Cache\CacheBackendInterface $cache * The cache backend. * @param \Symfony\Cmf\Component\Routing\RouteProviderInterface $route_provider * The route provider. - * @param Symfony\Component\HttpFoundation\RequestStack + * @param Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. */ public function __construct(UrlGeneratorInterface $url_generator, CacheBackendInterface $cache, SymfonyRouteProviderInterface $route_provider, RequestStack $request_stack) { @@ -114,7 +114,7 @@ public function clearCache() { foreach (array_keys($this->cachedUrls) as $cache_key) { $this->cache->delete($cache_key); } - $this->cachedUrls = array(); + $this->cachedUrls = []; } /** @@ -133,10 +133,10 @@ protected function writeCache() { /** * {@inheritdoc} */ - public function generate($name, $parameters = array(), $absolute = FALSE) { - $options = array(); - // We essentially inline the implementation from the Drupal UrlGenerator - // and avoid setting $options so that we increase the likelihood of caching. + public function generate($name, $parameters = [], $absolute = FALSE) { + $options = []; + // We essentially inline the implementation from the Drupal UrlGenerator and + // avoid setting $options so that we increase the likelihood of caching. if ($absolute) { $options['absolute'] = $absolute; } @@ -146,7 +146,7 @@ public function generate($name, $parameters = array(), $absolute = FALSE) { /** * {@inheritdoc} */ - public function generateFromPath($path = NULL, $options = array()) { + public function generateFromPath($path = NULL, $options = []) { $key = static::PATH_CACHE_PREFIX . hash('sha256', $path . serialize($options)); $cache_key = $this->requestStack->getCurrentRequest()->getUri(); if (!isset($this->cachedUrls[$cache_key][$key])) { @@ -159,7 +159,7 @@ public function generateFromPath($path = NULL, $options = array()) { /** * {@inheritdoc} */ - public function generateFromRoute($name, $parameters = array(), $options = array()) { + public function generateFromRoute($name, $parameters = [], $options = []) { // In some cases $name may be a Route object, rather than a string. $key = static::ROUTE_CACHE_PREFIX . hash('sha256', serialize($name) . serialize($options) . serialize($parameters)); $cache_key = $this->requestStack->getCurrentRequest()->getUri(); @@ -181,7 +181,7 @@ public function generateFromRoute($name, $parameters = array(), $options = array * Find the route using the provided route name. * * @param string $name - * The route name to fetch + * The route name to fetch. * * @return \Symfony\Component\Routing\Route * The found route. @@ -211,7 +211,7 @@ public function supports($name) { /** * {@inheritdoc} */ - public function getRouteDebugMessage($name, array $parameters = array()) { + public function getRouteDebugMessage($name, array $parameters = []) { return $this->urlGenerator->getRouteDebugMessage($name, $parameters); } @@ -239,7 +239,7 @@ public function getContext() { /** * {@inheritdoc} */ - public function getPathFromRoute($name, $parameters = array()) { + public function getPathFromRoute($name, $parameters = []) { return $this->urlGenerator->getPathFromRoute($name, $parameters); } diff --git a/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php b/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php index 14fafce..07defec 100644 --- a/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php @@ -106,13 +106,14 @@ public function testGenerate() { } /** - * Tests the generate method with the same route name but different parameters. + * Tests the generate method with the same route name but different + * parameters. * * @see \Drupal\Core\Routing\CachedUrlGenerator::generate() */ public function testGenerateWithDifferentParameters() { - // We are generating URL's four times but since two of them are cached, - // we expect that generateFromRoute will only be called twice. + // We are generating URLs four times but since two of them are cached, we + // expect that generateFromRoute will only be called twice. $this->urlGenerator->expects($this->exactly(2)) ->method('generateFromRoute') ->will($this->returnValueMap(array( @@ -125,8 +126,8 @@ public function testGenerateWithDifferentParameters() { // Second call will fetch from the cache. $this->assertEquals('test-route-1/value1', $this->cachedUrlGenerator->generate('test_route', array('key' => 'value1'))); // Third call uses the same route but since the parameters are different, - // the cache is not used and the generateFromRoute method will be called - // for the second time. + // the cache is not used and the generateFromRoute method will be called for + // the second time. $this->assertEquals('test-route-1/value2', $this->cachedUrlGenerator->generate('test_route', array('key' => 'value2'))); // Fourth call will fetch from the cache as it is now primed. $this->assertEquals('test-route-1/value2', $this->cachedUrlGenerator->generate('test_route', array('key' => 'value2'))); @@ -150,13 +151,13 @@ public function testGenerateFromPath() { } /** - * Tests the generate method with the same path but different options + * Tests the generate method with the same path but different options. * * @see \Drupal\Core\Routing\CachedUrlGenerator::generateFromPath() */ public function testGenerateFromPathWithDifferentParameters() { - // We are generating URL's six times but since three of them are cached, - // we expect that generateFromPath will only be called three times. + // We are generating URLs six times but since three of them are cached, we + // expect that generateFromPath will only be called three times. $this->urlGenerator->expects($this->exactly(3)) ->method('generateFromPath') ->will($this->returnValueMap(array( @@ -170,8 +171,8 @@ public function testGenerateFromPathWithDifferentParameters() { // Second call will fetch from cache. $this->assertEquals('http://localhost/test-route-1', $this->cachedUrlGenerator->generateFromPath('test-route-1', array('absolute' => TRUE))); // Third call uses the same path but since the options are different, the - // cache is not used and the generateFromPath method will be called for - // the second time. + // cache is not used and the generateFromPath method will be called for the + // second time. $this->assertEquals('test-route-1', $this->cachedUrlGenerator->generateFromPath('test-route-1', array('absolute' => FALSE))); // Fourth call will fetch from cache. $this->assertEquals('test-route-1', $this->cachedUrlGenerator->generateFromPath('test-route-1', array('absolute' => FALSE))); @@ -207,8 +208,8 @@ public function testGenerateFromRoute() { * @see \Drupal\Core\Routing\CachedUrlGenerator::generateFromRoute() */ public function testGenerateFromRouteWithDifferentParameters() { - // We are generating URL's eight times but since two of them are cached, - // we expect that generateFromRoute will only be called four times. + // We are generating URLs eight times but since two of them are cached, we + // expect that generateFromRoute will only be called four times. $this->urlGenerator->expects($this->exactly(4)) ->method('generateFromRoute') ->will($this->returnValueMap(array( @@ -223,8 +224,8 @@ public function testGenerateFromRouteWithDifferentParameters() { // Second call will fetch from the cache. $this->assertEquals('test-route-1/value1', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value1'))); // Third call uses the same route but since the options are different, the - // cache is not used and the generateFromRoute method will be called for - // the second time. + // cache is not used and the generateFromRoute method will be called for the + // second time. $this->assertEquals('http://localhost/test-route-1/value1', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value1'), array('absolute' => TRUE))); // Fourth call will fetch from the cache. $this->assertEquals('http://localhost/test-route-1/value1', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value1'), array('absolute' => TRUE))); @@ -235,8 +236,8 @@ public function testGenerateFromRouteWithDifferentParameters() { // Sixth call will fetch from the cache. $this->assertEquals('test-route-1/value2', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value2'))); // Seventh call uses the same route but since the options are different, the - // cache is not used and the generateFromRoute method will be called for - // the fourth time. + // cache is not used and the generateFromRoute method will be called for the + // fourth time. $this->assertEquals('http://localhost/test-route-1/value2', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value2'), array('absolute' => TRUE))); // Eighth call will fetch from the cache. $this->assertEquals('http://localhost/test-route-1/value2', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value2'), array('absolute' => TRUE)));