Session.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. public function getIterator()
  111. {
  112. return new \ArrayIterator($this->getAttributeBag()->all());
  113. }
  114. /**
  115. * Returns the number of attributes.
  116. *
  117. * @return int
  118. */
  119. public function count()
  120. {
  121. return \count($this->getAttributeBag()->all());
  122. }
  123. public function &getUsageIndex(): int
  124. {
  125. return $this->usageIndex;
  126. }
  127. /**
  128. * @internal
  129. */
  130. public function isEmpty(): bool
  131. {
  132. if ($this->isStarted()) {
  133. ++$this->usageIndex;
  134. }
  135. foreach ($this->data as &$data) {
  136. if (!empty($data)) {
  137. return false;
  138. }
  139. }
  140. return true;
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function invalidate($lifetime = null)
  146. {
  147. $this->storage->clear();
  148. return $this->migrate(true, $lifetime);
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function migrate($destroy = false, $lifetime = null)
  154. {
  155. return $this->storage->regenerate($destroy, $lifetime);
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function save()
  161. {
  162. $this->storage->save();
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function getId()
  168. {
  169. return $this->storage->getId();
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function setId($id)
  175. {
  176. if ($this->storage->getId() !== $id) {
  177. $this->storage->setId($id);
  178. }
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function getName()
  184. {
  185. return $this->storage->getName();
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function setName($name)
  191. {
  192. $this->storage->setName($name);
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. public function getMetadataBag()
  198. {
  199. ++$this->usageIndex;
  200. return $this->storage->getMetadataBag();
  201. }
  202. /**
  203. * {@inheritdoc}
  204. */
  205. public function registerBag(SessionBagInterface $bag)
  206. {
  207. $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex));
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function getBag($name)
  213. {
  214. $bag = $this->storage->getBag($name);
  215. return method_exists($bag, 'getBag') ? $bag->getBag() : $bag;
  216. }
  217. /**
  218. * Gets the flashbag interface.
  219. *
  220. * @return FlashBagInterface
  221. */
  222. public function getFlashBag()
  223. {
  224. return $this->getBag($this->flashName);
  225. }
  226. /**
  227. * Gets the attributebag interface.
  228. *
  229. * Note that this method was added to help with IDE autocompletion.
  230. */
  231. private function getAttributeBag(): AttributeBagInterface
  232. {
  233. return $this->getBag($this->attributeName);
  234. }
  235. }