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

8.0.x
Nathaniel Catchpole 2014-05-21 11:13:50 +01:00
parent 1f5ad10030
commit a8f43b08fd
4 changed files with 24 additions and 20 deletions

View File

@ -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:

View File

@ -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 @@ use Drupal\Core\Theme\ThemeNegotiatorInterface;
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 @@ class ThemeCacheContext implements CacheContextInterface {
* {@inheritdoc}
*/
public function getContext() {
return $this->themeNegotiator->determineActiveTheme($this->request) ?: 'stark';
$request = $this->requestStack->getCurrentRequest();
return $this->themeNegotiator->determineActiveTheme($request) ?: 'stark';
}
}

View File

@ -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 @@ use Symfony\Component\HttpFoundation\Request;
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 @@ class UrlCacheContext implements CacheContextInterface {
* {@inheritdoc}
*/
public function getContext() {
return $this->request->getUri();
return $this->requestStack->getCurrentRequest()->getUri();
}
}

View File

@ -11,6 +11,7 @@ use Drupal\Component\Utility\NestedArray;
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 @@ class BlockViewBuilderTest extends DrupalUnitTestBase {
// 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();