Session.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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\SessionBagInterface;
  17. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  18. /**
  19. * Session.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. * @author Drak <drak@zikula.org>
  23. *
  24. * @api
  25. */
  26. class Session implements SessionInterface, \IteratorAggregate, \Countable
  27. {
  28. /**
  29. * Storage driver.
  30. *
  31. * @var SessionStorageInterface
  32. */
  33. protected $storage;
  34. /**
  35. * @var string
  36. */
  37. private $flashName;
  38. /**
  39. * @var string
  40. */
  41. private $attributeName;
  42. /**
  43. * Constructor.
  44. *
  45. * @param SessionStorageInterface $storage A SessionStorageInterface instance.
  46. * @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
  47. * @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
  48. */
  49. public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
  50. {
  51. $this->storage = $storage ?: new NativeSessionStorage();
  52. $attributes = $attributes ?: new AttributeBag();
  53. $this->attributeName = $attributes->getName();
  54. $this->registerBag($attributes);
  55. $flashes = $flashes ?: new FlashBag();
  56. $this->flashName = $flashes->getName();
  57. $this->registerBag($flashes);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function start()
  63. {
  64. return $this->storage->start();
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function has($name)
  70. {
  71. return $this->storage->getBag($this->attributeName)->has($name);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function get($name, $default = null)
  77. {
  78. return $this->storage->getBag($this->attributeName)->get($name, $default);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function set($name, $value)
  84. {
  85. $this->storage->getBag($this->attributeName)->set($name, $value);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function all()
  91. {
  92. return $this->storage->getBag($this->attributeName)->all();
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function replace(array $attributes)
  98. {
  99. $this->storage->getBag($this->attributeName)->replace($attributes);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function remove($name)
  105. {
  106. return $this->storage->getBag($this->attributeName)->remove($name);
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function clear()
  112. {
  113. $this->storage->getBag($this->attributeName)->clear();
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function isStarted()
  119. {
  120. return $this->storage->isStarted();
  121. }
  122. /**
  123. * Returns an iterator for attributes.
  124. *
  125. * @return \ArrayIterator An \ArrayIterator instance
  126. */
  127. public function getIterator()
  128. {
  129. return new \ArrayIterator($this->storage->getBag($this->attributeName)->all());
  130. }
  131. /**
  132. * Returns the number of attributes.
  133. *
  134. * @return int The number of attributes
  135. */
  136. public function count()
  137. {
  138. return count($this->storage->getBag($this->attributeName)->all());
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function invalidate($lifetime = null)
  144. {
  145. $this->storage->clear();
  146. return $this->migrate(true, $lifetime);
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function migrate($destroy = false, $lifetime = null)
  152. {
  153. return $this->storage->regenerate($destroy, $lifetime);
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function save()
  159. {
  160. $this->storage->save();
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function getId()
  166. {
  167. return $this->storage->getId();
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function setId($id)
  173. {
  174. $this->storage->setId($id);
  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. return $this->storage->getMetadataBag();
  196. }
  197. /**
  198. * {@inheritdoc}
  199. */
  200. public function registerBag(SessionBagInterface $bag)
  201. {
  202. $this->storage->registerBag($bag);
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function getBag($name)
  208. {
  209. return $this->storage->getBag($name);
  210. }
  211. /**
  212. * Gets the flashbag interface.
  213. *
  214. * @return FlashBagInterface
  215. */
  216. public function getFlashBag()
  217. {
  218. return $this->getBag($this->flashName);
  219. }
  220. }