No Description

Session.php 5.3KB

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