Commit a8f43b08 authored by catch's avatar catch

Issue #2236167 by znerol: Use request stack in cache contexts.

parent 1f5ad100
......@@ -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:
......
......@@ -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';
}
}
......@@ -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();
}
}
......@@ -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();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment