From a8f43b08fdcc01381d48eaecb151ea38c4058489 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Wed, 21 May 2014 11:13:50 +0100 Subject: [PATCH] Issue #2236167 by znerol: Use request stack in cache contexts. --- core/core.services.yml | 4 ++-- .../Drupal/Core/Cache/ThemeCacheContext.php | 19 ++++++++++--------- .../lib/Drupal/Core/Cache/UrlCacheContext.php | 16 ++++++++-------- .../block/Tests/BlockViewBuilderTest.php | 5 ++++- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index 66b3d9bc786c..dbaf1e1f4089 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -9,7 +9,7 @@ services: arguments: ['@service_container', '%cache_contexts%' ] cache_context.url: class: Drupal\Core\Cache\UrlCacheContext - arguments: ['@request'] + arguments: ['@request_stack'] tags: - { name: cache.context} cache_context.language: @@ -19,7 +19,7 @@ services: - { name: cache.context} cache_context.theme: class: Drupal\Core\Cache\ThemeCacheContext - arguments: ['@request', '@theme.negotiator'] + arguments: ['@request_stack', '@theme.negotiator'] tags: - { name: cache.context} cache.backend.database: diff --git a/core/lib/Drupal/Core/Cache/ThemeCacheContext.php b/core/lib/Drupal/Core/Cache/ThemeCacheContext.php index d7774715039e..a11671b8feb3 100644 --- a/core/lib/Drupal/Core/Cache/ThemeCacheContext.php +++ b/core/lib/Drupal/Core/Cache/ThemeCacheContext.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Cache; -use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; use Drupal\Core\Theme\ThemeNegotiatorInterface; /** @@ -16,11 +16,11 @@ class ThemeCacheContext implements CacheContextInterface { /** - * The current request. + * The request stack. * - * @var \Symfony\Component\HttpFoundation\Request + * @var \Symfony\Component\HttpFoundation\RequestStack */ - protected $request; + protected $requestStack; /** * The theme negotiator. @@ -32,13 +32,13 @@ class ThemeCacheContext implements CacheContextInterface { /** * Constructs a new ThemeCacheContext service. * - * @param \Symfony\Component\HttpFoundation\Request $request - * The HTTP request object. + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack + * The request stack. * @param \Drupal\Core\Theme\ThemeNegotiatorInterface $theme_negotiator * The theme negotiator. */ - public function __construct(Request $request, ThemeNegotiatorInterface $theme_negotiator) { - $this->request = $request; + public function __construct(RequestStack $request_stack, ThemeNegotiatorInterface $theme_negotiator) { + $this->requestStack = $request_stack; $this->themeNegotiator = $theme_negotiator; } @@ -53,7 +53,8 @@ public static function getLabel() { * {@inheritdoc} */ public function getContext() { - return $this->themeNegotiator->determineActiveTheme($this->request) ?: 'stark'; + $request = $this->requestStack->getCurrentRequest(); + return $this->themeNegotiator->determineActiveTheme($request) ?: 'stark'; } } diff --git a/core/lib/Drupal/Core/Cache/UrlCacheContext.php b/core/lib/Drupal/Core/Cache/UrlCacheContext.php index 596e1348e18a..9c9367f935ca 100644 --- a/core/lib/Drupal/Core/Cache/UrlCacheContext.php +++ b/core/lib/Drupal/Core/Cache/UrlCacheContext.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Cache; -use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; /** * Defines the UrlCacheContext service, for "per page" caching. @@ -15,20 +15,20 @@ class UrlCacheContext implements CacheContextInterface { /** - * The current request. + * The request stack. * - * @var \Symfony\Component\HttpFoundation\Request + * @var \Symfony\Component\HttpFoundation\RequestStack */ protected $request; /** * Constructs a new UrlCacheContext service. * - * @param \Symfony\Component\HttpFoundation\Request $request - * The HTTP request object. + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack + * The request stack. */ - public function __construct(Request $request) { - $this->request = $request; + public function __construct(RequestStack $request_stack) { + $this->requestStack = $request_stack; } /** @@ -42,7 +42,7 @@ public static function getLabel() { * {@inheritdoc} */ public function getContext() { - return $this->request->getUri(); + return $this->requestStack->getCurrentRequest()->getUri(); } } diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockViewBuilderTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockViewBuilderTest.php index feb54712f61f..a6571cb9a893 100644 --- a/core/modules/block/lib/Drupal/block/Tests/BlockViewBuilderTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/BlockViewBuilderTest.php @@ -11,6 +11,7 @@ use Drupal\Core\Cache\UrlCacheContext; use Drupal\simpletest\DrupalUnitTestBase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; /** * Tests the block view builder. @@ -302,7 +303,9 @@ public function testBlockViewBuilderCacheContexts() { // Third: the same block configuration, but a different URL. $original_url_cache_context = $this->container->get('cache_context.url'); - $temp_context = new UrlCacheContext(Request::create('/foo')); + $request_stack = new RequestStack(); + $request_stack->push(Request::create('/foo')); + $temp_context = new UrlCacheContext($request_stack); $this->container->set('cache_context.url', $temp_context); $old_cid = $cid; $build = $this->getBlockRenderArray(); -- GitLab