Session.php 5.2 KB

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