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

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