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

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