Session.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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
  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. $attributeBag = $attributes ?: new AttributeBag();
  53. $this->attributeName = $attributeBag->getName();
  54. $this->registerBag($attributeBag);
  55. $flashBag = $flashes ?: new FlashBag();
  56. $this->flashName = $flashBag->getName();
  57. $this->registerBag($flashBag);
  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 invalidate()
  119. {
  120. $this->storage->clear();
  121. return $this->storage->regenerate(true);
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function migrate($destroy = false)
  127. {
  128. return $this->storage->regenerate($destroy);
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function save()
  134. {
  135. $this->storage->save();
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function getId()
  141. {
  142. return $this->storage->getId();
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function setId($id)
  148. {
  149. $this->storage->setId($id);
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getName()
  155. {
  156. return $this->storage->getName();
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function setName($name)
  162. {
  163. $this->storage->setName($name);
  164. }
  165. /**
  166. * Registers a SessionBagInterface with the session.
  167. *
  168. * @param SessionBagInterface $bag
  169. */
  170. public function registerBag(SessionBagInterface $bag)
  171. {
  172. $this->storage->registerBag($bag);
  173. }
  174. /**
  175. * Get's a bag instance.
  176. *
  177. * @param string $name
  178. *
  179. * @return SessionBagInterface
  180. */
  181. public function getBag($name)
  182. {
  183. return $this->storage->getBag($name);
  184. }
  185. /**
  186. * Gets the flashbag interface.
  187. *
  188. * @return FlashBagInterface
  189. */
  190. public function getFlashBag()
  191. {
  192. return $this->getBag($this->flashName);
  193. }
  194. // the following methods are kept for compatibility with Symfony 2.0 (they will be removed for Symfony 2.3)
  195. /**
  196. * @return array
  197. *
  198. * @deprecated since 2.1, will be removed from 2.3
  199. */
  200. public function getFlashes()
  201. {
  202. return $this->getBag('flashes')->all();
  203. }
  204. /**
  205. * @param array $values
  206. *
  207. * @deprecated since 2.1, will be removed from 2.3
  208. */
  209. public function setFlashes($values)
  210. {
  211. $this->getBag('flashes')->setAll($values);
  212. }
  213. /**
  214. * @param string $name
  215. * @param string $default
  216. *
  217. * @return string
  218. *
  219. * @deprecated since 2.1, will be removed from 2.3
  220. */
  221. public function getFlash($name, $default = null)
  222. {
  223. return $this->getBag('flashes')->get($name, $default);
  224. }
  225. /**
  226. * @param string $name
  227. * @param string $value
  228. *
  229. * @deprecated since 2.1, will be removed from 2.3
  230. */
  231. public function setFlash($name, $value)
  232. {
  233. $this->getBag('flashes')->set($name, $value);
  234. }
  235. /**
  236. * @param string $name
  237. *
  238. * @return Boolean
  239. *
  240. * @deprecated since 2.1, will be removed from 2.3
  241. */
  242. public function hasFlash($name)
  243. {
  244. return $this->getBag('flashes')->has($name);
  245. }
  246. /**
  247. * @param string $name
  248. *
  249. * @deprecated since 2.1, will be removed from 2.3
  250. */
  251. public function removeFlash($name)
  252. {
  253. $this->getBag('flashes')->get($name);
  254. }
  255. /**
  256. * @return array
  257. *
  258. * @deprecated since 2.1, will be removed from 2.3
  259. */
  260. public function clearFlashes()
  261. {
  262. return $this->getBag('flashes')->clear();
  263. }
  264. }