vendor/contao/core-bundle/src/Session/LazySessionAccess.php line 55

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Session;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. /**
  13.  * Automatically starts the session if someone accesses $_SESSION.
  14.  *
  15.  * @internal
  16.  */
  17. class LazySessionAccess implements \ArrayAccess, \Countable
  18. {
  19.     /**
  20.      * @var SessionInterface
  21.      */
  22.     private $session;
  23.     /**
  24.      * @var bool
  25.      */
  26.     private $hasPreviousSession;
  27.     public function __construct(SessionInterface $sessionbool $hasPreviousSession true)
  28.     {
  29.         $this->session $session;
  30.         $this->hasPreviousSession $hasPreviousSession;
  31.     }
  32.     #[\ReturnTypeWillChange]
  33.     public function offsetExists($offset): bool
  34.     {
  35.         if (!$this->hasPreviousSession && !$this->session->isStarted()) {
  36.             return false;
  37.         }
  38.         $this->startSession();
  39.         return \array_key_exists($offset$_SESSION);
  40.     }
  41.     #[\ReturnTypeWillChange]
  42.     public function &offsetGet($offset)
  43.     {
  44.         $this->startSession();
  45.         return $_SESSION[$offset];
  46.     }
  47.     #[\ReturnTypeWillChange]
  48.     public function offsetSet($offset$value): void
  49.     {
  50.         $this->startSession();
  51.         $_SESSION[$offset] = $value;
  52.     }
  53.     #[\ReturnTypeWillChange]
  54.     public function offsetUnset($offset): void
  55.     {
  56.         $this->startSession();
  57.         unset($_SESSION[$offset]);
  58.     }
  59.     #[\ReturnTypeWillChange]
  60.     public function count(): int
  61.     {
  62.         if (!$this->hasPreviousSession && !$this->session->isStarted()) {
  63.             return 0;
  64.         }
  65.         $this->startSession();
  66.         return \count($_SESSION);
  67.     }
  68.     /**
  69.      * @throws \RuntimeException
  70.      */
  71.     private function startSession(): void
  72.     {
  73.         trigger_deprecation('contao/core-bundle''4.5''Using "$_SESSION" has been deprecated and will no longer work in Contao 5.0. Use the Symfony session instead.');
  74.         $this->session->start();
  75.         /** @phpstan-ignore-next-line */
  76.         if ($_SESSION instanceof self) {
  77.             throw new \RuntimeException('Unable to start the native session, $_SESSION was not replaced.');
  78.         }
  79.         // Accessing the session object may replace the global $_SESSION variable,
  80.         // so we store the bags in a local variable first before setting them on $_SESSION
  81.         $beBag $this->session->getBag('contao_backend');
  82.         $feBag $this->session->getBag('contao_frontend');
  83.         $_SESSION['BE_DATA'] = $beBag;
  84.         $_SESSION['FE_DATA'] = $feBag;
  85.     }
  86. }