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