SessionCacheContext.php 613 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace Drupal\Core\Cache\Context;
  3. use Drupal\Component\Utility\Crypt;
  4. /**
  5. * Defines the SessionCacheContext service, for "per session" caching.
  6. *
  7. * Cache context ID: 'session'.
  8. */
  9. class SessionCacheContext extends RequestStackCacheContextBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static function getLabel() {
  14. return t('Session');
  15. }
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function getContext() {
  20. $request = $this->requestStack->getCurrentRequest();
  21. if ($request->hasSession()) {
  22. return Crypt::hashBase64($request->getSession()->getId());
  23. }
  24. return 'none';
  25. }
  26. }