Session.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace Symfony\Component\HttpFoundation;
  3. use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface;
  4. /*
  5. * This file is part of the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. /**
  13. * Session.
  14. *
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class Session implements \Serializable
  18. {
  19. protected $storage;
  20. protected $attributes;
  21. protected $oldFlashes;
  22. protected $started;
  23. protected $options;
  24. /**
  25. * Constructor.
  26. *
  27. * @param SessionStorageInterface $session A SessionStorageInterface instance
  28. * @param array $options An array of options
  29. */
  30. public function __construct(SessionStorageInterface $storage, array $options = array())
  31. {
  32. $this->storage = $storage;
  33. $this->options = $options;
  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($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. * Returns the locale
  148. *
  149. * @return string
  150. */
  151. public function getLocale()
  152. {
  153. return $this->attributes['_locale'];
  154. }
  155. /**
  156. * Sets the locale.
  157. *
  158. * @param string $locale
  159. */
  160. public function setLocale($locale)
  161. {
  162. if (false === $this->started) {
  163. $this->start();
  164. }
  165. $this->attributes['_locale'] = $locale;
  166. }
  167. public function getFlashes()
  168. {
  169. return $this->attributes['_flash'];
  170. }
  171. public function setFlashes($values)
  172. {
  173. if (false === $this->started) {
  174. $this->start();
  175. }
  176. $this->attributes['_flash'] = $values;
  177. }
  178. public function getFlash($name, $default = null)
  179. {
  180. return array_key_exists($name, $this->attributes['_flash']) ? $this->attributes['_flash'][$name] : $default;
  181. }
  182. public function setFlash($name, $value)
  183. {
  184. if (false === $this->started) {
  185. $this->start();
  186. }
  187. $this->attributes['_flash'][$name] = $value;
  188. unset($this->oldFlashes[$name]);
  189. }
  190. public function hasFlash($name)
  191. {
  192. return array_key_exists($name, $this->attributes['_flash']);
  193. }
  194. public function save()
  195. {
  196. if (true === $this->started) {
  197. if (isset($this->attributes['_flash'])) {
  198. $this->attributes['_flash'] = array_diff_key($this->attributes['_flash'], $this->oldFlashes);
  199. }
  200. $this->storage->write('_symfony2', $this->attributes);
  201. }
  202. }
  203. public function __destruct()
  204. {
  205. $this->save();
  206. }
  207. public function serialize()
  208. {
  209. return serialize(array($this->storage, $this->options));
  210. }
  211. public function unserialize($serialized)
  212. {
  213. list($this->storage, $this->options) = unserialize($serialized);
  214. $this->attributes = array();
  215. $this->started = false;
  216. }
  217. protected function getDefaultLocale()
  218. {
  219. return isset($this->options['default_locale']) ? $this->options['default_locale'] : 'en';
  220. }
  221. }