Session.php 5.3 KB

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