class HeadersCacheContext
Same name in other branches
- 8.9.x core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php \Drupal\Core\Cache\Context\HeadersCacheContext
- 10 core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php \Drupal\Core\Cache\Context\HeadersCacheContext
- 11.x core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php \Drupal\Core\Cache\Context\HeadersCacheContext
Defines the HeadersCacheContext service, for "per header" caching.
Cache context ID: 'headers' (to vary by all headers). Calculated cache context ID: 'headers:%name', e.g. 'headers:X-Something' (to vary by the 'X-Something' header).
Hierarchy
- class \Drupal\Core\Cache\Context\RequestStackCacheContextBase
- class \Drupal\Core\Cache\Context\HeadersCacheContext extends \Drupal\Core\Cache\Context\RequestStackCacheContextBase implements \Drupal\Core\Cache\Context\CalculatedCacheContextInterface
Expanded class hierarchy of HeadersCacheContext
1 file declares its use of HeadersCacheContext
- HeadersCacheContextTest.php in core/
tests/ Drupal/ Tests/ Core/ Cache/ Context/ HeadersCacheContextTest.php
1 string reference to 'HeadersCacheContext'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses HeadersCacheContext
File
-
core/
lib/ Drupal/ Core/ Cache/ Context/ HeadersCacheContext.php, line 14
Namespace
Drupal\Core\Cache\ContextView source
class HeadersCacheContext extends RequestStackCacheContextBase implements CalculatedCacheContextInterface {
/**
* {@inheritdoc}
*/
public static function getLabel() {
return t('HTTP headers');
}
/**
* {@inheritdoc}
*/
public function getContext($header = NULL) {
if ($header === NULL) {
$headers = $this->requestStack
->getCurrentRequest()->headers
->all();
// Order headers by name to have less cache variations.
ksort($headers);
$result = '';
foreach ($headers as $name => $value) {
if ($result) {
$result .= '&';
}
// Sort values to minimize cache variations.
sort($value);
$result .= $name . '=' . implode(',', $value);
}
return $result;
}
elseif ($this->requestStack
->getCurrentRequest()->headers
->has($header)) {
$value = $this->requestStack
->getCurrentRequest()->headers
->get($header);
if ($value !== '') {
return $value;
}
return '?valueless?';
}
return '';
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata($header = NULL) {
return new CacheableMetadata();
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
HeadersCacheContext::getCacheableMetadata | public | function | Gets the cacheability metadata for the context based on the parameter value. | Overrides CalculatedCacheContextInterface::getCacheableMetadata |
HeadersCacheContext::getContext | public | function | Returns the string representation of the cache context. | Overrides CalculatedCacheContextInterface::getContext |
HeadersCacheContext::getLabel | public static | function | Returns the label of the cache context. | Overrides CalculatedCacheContextInterface::getLabel |
RequestStackCacheContextBase::$requestStack | protected | property | The request stack. | |
RequestStackCacheContextBase::__construct | public | function | Constructs a new RequestStackCacheContextBase class. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.