菜谱项目

Session.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. /**
  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->getAttributeBag()->has($name);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function get($name, $default = null)
  59. {
  60. return $this->getAttributeBag()->get($name, $default);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function set($name, $value)
  66. {
  67. $this->getAttributeBag()->set($name, $value);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function all()
  73. {
  74. return $this->getAttributeBag()->all();
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function replace(array $attributes)
  80. {
  81. $this->getAttributeBag()->replace($attributes);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function remove($name)
  87. {
  88. return $this->getAttributeBag()->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->getAttributeBag()->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->getAttributeBag()->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. $this->storage->setId($id);
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function getName()
  162. {
  163. return $this->storage->getName();
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function setName($name)
  169. {
  170. $this->storage->setName($name);
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function getMetadataBag()
  176. {
  177. return $this->storage->getMetadataBag();
  178. }
  179. /**
  180. * {@inheritdoc}
  181. */
  182. public function registerBag(SessionBagInterface $bag)
  183. {
  184. $this->storage->registerBag($bag);
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function getBag($name)
  190. {
  191. return $this->storage->getBag($name);
  192. }
  193. /**
  194. * Gets the flashbag interface.
  195. *
  196. * @return FlashBagInterface
  197. */
  198. public function getFlashBag()
  199. {
  200. return $this->getBag($this->flashName);
  201. }
  202. /**
  203. * Gets the attributebag interface.
  204. *
  205. * Note that this method was added to help with IDE autocompletion.
  206. *
  207. * @return AttributeBagInterface
  208. */
  209. private function getAttributeBag()
  210. {
  211. return $this->storage->getBag($this->attributeName);
  212. }
  213. }