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

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