Session.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 SessionInterface, \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 = $usageReporter instanceof \Closure || !\is_callable($usageReporter) ? $usageReporter : \Closure::fromCallable($usageReporter);
  40. $attributes = $attributes ?? new AttributeBag();
  41. $this->attributeName = $attributes->getName();
  42. $this->registerBag($attributes);
  43. $flashes = $flashes ?? new FlashBag();
  44. $this->flashName = $flashes->getName();
  45. $this->registerBag($flashes);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function start(): bool
  51. {
  52. return $this->storage->start();
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function has(string $name): bool
  58. {
  59. return $this->getAttributeBag()->has($name);
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function get(string $name, mixed $default = null): mixed
  65. {
  66. return $this->getAttributeBag()->get($name, $default);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function set(string $name, mixed $value)
  72. {
  73. $this->getAttributeBag()->set($name, $value);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function all(): array
  79. {
  80. return $this->getAttributeBag()->all();
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function replace(array $attributes)
  86. {
  87. $this->getAttributeBag()->replace($attributes);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function remove(string $name): mixed
  93. {
  94. return $this->getAttributeBag()->remove($name);
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function clear()
  100. {
  101. $this->getAttributeBag()->clear();
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function isStarted(): bool
  107. {
  108. return $this->storage->isStarted();
  109. }
  110. /**
  111. * Returns an iterator for attributes.
  112. *
  113. * @return \ArrayIterator<string, mixed>
  114. */
  115. public function getIterator(): \ArrayIterator
  116. {
  117. return new \ArrayIterator($this->getAttributeBag()->all());
  118. }
  119. /**
  120. * Returns the number of attributes.
  121. */
  122. public function count(): int
  123. {
  124. return \count($this->getAttributeBag()->all());
  125. }
  126. public function &getUsageIndex(): int
  127. {
  128. return $this->usageIndex;
  129. }
  130. /**
  131. * @internal
  132. */
  133. public function isEmpty(): bool
  134. {
  135. if ($this->isStarted()) {
  136. ++$this->usageIndex;
  137. if ($this->usageReporter && 0 <= $this->usageIndex) {
  138. ($this->usageReporter)();
  139. }
  140. }
  141. foreach ($this->data as &$data) {
  142. if (!empty($data)) {
  143. return false;
  144. }
  145. }
  146. return true;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function invalidate(int $lifetime = null): bool
  152. {
  153. $this->storage->clear();
  154. return $this->migrate(true, $lifetime);
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function migrate(bool $destroy = false, int $lifetime = null): bool
  160. {
  161. return $this->storage->regenerate($destroy, $lifetime);
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function save()
  167. {
  168. $this->storage->save();
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function getId(): string
  174. {
  175. return $this->storage->getId();
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function setId(string $id)
  181. {
  182. if ($this->storage->getId() !== $id) {
  183. $this->storage->setId($id);
  184. }
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function getName(): string
  190. {
  191. return $this->storage->getName();
  192. }
  193. /**
  194. * {@inheritdoc}
  195. */
  196. public function setName(string $name)
  197. {
  198. $this->storage->setName($name);
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function getMetadataBag(): MetadataBag
  204. {
  205. ++$this->usageIndex;
  206. if ($this->usageReporter && 0 <= $this->usageIndex) {
  207. ($this->usageReporter)();
  208. }
  209. return $this->storage->getMetadataBag();
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function registerBag(SessionBagInterface $bag)
  215. {
  216. $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex, $this->usageReporter));
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function getBag(string $name): SessionBagInterface
  222. {
  223. $bag = $this->storage->getBag($name);
  224. return method_exists($bag, 'getBag') ? $bag->getBag() : $bag;
  225. }
  226. /**
  227. * Gets the flashbag interface.
  228. */
  229. public function getFlashBag(): FlashBagInterface
  230. {
  231. return $this->getBag($this->flashName);
  232. }
  233. /**
  234. * Gets the attributebag interface.
  235. *
  236. * Note that this method was added to help with IDE autocompletion.
  237. */
  238. private function getAttributeBag(): AttributeBagInterface
  239. {
  240. return $this->getBag($this->attributeName);
  241. }
  242. }