123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
-
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
- namespace Symfony\Component\HttpFoundation\Session;
-
- use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
- use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
- use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
- use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
- use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
- use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
-
- // Help opcache.preload discover always-needed symbols
- class_exists(AttributeBag::class);
- class_exists(FlashBag::class);
- class_exists(SessionBagProxy::class);
-
- /**
- * @author Fabien Potencier <fabien@symfony.com>
- * @author Drak <drak@zikula.org>
- *
- * @implements \IteratorAggregate<string, mixed>
- */
- class Session implements SessionInterface, \IteratorAggregate, \Countable
- {
- protected $storage;
-
- private $flashName;
- private $attributeName;
- private $data = [];
- private $usageIndex = 0;
- private $usageReporter;
-
- public function __construct(?SessionStorageInterface $storage = null, ?AttributeBagInterface $attributes = null, ?FlashBagInterface $flashes = null, ?callable $usageReporter = null)
- {
- $this->storage = $storage ?? new NativeSessionStorage();
- $this->usageReporter = $usageReporter;
-
- $attributes = $attributes ?? new AttributeBag();
- $this->attributeName = $attributes->getName();
- $this->registerBag($attributes);
-
- $flashes = $flashes ?? new FlashBag();
- $this->flashName = $flashes->getName();
- $this->registerBag($flashes);
- }
-
- /**
- * {@inheritdoc}
- */
- public function start()
- {
- return $this->storage->start();
- }
-
- /**
- * {@inheritdoc}
- */
- public function has(string $name)
- {
- return $this->getAttributeBag()->has($name);
- }
-
- /**
- * {@inheritdoc}
- */
- public function get(string $name, $default = null)
- {
- return $this->getAttributeBag()->get($name, $default);
- }
-
- /**
- * {@inheritdoc}
- */
- public function set(string $name, $value)
- {
- $this->getAttributeBag()->set($name, $value);
- }
-
- /**
- * {@inheritdoc}
- */
- public function all()
- {
- return $this->getAttributeBag()->all();
- }
-
- /**
- * {@inheritdoc}
- */
- public function replace(array $attributes)
- {
- $this->getAttributeBag()->replace($attributes);
- }
-
- /**
- * {@inheritdoc}
- */
- public function remove(string $name)
- {
- return $this->getAttributeBag()->remove($name);
- }
-
- /**
- * {@inheritdoc}
- */
- public function clear()
- {
- $this->getAttributeBag()->clear();
- }
-
- /**
- * {@inheritdoc}
- */
- public function isStarted()
- {
- return $this->storage->isStarted();
- }
-
- /**
- * Returns an iterator for attributes.
- *
- * @return \ArrayIterator<string, mixed>
- */
- #[\ReturnTypeWillChange]
- public function getIterator()
- {
- return new \ArrayIterator($this->getAttributeBag()->all());
- }
-
- /**
- * Returns the number of attributes.
- *
- * @return int
- */
- #[\ReturnTypeWillChange]
- public function count()
- {
- return \count($this->getAttributeBag()->all());
- }
-
- public function &getUsageIndex(): int
- {
- return $this->usageIndex;
- }
-
- /**
- * @internal
- */
- public function isEmpty(): bool
- {
- if ($this->isStarted()) {
- ++$this->usageIndex;
- if ($this->usageReporter && 0 <= $this->usageIndex) {
- ($this->usageReporter)();
- }
- }
- foreach ($this->data as &$data) {
- if (!empty($data)) {
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * {@inheritdoc}
- */
- public function invalidate(?int $lifetime = null)
- {
- $this->storage->clear();
-
- return $this->migrate(true, $lifetime);
- }
-
- /**
- * {@inheritdoc}
- */
- public function migrate(bool $destroy = false, ?int $lifetime = null)
- {
- return $this->storage->regenerate($destroy, $lifetime);
- }
-
- /**
- * {@inheritdoc}
- */
- public function save()
- {
- $this->storage->save();
- }
-
- /**
- * {@inheritdoc}
- */
- public function getId()
- {
- return $this->storage->getId();
- }
-
- /**
- * {@inheritdoc}
- */
- public function setId(string $id)
- {
- if ($this->storage->getId() !== $id) {
- $this->storage->setId($id);
- }
- }
-
- /**
- * {@inheritdoc}
- */
- public function getName()
- {
- return $this->storage->getName();
- }
-
- /**
- * {@inheritdoc}
- */
- public function setName(string $name)
- {
- $this->storage->setName($name);
- }
-
- /**
- * {@inheritdoc}
- */
- public function getMetadataBag()
- {
- ++$this->usageIndex;
- if ($this->usageReporter && 0 <= $this->usageIndex) {
- ($this->usageReporter)();
- }
-
- return $this->storage->getMetadataBag();
- }
-
- /**
- * {@inheritdoc}
- */
- public function registerBag(SessionBagInterface $bag)
- {
- $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex, $this->usageReporter));
- }
-
- /**
- * {@inheritdoc}
- */
- public function getBag(string $name)
- {
- $bag = $this->storage->getBag($name);
-
- return method_exists($bag, 'getBag') ? $bag->getBag() : $bag;
- }
-
- /**
- * Gets the flashbag interface.
- *
- * @return FlashBagInterface
- */
- public function getFlashBag()
- {
- return $this->getBag($this->flashName);
- }
-
- /**
- * Gets the attributebag interface.
- *
- * Note that this method was added to help with IDE autocompletion.
- */
- private function getAttributeBag(): AttributeBagInterface
- {
- return $this->getBag($this->attributeName);
- }
- }
|