1<?php 2 3namespace Drupal\Core\Cache\Context; 4 5use Drupal\Core\Cache\CacheableMetadata; 6 7/** 8 * Defines the HeadersCacheContext service, for "per header" caching. 9 * 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). 13 */ 14class HeadersCacheContext extends RequestStackCacheContextBase implements CalculatedCacheContextInterface { 15 16 /** 17 * {@inheritdoc} 18 */ 19 public static function getLabel() { 20 return t('HTTP headers'); 21 } 22 23 /** 24 * {@inheritdoc} 25 */ 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. 30 ksort($headers); 31 $result = ''; 32 foreach ($headers as $name => $value) { 33 if ($result) { 34 $result .= '&'; 35 } 36 // Sort values to minimize cache variations. 37 sort($value); 38 $result .= $name . '=' . implode(',', $value); 39 } 40 return $result; 41 } 42 elseif ($this->requestStack->getCurrentRequest()->headers->has($header)) { 43 $value = $this->requestStack->getCurrentRequest()->headers->get($header); 44 if ($value !== '') { 45 return $value; 46 } 47 return '?valueless?'; 48 } 49 return ''; 50 } 51 52 /** 53 * {@inheritdoc} 54 */ 55 public function getCacheableMetadata($header = NULL) { 56 return new CacheableMetadata(); 57 } 58 59} 60