Session.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 SessionStorageInterface $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 = null, ?AttributeBagInterface $attributes = null, ?FlashBagInterface $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 $name, mixed $default = null): mixed
  56. {
  57. return $this->getAttributeBag()->get($name, $default);
  58. }
  59. public function set(string $name, mixed $value): void
  60. {
  61. $this->getAttributeBag()->set($name, $value);
  62. }
  63. public function all(): array
  64. {
  65. return $this->getAttributeBag()->all();
  66. }
  67. public function replace(array $attributes): void
  68. {
  69. $this->getAttributeBag()->replace($attributes);
  70. }
  71. public function remove(string $name): mixed
  72. {
  73. return $this->getAttributeBag()->remove($name);
  74. }
  75. public function clear(): void
  76. {
  77. $this->getAttributeBag()->clear();
  78. }
  79. public function isStarted(): bool
  80. {
  81. return $this->storage->isStarted();
  82. }
  83. /**
  84. * Returns an iterator for attributes.
  85. *
  86. * @return \ArrayIterator<string, mixed>
  87. */
  88. public function getIterator(): \ArrayIterator
  89. {
  90. return new \ArrayIterator($this->getAttributeBag()->all());
  91. }
  92. /**
  93. * Returns the number of attributes.
  94. */
  95. public function count(): int
  96. {
  97. return \count($this->getAttributeBag()->all());
  98. }
  99. public function &getUsageIndex(): int
  100. {
  101. return $this->usageIndex;
  102. }
  103. /**
  104. * @internal
  105. */
  106. public function isEmpty(): bool
  107. {
  108. if ($this->isStarted()) {
  109. ++$this->usageIndex;
  110. if ($this->usageReporter && 0 <= $this->usageIndex) {
  111. ($this->usageReporter)();
  112. }
  113. }
  114. foreach ($this->data as &$data) {
  115. if ($data) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. public function invalidate(?int $lifetime = null): bool
  122. {
  123. $this->storage->clear();
  124. return $this->migrate(true, $lifetime);
  125. }
  126. public function migrate(bool $destroy = false, ?int $lifetime = null): bool
  127. {
  128. return $this->storage->regenerate($destroy, $lifetime);
  129. }
  130. public function save(): void
  131. {
  132. $this->storage->save();
  133. }
  134. public function getId(): string
  135. {
  136. return $this->storage->getId();
  137. }
  138. public function setId(string $id): void
  139. {
  140. if ($this->storage->getId() !== $id) {
  141. $this->storage->setId($id);
  142. }
  143. }
  144. public function getName(): string
  145. {
  146. return $this->storage->getName();
  147. }
  148. public function setName(string $name): void
  149. {
  150. $this->storage->setName($name);
  151. }
  152. public function getMetadataBag(): MetadataBag
  153. {
  154. ++$this->usageIndex;
  155. if ($this->usageReporter && 0 <= $this->usageIndex) {
  156. ($this->usageReporter)();
  157. }
  158. return $this->storage->getMetadataBag();
  159. }
  160. public function registerBag(SessionBagInterface $bag): void
  161. {
  162. $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex, $this->usageReporter));
  163. }
  164. public function getBag(string $name): SessionBagInterface
  165. {
  166. $bag = $this->storage->getBag($name);
  167. return method_exists($bag, 'getBag') ? $bag->getBag() : $bag;
  168. }
  169. /**
  170. * Gets the flashbag interface.
  171. */
  172. public function getFlashBag(): FlashBagInterface
  173. {
  174. return $this->getBag($this->flashName);
  175. }
  176. /**
  177. * Gets the attributebag interface.
  178. *
  179. * Note that this method was added to help with IDE autocompletion.
  180. */
  181. private function getAttributeBag(): AttributeBagInterface
  182. {
  183. return $this->getBag($this->attributeName);
  184. }
  185. }