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

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.     private $usageReporter;
  33.     public function __construct(SessionStorageInterface $storage nullAttributeBagInterface $attributes nullFlashBagInterface $flashes null, callable $usageReporter null)
  34.     {
  35.         $this->storage $storage ?? new NativeSessionStorage();
  36.         $this->usageReporter $usageReporter;
  37.         $attributes $attributes ?? new AttributeBag();
  38.         $this->attributeName $attributes->getName();
  39.         $this->registerBag($attributes);
  40.         $flashes $flashes ?? new FlashBag();
  41.         $this->flashName $flashes->getName();
  42.         $this->registerBag($flashes);
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function start()
  48.     {
  49.         return $this->storage->start();
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function has(string $name)
  55.     {
  56.         return $this->getAttributeBag()->has($name);
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function get(string $name$default null)
  62.     {
  63.         return $this->getAttributeBag()->get($name$default);
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     public function set(string $name$value)
  69.     {
  70.         $this->getAttributeBag()->set($name$value);
  71.     }
  72.     /**
  73.      * {@inheritdoc}
  74.      */
  75.     public function all()
  76.     {
  77.         return $this->getAttributeBag()->all();
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function replace(array $attributes)
  83.     {
  84.         $this->getAttributeBag()->replace($attributes);
  85.     }
  86.     /**
  87.      * {@inheritdoc}
  88.      */
  89.     public function remove(string $name)
  90.     {
  91.         return $this->getAttributeBag()->remove($name);
  92.     }
  93.     /**
  94.      * {@inheritdoc}
  95.      */
  96.     public function clear()
  97.     {
  98.         $this->getAttributeBag()->clear();
  99.     }
  100.     /**
  101.      * {@inheritdoc}
  102.      */
  103.     public function isStarted()
  104.     {
  105.         return $this->storage->isStarted();
  106.     }
  107.     /**
  108.      * Returns an iterator for attributes.
  109.      *
  110.      * @return \ArrayIterator An \ArrayIterator instance
  111.      */
  112.     #[\ReturnTypeWillChange]
  113.     public function getIterator()
  114.     {
  115.         return new \ArrayIterator($this->getAttributeBag()->all());
  116.     }
  117.     /**
  118.      * Returns the number of attributes.
  119.      *
  120.      * @return int
  121.      */
  122.     #[\ReturnTypeWillChange]
  123.     public function count()
  124.     {
  125.         return \count($this->getAttributeBag()->all());
  126.     }
  127.     public function &getUsageIndex(): int
  128.     {
  129.         return $this->usageIndex;
  130.     }
  131.     /**
  132.      * @internal
  133.      */
  134.     public function isEmpty(): bool
  135.     {
  136.         if ($this->isStarted()) {
  137.             ++$this->usageIndex;
  138.             if ($this->usageReporter && <= $this->usageIndex) {
  139.                 ($this->usageReporter)();
  140.             }
  141.         }
  142.         foreach ($this->data as &$data) {
  143.             if (!empty($data)) {
  144.                 return false;
  145.             }
  146.         }
  147.         return true;
  148.     }
  149.     /**
  150.      * {@inheritdoc}
  151.      */
  152.     public function invalidate(int $lifetime null)
  153.     {
  154.         $this->storage->clear();
  155.         return $this->migrate(true$lifetime);
  156.     }
  157.     /**
  158.      * {@inheritdoc}
  159.      */
  160.     public function migrate(bool $destroy falseint $lifetime null)
  161.     {
  162.         return $this->storage->regenerate($destroy$lifetime);
  163.     }
  164.     /**
  165.      * {@inheritdoc}
  166.      */
  167.     public function save()
  168.     {
  169.         $this->storage->save();
  170.     }
  171.     /**
  172.      * {@inheritdoc}
  173.      */
  174.     public function getId()
  175.     {
  176.         return $this->storage->getId();
  177.     }
  178.     /**
  179.      * {@inheritdoc}
  180.      */
  181.     public function setId(string $id)
  182.     {
  183.         if ($this->storage->getId() !== $id) {
  184.             $this->storage->setId($id);
  185.         }
  186.     }
  187.     /**
  188.      * {@inheritdoc}
  189.      */
  190.     public function getName()
  191.     {
  192.         return $this->storage->getName();
  193.     }
  194.     /**
  195.      * {@inheritdoc}
  196.      */
  197.     public function setName(string $name)
  198.     {
  199.         $this->storage->setName($name);
  200.     }
  201.     /**
  202.      * {@inheritdoc}
  203.      */
  204.     public function getMetadataBag()
  205.     {
  206.         ++$this->usageIndex;
  207.         if ($this->usageReporter && <= $this->usageIndex) {
  208.             ($this->usageReporter)();
  209.         }
  210.         return $this->storage->getMetadataBag();
  211.     }
  212.     /**
  213.      * {@inheritdoc}
  214.      */
  215.     public function registerBag(SessionBagInterface $bag)
  216.     {
  217.         $this->storage->registerBag(new SessionBagProxy($bag$this->data$this->usageIndex$this->usageReporter));
  218.     }
  219.     /**
  220.      * {@inheritdoc}
  221.      */
  222.     public function getBag(string $name)
  223.     {
  224.         $bag $this->storage->getBag($name);
  225.         return method_exists($bag'getBag') ? $bag->getBag() : $bag;
  226.     }
  227.     /**
  228.      * Gets the flashbag interface.
  229.      *
  230.      * @return FlashBagInterface
  231.      */
  232.     public function getFlashBag()
  233.     {
  234.         return $this->getBag($this->flashName);
  235.     }
  236.     /**
  237.      * Gets the attributebag interface.
  238.      *
  239.      * Note that this method was added to help with IDE autocompletion.
  240.      */
  241.     private function getAttributeBag(): AttributeBagInterface
  242.     {
  243.         return $this->getBag($this->attributeName);
  244.     }
  245. }