HeadersCacheContext.php in Drupal 8
File
core/lib/Drupal/Core/Cache/Context/HeadersCacheContext.php
View source
<?php
namespace Drupal\Core\Cache\Context;
use Drupal\Core\Cache\CacheableMetadata;
class HeadersCacheContext extends RequestStackCacheContextBase implements CalculatedCacheContextInterface {
public static function getLabel() {
return t('HTTP headers');
}
public function getContext($header = NULL) {
if ($header === NULL) {
$headers = $this->requestStack
->getCurrentRequest()->headers
->all();
ksort($headers);
$result = '';
foreach ($headers as $name => $value) {
if ($result) {
$result .= '&';
}
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 '';
}
public function getCacheableMetadata($header = NULL) {
return new CacheableMetadata();
}
}