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

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