vendor/symfony/http-foundation/Session/Session.php line 251

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpFoundation\Session;
  11. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  12. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
  13. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  14. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  15. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  16. use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
  17. // Help opcache.preload discover always-needed symbols
  18. class_exists(AttributeBag::class);
  19. class_exists(FlashBag::class);
  20. class_exists(SessionBagProxy::class);
  21. /**
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  * @author Drak <drak@zikula.org>
  24.  */
  25. class Session implements SessionInterface\IteratorAggregate\Countable
  26. {
  27.     protected $storage;
  28.     private $flashName;
  29.     private $attributeName;
  30.     private $data = [];
  31.     private $usageIndex 0;
  32.     public function __construct(SessionStorageInterface $storage nullAttributeBagInterface $attributes nullFlashBagInterface $flashes null)
  33.     {
  34.         $this->storage $storage ?? new NativeSessionStorage();
  35.         $attributes $attributes ?? new AttributeBag();
  36.         $this->attributeName $attributes->getName();
  37.         $this->registerBag($attributes);
  38.         $flashes $flashes ?? new FlashBag();
  39.         $this->flashName $flashes->getName();
  40.         $this->registerBag($flashes);
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function start()
  46.     {
  47.         return $this->storage->start();
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public function has($name)
  53.     {
  54.         return $this->getAttributeBag()->has($name);
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function get($name$default null)
  60.     {
  61.         return $this->getAttributeBag()->get($name$default);
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function set($name$value)
  67.     {
  68.         $this->getAttributeBag()->set($name$value);
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function all()
  74.     {
  75.         return $this->getAttributeBag()->all();
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function replace(array $attributes)
  81.     {
  82.         $this->getAttributeBag()->replace($attributes);
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function remove($name)
  88.     {
  89.         return $this->getAttributeBag()->remove($name);
  90.     }
  91.     /**
  92.      * {@inheritdoc}
  93.      */
  94.     public function clear()
  95.     {
  96.         $this->getAttributeBag()->clear();
  97.     }
  98.     /**
  99.      * {@inheritdoc}
  100.      */
  101.     public function isStarted()
  102.     {
  103.         return $this->storage->isStarted();
  104.     }
  105.     /**
  106.      * Returns an iterator for attributes.
  107.      *
  108.      * @return \ArrayIterator An \ArrayIterator instance
  109.      */
  110.     #[\ReturnTypeWillChange]
  111.     public function getIterator()
  112.     {
  113.         return new \ArrayIterator($this->getAttributeBag()->all());
  114.     }
  115.     /**
  116.      * Returns the number of attributes.
  117.      *
  118.      * @return int
  119.      */
  120.     #[\ReturnTypeWillChange]
  121.     public function count()
  122.     {
  123.         return \count($this->getAttributeBag()->all());
  124.     }
  125.     public function &getUsageIndex(): int
  126.     {
  127.         return $this->usageIndex;
  128.     }
  129.     /**
  130.      * @internal
  131.      */
  132.     public function isEmpty(): bool
  133.     {
  134.         if ($this->isStarted()) {
  135.             ++$this->usageIndex;
  136.         }
  137.         foreach ($this->data as &$data) {
  138.             if (!empty($data)) {
  139.                 return false;
  140.             }
  141.         }
  142.         return true;
  143.     }
  144.     /**
  145.      * {@inheritdoc}
  146.      */
  147.     public function invalidate($lifetime null)
  148.     {
  149.         $this->storage->clear();
  150.         return $this->migrate(true$lifetime);
  151.     }
  152.     /**
  153.      * {@inheritdoc}
  154.      */
  155.     public function migrate($destroy false$lifetime null)
  156.     {
  157.         return $this->storage->regenerate($destroy$lifetime);
  158.     }
  159.     /**
  160.      * {@inheritdoc}
  161.      */
  162.     public function save()
  163.     {
  164.         $this->storage->save();
  165.     }
  166.     /**
  167.      * {@inheritdoc}
  168.      */
  169.     public function getId()
  170.     {
  171.         return $this->storage->getId();
  172.     }
  173.     /**
  174.      * {@inheritdoc}
  175.      */
  176.     public function setId($id)
  177.     {
  178.         if ($this->storage->getId() !== $id) {
  179.             $this->storage->setId($id);
  180.         }
  181.     }
  182.     /**
  183.      * {@inheritdoc}
  184.      */
  185.     public function getName()
  186.     {
  187.         return $this->storage->getName();
  188.     }
  189.     /**
  190.      * {@inheritdoc}
  191.      */
  192.     public function setName($name)
  193.     {
  194.         $this->storage->setName($name);
  195.     }
  196.     /**
  197.      * {@inheritdoc}
  198.      */
  199.     public function getMetadataBag()
  200.     {
  201.         ++$this->usageIndex;
  202.         return $this->storage->getMetadataBag();
  203.     }
  204.     /**
  205.      * {@inheritdoc}
  206.      */
  207.     public function registerBag(SessionBagInterface $bag)
  208.     {
  209.         $this->storage->registerBag(new SessionBagProxy($bag$this->data$this->usageIndex));
  210.     }
  211.     /**
  212.      * {@inheritdoc}
  213.      */
  214.     public function getBag($name)
  215.     {
  216.         $bag $this->storage->getBag($name);
  217.         return method_exists($bag'getBag') ? $bag->getBag() : $bag;
  218.     }
  219.     /**
  220.      * Gets the flashbag interface.
  221.      *
  222.      * @return FlashBagInterface
  223.      */
  224.     public function getFlashBag()
  225.     {
  226.         return $this->getBag($this->flashName);
  227.     }
  228.     /**
  229.      * Gets the attributebag interface.
  230.      *
  231.      * Note that this method was added to help with IDE autocompletion.
  232.      */
  233.     private function getAttributeBag(): AttributeBagInterface
  234.     {
  235.         return $this->getBag($this->attributeName);
  236.     }
  237. }