Session.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 = 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. /**
  60. * @return void
  61. */
  62. public function set(string $name, mixed $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 && 0 <= $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 = false, ?int $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 && 0 <= $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. }