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

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. class LazySessionAccess implements \ArrayAccess, \Countable
  16. {
  17.     /**
  18.      * @var SessionInterface
  19.      */
  20.     private $session;
  21.     public function __construct(SessionInterface $session)
  22.     {
  23.         $this->session $session;
  24.     }
  25.     public function offsetExists($offset): bool
  26.     {
  27.         $this->startSession();
  28.         return \array_key_exists($offset$_SESSION);
  29.     }
  30.     public function &offsetGet($offset)
  31.     {
  32.         $this->startSession();
  33.         return $_SESSION[$offset];
  34.     }
  35.     public function offsetSet($offset$value): void
  36.     {
  37.         $this->startSession();
  38.         $_SESSION[$offset] = $value;
  39.     }
  40.     public function offsetUnset($offset): void
  41.     {
  42.         $this->startSession();
  43.         unset($_SESSION[$offset]);
  44.     }
  45.     public function count(): int
  46.     {
  47.         $this->startSession();
  48.         return \count($_SESSION);
  49.     }
  50.     /**
  51.      * @throws \RuntimeException
  52.      */
  53.     private function startSession(): void
  54.     {
  55.         @trigger_error('Using $_SESSION has been deprecated and will no longer work in Contao 5.0. Use the Symfony session instead.'E_USER_DEPRECATED);
  56.         $this->session->start();
  57.         if ($_SESSION instanceof self) {
  58.             throw new \RuntimeException('Unable to start the native session, $_SESSION was not replaced.');
  59.         }
  60.         // Accessing the session object may replace the global $_SESSION variable,
  61.         // so we store the bags in a local variable first before setting them on $_SESSION
  62.         $beBag $this->session->getBag('contao_backend');
  63.         $feBag $this->session->getBag('contao_frontend');
  64.         $_SESSION['BE_DATA'] = $beBag;
  65.         $_SESSION['FE_DATA'] = $feBag;
  66.     }
  67. }