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%' ] arguments: ['@service_container', '%cache_contexts%' ]
cache_context.url: cache_context.url:
class: Drupal\Core\Cache\UrlCacheContext class: Drupal\Core\Cache\UrlCacheContext
arguments: ['@request'] arguments: ['@request_stack']
tags: tags:
- { name: cache.context} - { name: cache.context}
cache_context.language: cache_context.language:
@ -19,7 +19,7 @@ services:
- { name: cache.context} - { name: cache.context}
cache_context.theme: cache_context.theme:
class: Drupal\Core\Cache\ThemeCacheContext class: Drupal\Core\Cache\ThemeCacheContext
arguments: ['@request', '@theme.negotiator'] arguments: ['@request_stack', '@theme.negotiator']
tags: tags:
- { name: cache.context} - { name: cache.context}
cache.backend.database: cache.backend.database:

View File

@ -7,7 +7,7 @@
namespace Drupal\Core\Cache; namespace Drupal\Core\Cache;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Theme\ThemeNegotiatorInterface; use Drupal\Core\Theme\ThemeNegotiatorInterface;
/** /**
@ -16,11 +16,11 @@ use Drupal\Core\Theme\ThemeNegotiatorInterface;
class ThemeCacheContext implements CacheContextInterface { 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. * The theme negotiator.
@ -32,13 +32,13 @@ class ThemeCacheContext implements CacheContextInterface {
/** /**
* Constructs a new ThemeCacheContext service. * Constructs a new ThemeCacheContext service.
* *
* @param \Symfony\Component\HttpFoundation\Request $request * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The HTTP request object. * The request stack.
* @param \Drupal\Core\Theme\ThemeNegotiatorInterface $theme_negotiator * @param \Drupal\Core\Theme\ThemeNegotiatorInterface $theme_negotiator
* The theme negotiator. * The theme negotiator.
*/ */
public function __construct(Request $request, ThemeNegotiatorInterface $theme_negotiator) { public function __construct(RequestStack $request_stack, ThemeNegotiatorInterface $theme_negotiator) {
$this->request = $request; $this->requestStack = $request_stack;
$this->themeNegotiator = $theme_negotiator; $this->themeNegotiator = $theme_negotiator;
} }
@ -53,7 +53,8 @@ class ThemeCacheContext implements CacheContextInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getContext() { 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; namespace Drupal\Core\Cache;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack;
/** /**
* Defines the UrlCacheContext service, for "per page" caching. * Defines the UrlCacheContext service, for "per page" caching.
@ -15,20 +15,20 @@ use Symfony\Component\HttpFoundation\Request;
class UrlCacheContext implements CacheContextInterface { class UrlCacheContext implements CacheContextInterface {
/** /**
* The current request. * The request stack.
* *
* @var \Symfony\Component\HttpFoundation\Request * @var \Symfony\Component\HttpFoundation\RequestStack
*/ */
protected $request; protected $request;
/** /**
* Constructs a new UrlCacheContext service. * Constructs a new UrlCacheContext service.
* *
* @param \Symfony\Component\HttpFoundation\Request $request * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The HTTP request object. * The request stack.
*/ */
public function __construct(Request $request) { public function __construct(RequestStack $request_stack) {
$this->request = $request; $this->requestStack = $request_stack;
} }
/** /**
@ -42,7 +42,7 @@ class UrlCacheContext implements CacheContextInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getContext() { 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\Core\Cache\UrlCacheContext;
use Drupal\simpletest\DrupalUnitTestBase; use Drupal\simpletest\DrupalUnitTestBase;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/** /**
* Tests the block view builder. * Tests the block view builder.
@ -302,7 +303,9 @@ class BlockViewBuilderTest extends DrupalUnitTestBase {
// Third: the same block configuration, but a different URL. // Third: the same block configuration, but a different URL.
$original_url_cache_context = $this->container->get('cache_context.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); $this->container->set('cache_context.url', $temp_context);
$old_cid = $cid; $old_cid = $cid;
$build = $this->getBlockRenderArray(); $build = $this->getBlockRenderArray();