diff --git a/core/core.services.yml b/core/core.services.yml index 7443b8a..b4bd43a 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -506,9 +506,7 @@ services: - [setContext, ['@?router.request_context']] url_generator: class: Drupal\Core\Routing\CachedUrlGenerator - arguments: ['@url_generator.uncached', '@cache.url_generator', '@language_manager', '@router.route_provider'] - calls: - - [prepareCache, ['@request_stack']] + arguments: ['@url_generator.uncached', '@cache.url_generator', '@language_manager', '@router.route_provider', '@request_stack'] tags: - { name: needs_destruction } unrouted_url_assembler: diff --git a/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php b/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php index a253f4c..b235320 100644 --- a/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php +++ b/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php @@ -11,6 +11,7 @@ use Drupal\Core\DestructableInterface; use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageManagerInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Symfony\Cmf\Component\Routing\RouteProviderInterface as SymfonyRouteProviderInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Routing\Exception\RouteNotFoundException; @@ -37,18 +38,18 @@ class CachedUrlGenerator implements DestructableInterface, CachedUrlGeneratorInt protected $cache; /** - * Language manager for retrieving the URL language type. + * An array of cached URLs keyed by route name or path. * - * @var \Drupal\Core\Language\LanguageManagerInterface + * @var array */ - protected $languageManager; + protected $cachedUrls = array(); /** - * An array of cached URLs keyed by route name or path. + * The route provider. * - * @var array + * @var \Symfony\Cmf\Component\Routing\RouteProviderInterface */ - protected $cachedUrls = array(); + protected $routeProvider; /** * Whether the cache needs to be written. @@ -75,13 +76,6 @@ class CachedUrlGenerator implements DestructableInterface, CachedUrlGeneratorInt const PATH_CACHE_PREFIX = 'path::'; /** - * The route provider. - * - * @var \Symfony\Cmf\Component\Routing\RouteProviderInterface - */ - protected $routeProvider; - - /** * Constructs a \Drupal\Core\Routing\CachedUrlGenerator. * * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator @@ -92,12 +86,32 @@ class CachedUrlGenerator implements DestructableInterface, CachedUrlGeneratorInt * The language manager. * @param \Symfony\Cmf\Component\Routing\RouteProviderInterface $route_provider * The route provider. + * @param Symfony\Component\HttpFoundation\RequestStack + * The request stack. */ - public function __construct(UrlGeneratorInterface $url_generator, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, SymfonyRouteProviderInterface $route_provider) { + public function __construct(UrlGeneratorInterface $url_generator, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, SymfonyRouteProviderInterface $route_provider, RequestStack $request_stack) { $this->urlGenerator = $url_generator; $this->cache = $cache; - $this->languageManager = $language_manager; $this->routeProvider = $route_provider; + + // Select cache. + $this->cacheKey = $request_stack->getCurrentRequest()->attributes->get('_system_path'); + if ( is_null($this->cacheKey) ){ + $this->cacheKey = 'No match'; + } + if (!RequestHelper::isCleanUrl($request_stack->getCurrentRequest())){ + $this->cacheKey .= '::' . $request_stack->getCurrentRequest()->getScriptName(); + } + // Only multilingual sites have language dependant URLs. + if ($language_manager->isMultilingual()) { + $this->cacheKey .= '::' . $language_manager->getCurrentLanguage(Language::TYPE_URL)->getId(); + } + + // Retrieve stored URLs + $cached = $this->cache->get($this->cacheKey); + if ($cached) { + $this->cachedUrls = $cached->data; + } } /** @@ -189,27 +203,6 @@ protected function getRoute($name) { } /** - * Based on the current request load the appropiate set of cached URLs. - * - * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack - */ - public function prepareCache(RequestStack $requestStack) { - $this->cacheKey = $requestStack->getCurrentRequest()->attributes->get('_system_path'); - if (!RequestHelper::isCleanUrl($requestStack->getCurrentRequest())){ - $this->cacheKey .= '::' . $requestStack->getCurrentRequest()->getScriptName(); - } - // Only multilingual sites have language dependant URLs. - if ($this->languageManager->isMultilingual()) { - $this->cacheKey .= '::' . $this->languageManager->getCurrentLanguage(Language::TYPE_URL)->getId(); - } - - $cached = $this->cache->get($this->cacheKey); - if ($cached) { - $this->cachedUrls = $cached->data; - } - } - - /** * {@inheritdoc} */ public function supports($name) { diff --git a/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php b/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php index 3487410..20edd23 100644 --- a/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php @@ -63,11 +63,11 @@ class CachedUrlGeneratorTest extends UnitTestCase { protected $route; /** - * The log of all caches accessed. Keyed by cid. + *The request stack. * - * $var array + * @var \Symfony\Component\HttpFoundation\RequestStack */ - protected $cacheIds = array(); + protected $requestStack; /** * {@inheritdoc} @@ -85,7 +85,9 @@ protected function setUp() { $this->routeProvider->expects($this->any()) ->method('getRouteByName') ->willReturn($this->route); - $this->cachedUrlGenerator = new CachedUrlGenerator($this->urlGenerator, $this->cache, $this->languageManager, $this->routeProvider); + $this->requestStack = new RequestStack(); + $this->requestStack->push(new Request()); + $this->cachedUrlGenerator = new CachedUrlGenerator($this->urlGenerator, $this->cache, $this->languageManager, $this->routeProvider, $this->requestStack); } /**