Session.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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\Storage\SessionStorageInterface;
  12. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  13. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
  14. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  15. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  16. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  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 = array();
  27. private $hasBeenStarted;
  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 bool
  126. *
  127. * @internal
  128. */
  129. public function hasBeenStarted()
  130. {
  131. return $this->hasBeenStarted;
  132. }
  133. /**
  134. * @return bool
  135. *
  136. * @internal
  137. */
  138. public function isEmpty()
  139. {
  140. foreach ($this->data as &$data) {
  141. if (!empty($data)) {
  142. return false;
  143. }
  144. }
  145. return true;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function invalidate($lifetime = null)
  151. {
  152. $this->storage->clear();
  153. return $this->migrate(true, $lifetime);
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function migrate($destroy = false, $lifetime = null)
  159. {
  160. return $this->storage->regenerate($destroy, $lifetime);
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function save()
  166. {
  167. $this->storage->save();
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function getId()
  173. {
  174. return $this->storage->getId();
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function setId($id)
  180. {
  181. $this->storage->setId($id);
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function getName()
  187. {
  188. return $this->storage->getName();
  189. }
  190. /**
  191. * {@inheritdoc}
  192. */
  193. public function setName($name)
  194. {
  195. $this->storage->setName($name);
  196. }
  197. /**
  198. * {@inheritdoc}
  199. */
  200. public function getMetadataBag()
  201. {
  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->hasBeenStarted));
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function getBag($name)
  215. {
  216. return $this->storage->getBag($name)->getBag();
  217. }
  218. /**
  219. * Gets the flashbag interface.
  220. *
  221. * @return FlashBagInterface
  222. */
  223. public function getFlashBag()
  224. {
  225. return $this->getBag($this->flashName);
  226. }
  227. /**
  228. * Gets the attributebag interface.
  229. *
  230. * Note that this method was added to help with IDE autocompletion.
  231. *
  232. * @return AttributeBagInterface
  233. */
  234. private function getAttributeBag()
  235. {
  236. return $this->getBag($this->attributeName);
  237. }
  238. }