CookiesCacheContext.php 970 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace Drupal\Core\Cache\Context;
  3. use Drupal\Core\Cache\CacheableMetadata;
  4. /**
  5. * Defines the CookiesCacheContext service, for "per cookie" caching.
  6. *
  7. * Cache context ID: 'cookies' (to vary by all cookies).
  8. * Calculated cache context ID: 'cookies:%name', e.g. 'cookies:device_type' (to
  9. * vary by the 'device_type' cookie).
  10. */
  11. class CookiesCacheContext extends RequestStackCacheContextBase implements CalculatedCacheContextInterface {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public static function getLabel() {
  16. return t('HTTP cookies');
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getContext($cookie = NULL) {
  22. if ($cookie === NULL) {
  23. return $this->requestStack->getCurrentRequest()->cookies->all();
  24. }
  25. else {
  26. return $this->requestStack->getCurrentRequest()->cookies->get($cookie);
  27. }
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getCacheableMetadata($cookie = NULL) {
  33. return new CacheableMetadata();
  34. }
  35. }