Session.php 5.5 KB

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