Session.php 5.7 KB

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