diff --git a/core/core.services.yml b/core/core.services.yml index 66b3d9bc786cf58619ef001c510573a43bcf4c2d..dbaf1e1f40895514308d13cc0cb12def75ce447f 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 d7774715039ea70bbb1cfe610cb16d9d0d5be896..a11671b8feb37627623191f8490989a44f052dd4 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 596e1348e18a0445b62dfaa0effaa4f63b734336..9c9367f935ca541a029a8e7c8f3a94c6d88ac4b2 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 feb54712f61f5e7f3eaf07f10389fc6d58bd5d35..a6571cb9a89399a6c263adef751b983ff770d896 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();