NoSessionOpen.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Drupal\Core\PageCache\RequestPolicy;
  3. use Drupal\Core\PageCache\RequestPolicyInterface;
  4. use Drupal\Core\Session\SessionConfigurationInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. /**
  7. * A policy allowing delivery of cached pages when there is no session open.
  8. *
  9. * Do not serve cached pages to authenticated users, or to anonymous users when
  10. * $_SESSION is non-empty. $_SESSION may contain status messages from a form
  11. * submission, the contents of a shopping cart, or other user-specific content
  12. * that should not be cached and displayed to other users.
  13. */
  14. class NoSessionOpen implements RequestPolicyInterface {
  15. /**
  16. * The session configuration.
  17. *
  18. * @var \Drupal\Core\Session\SessionConfigurationInterface
  19. */
  20. protected $sessionConfiguration;
  21. /**
  22. * Constructs a new page cache session policy.
  23. *
  24. * @param \Drupal\Core\Session\SessionConfigurationInterface $session_configuration
  25. * The session configuration.
  26. */
  27. public function __construct(SessionConfigurationInterface $session_configuration) {
  28. $this->sessionConfiguration = $session_configuration;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function check(Request $request) {
  34. if (!$this->sessionConfiguration->hasSession($request)) {
  35. return static::ALLOW;
  36. }
  37. }
  38. }