Session.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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;
  11. use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface;
  12. /**
  13. * Session.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Session implements \Serializable
  18. {
  19. protected $storage;
  20. protected $attributes;
  21. protected $oldFlashes;
  22. protected $started;
  23. protected $defaultLocale;
  24. /**
  25. * Constructor.
  26. *
  27. * @param SessionStorageInterface $storage A SessionStorageInterface instance
  28. * @param string $defaultLocale The default locale
  29. */
  30. public function __construct(SessionStorageInterface $storage, $defaultLocale = 'en')
  31. {
  32. $this->storage = $storage;
  33. $this->defaultLocale = $defaultLocale;
  34. $this->attributes = array('_flash' => array(), '_locale' => $this->getDefaultLocale());
  35. $this->started = false;
  36. }
  37. /**
  38. * Starts the session storage.
  39. */
  40. public function start()
  41. {
  42. if (true === $this->started) {
  43. return;
  44. }
  45. $this->storage->start();
  46. $this->attributes = $this->storage->read('_symfony2');
  47. if (!isset($this->attributes['_flash'])) {
  48. $this->attributes['_flash'] = array();
  49. }
  50. if (!isset($this->attributes['_locale'])) {
  51. $this->attributes['_locale'] = $this->getDefaultLocale();
  52. }
  53. // flag current flash messages to be removed at shutdown
  54. $this->oldFlashes = array_flip(array_keys($this->attributes['_flash']));
  55. $this->started = true;
  56. }
  57. /**
  58. * Checks if an attribute is defined.
  59. *
  60. * @param string $name The attribute name
  61. *
  62. * @return Boolean true if the attribute is defined, false otherwise
  63. */
  64. public function has($name)
  65. {
  66. return array_key_exists($name, $this->attributes);
  67. }
  68. /**
  69. * Returns an attribute.
  70. *
  71. * @param string $name The attribute name
  72. * @param mixed $default The default value
  73. *
  74. * @return mixed
  75. */
  76. public function get($name, $default = null)
  77. {
  78. return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
  79. }
  80. /**
  81. * Sets an attribute.
  82. *
  83. * @param string $name
  84. * @param mixed $value
  85. */
  86. public function set($name, $value)
  87. {
  88. if (false === $this->started) {
  89. $this->start();
  90. }
  91. $this->attributes[$name] = $value;
  92. }
  93. /**
  94. * Returns attributes.
  95. *
  96. * @return array Attributes
  97. */
  98. public function getAttributes()
  99. {
  100. return $this->attributes;
  101. }
  102. /**
  103. * Sets attributes.
  104. *
  105. * @param array $attributes Attributes
  106. */
  107. public function setAttributes(array $attributes)
  108. {
  109. if (false === $this->started) {
  110. $this->start();
  111. }
  112. $this->attributes = $attributes;
  113. }
  114. /**
  115. * Removes an attribute.
  116. *
  117. * @param string $name
  118. */
  119. public function remove($name)
  120. {
  121. if (false === $this->started) {
  122. $this->start();
  123. }
  124. if (array_key_exists($name, $this->attributes)) {
  125. unset($this->attributes[$name]);
  126. }
  127. }
  128. /**
  129. * Clears all attributes.
  130. */
  131. public function clear()
  132. {
  133. if (false === $this->started) {
  134. $this->start();
  135. }
  136. $this->attributes = array();
  137. }
  138. /**
  139. * Invalidates the current session.
  140. */
  141. public function invalidate()
  142. {
  143. $this->clear();
  144. $this->storage->regenerate();
  145. }
  146. /**
  147. * Migrates the current session to a new session id while maintaining all
  148. * session attributes.
  149. */
  150. public function migrate()
  151. {
  152. $this->storage->regenerate();
  153. }
  154. /**
  155. * Returns the session ID
  156. *
  157. * @return mixed The session ID
  158. */
  159. public function getId()
  160. {
  161. return $this->storage->getId();
  162. }
  163. /**
  164. * Returns the locale
  165. *
  166. * @return string
  167. */
  168. public function getLocale()
  169. {
  170. return $this->attributes['_locale'];
  171. }
  172. /**
  173. * Sets the locale.
  174. *
  175. * @param string $locale
  176. */
  177. public function setLocale($locale)
  178. {
  179. if (false === $this->started) {
  180. $this->start();
  181. }
  182. $this->attributes['_locale'] = $locale;
  183. }
  184. public function getFlashes()
  185. {
  186. return isset($this->attributes['_flash']) ? $this->attributes['_flash'] : array();
  187. }
  188. public function setFlashes($values)
  189. {
  190. if (false === $this->started) {
  191. $this->start();
  192. }
  193. $this->attributes['_flash'] = $values;
  194. $this->oldFlashes = array();
  195. }
  196. public function getFlash($name, $default = null)
  197. {
  198. return array_key_exists($name, $this->getFlashes()) ? $this->attributes['_flash'][$name] : $default;
  199. }
  200. public function setFlash($name, $value)
  201. {
  202. if (false === $this->started) {
  203. $this->start();
  204. }
  205. $this->attributes['_flash'][$name] = $value;
  206. unset($this->oldFlashes[$name]);
  207. }
  208. public function hasFlash($name)
  209. {
  210. if (false === $this->started) {
  211. $this->start();
  212. }
  213. return array_key_exists($name, $this->attributes['_flash']);
  214. }
  215. public function removeFlash($name)
  216. {
  217. if (false === $this->started) {
  218. $this->start();
  219. }
  220. unset($this->attributes['_flash'][$name]);
  221. }
  222. public function clearFlashes()
  223. {
  224. if (false === $this->started) {
  225. $this->start();
  226. }
  227. $this->attributes['_flash'] = array();
  228. $this->oldFlashes = array();
  229. }
  230. public function save()
  231. {
  232. if (false === $this->started) {
  233. $this->start();
  234. }
  235. if (isset($this->attributes['_flash'])) {
  236. $this->attributes['_flash'] = array_diff_key($this->attributes['_flash'], $this->oldFlashes);
  237. }
  238. $this->storage->write('_symfony2', $this->attributes);
  239. }
  240. public function __destruct()
  241. {
  242. $this->save();
  243. }
  244. public function serialize()
  245. {
  246. return serialize(array($this->storage, $this->defaultLocale));
  247. }
  248. public function unserialize($serialized)
  249. {
  250. list($this->storage, $this->defaultLocale) = unserialize($serialized);
  251. $this->attributes = array();
  252. $this->started = false;
  253. }
  254. private function getDefaultLocale()
  255. {
  256. return $this->defaultLocale;
  257. }
  258. }