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

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.  * @implements \ArrayAccess<mixed, mixed>
  17.  */
  18. class LazySessionAccess implements \ArrayAccess\Countable
  19. {
  20.     private SessionInterface $session;
  21.     private bool $hasPreviousSession;
  22.     public function __construct(SessionInterface $sessionbool $hasPreviousSession true)
  23.     {
  24.         $this->session $session;
  25.         $this->hasPreviousSession $hasPreviousSession;
  26.     }
  27.     /**
  28.      * @param mixed $offset
  29.      */
  30.     public function offsetExists($offset): bool
  31.     {
  32.         if (!$this->hasPreviousSession && !$this->session->isStarted()) {
  33.             return false;
  34.         }
  35.         $this->startSession();
  36.         return \array_key_exists($offset$_SESSION);
  37.     }
  38.     /**
  39.      * @param mixed $offset
  40.      *
  41.      * @return mixed
  42.      */
  43.     #[\ReturnTypeWillChange]
  44.     public function &offsetGet($offset)
  45.     {
  46.         $this->startSession();
  47.         return $_SESSION[$offset];
  48.     }
  49.     /**
  50.      * @param mixed $offset
  51.      * @param mixed $value
  52.      */
  53.     public function offsetSet($offset$value): void
  54.     {
  55.         $this->startSession();
  56.         $_SESSION[$offset] = $value;
  57.     }
  58.     /**
  59.      * @param mixed $offset
  60.      */
  61.     public function offsetUnset($offset): void
  62.     {
  63.         $this->startSession();
  64.         unset($_SESSION[$offset]);
  65.     }
  66.     public function count(): int
  67.     {
  68.         if (!$this->hasPreviousSession && !$this->session->isStarted()) {
  69.             return 0;
  70.         }
  71.         $this->startSession();
  72.         return \count($_SESSION);
  73.     }
  74.     private function startSession(): void
  75.     {
  76.         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.');
  77.         $this->session->start();
  78.         /** @phpstan-ignore-next-line */
  79.         if ($_SESSION instanceof self) {
  80.             throw new \RuntimeException('Unable to start the native session, $_SESSION was not replaced.');
  81.         }
  82.         // Accessing the session object may replace the global $_SESSION variable,
  83.         // so we store the bags in a local variable first before setting them on $_SESSION
  84.         $beBag $this->session->getBag('contao_backend');
  85.         $feBag $this->session->getBag('contao_frontend');
  86.         $_SESSION['BE_DATA'] = $beBag;
  87.         $_SESSION['FE_DATA'] = $feBag;
  88.     }
  89. }