3 namespace Drupal\Core\Cache\Context;
5 use Drupal\Core\Cache\CacheableMetadata;
8 * Defines the HeadersCacheContext service, for "per header" caching.
10 * Cache context ID: 'headers' (to vary by all headers).
11 * Calculated cache context ID: 'headers:%name', e.g. 'headers:X-Something' (to
12 * vary by the 'X-Something' header).
14 class HeadersCacheContext extends RequestStackCacheContextBase implements CalculatedCacheContextInterface {
19 public static function getLabel() {
20 return t('HTTP headers');
26 public function getContext($header = NULL) {
27 if ($header === NULL) {
28 $headers = $this->requestStack->getCurrentRequest()->headers->all();
29 // Order headers by name to have less cache variations.
32 foreach ($headers as $name => $value) {
36 // Sort values to minimize cache variations.
38 $result .= $name . '=' . implode(',', $value);
42 elseif ($this->requestStack->getCurrentRequest()->headers->has($header)) {
43 $value = $this->requestStack->getCurrentRequest()->headers->get($header);
55 public function getCacheableMetadata($header = NULL) {
56 return new CacheableMetadata();