Session.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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();
  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'] = isset($this->options['default_locale']) ? $this->options['default_locale'] : 'en';
  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. if (false === $this->started) {
  67. $this->start();
  68. }
  69. return array_key_exists($name, $this->attributes);
  70. }
  71. /**
  72. * Returns an attribute.
  73. *
  74. * @param string $name The attribute name
  75. * @param mixed $default The default value
  76. *
  77. * @return mixed
  78. */
  79. public function get($name, $default = null)
  80. {
  81. if (false === $this->started) {
  82. $this->start();
  83. }
  84. return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
  85. }
  86. /**
  87. * Sets an attribute.
  88. *
  89. * @param string $name
  90. * @param mixed $value
  91. */
  92. public function set($name, $value)
  93. {
  94. if (false === $this->started) {
  95. $this->start();
  96. }
  97. $this->attributes[$name] = $value;
  98. }
  99. /**
  100. * Returns attributes.
  101. *
  102. * @return array Attributes
  103. */
  104. public function getAttributes()
  105. {
  106. if (false === $this->started) {
  107. $this->start();
  108. }
  109. return $this->attributes;
  110. }
  111. /**
  112. * Sets attributes.
  113. *
  114. * @param array $attributes Attributes
  115. */
  116. public function setAttributes($attributes)
  117. {
  118. if (false === $this->started) {
  119. $this->start();
  120. }
  121. $this->attributes = $attributes;
  122. }
  123. /**
  124. * Removes an attribute.
  125. *
  126. * @param string $name
  127. */
  128. public function remove($name)
  129. {
  130. if (false === $this->started) {
  131. $this->start();
  132. }
  133. if (array_key_exists($name, $this->attributes)) {
  134. unset($this->attributes[$name]);
  135. }
  136. }
  137. public function invalidate()
  138. {
  139. $this->storage->regenerate();
  140. }
  141. /**
  142. * Returns the locale
  143. *
  144. * @return string
  145. */
  146. public function getLocale()
  147. {
  148. if (false === $this->started) {
  149. $this->start();
  150. }
  151. return $this->attributes['_locale'];
  152. }
  153. /**
  154. * Sets the locale.
  155. *
  156. * @param string $locale
  157. */
  158. public function setLocale($locale)
  159. {
  160. if (false === $this->started) {
  161. $this->start();
  162. }
  163. $this->attributes['_locale'] = $locale;
  164. }
  165. public function getFlashMessages()
  166. {
  167. if (false === $this->started) {
  168. $this->start();
  169. }
  170. return $this->attributes['_flash'];
  171. }
  172. public function setFlashMessages($values)
  173. {
  174. if (false === $this->started) {
  175. $this->start();
  176. }
  177. $this->attributes['_flash'] = $values;
  178. }
  179. public function getFlash($name, $default = null)
  180. {
  181. if (false === $this->started) {
  182. $this->start();
  183. }
  184. return array_key_exists($name, $this->attributes['_flash']) ? $this->attributes['_flash'][$name] : $default;
  185. }
  186. public function setFlash($name, $value)
  187. {
  188. if (false === $this->started) {
  189. $this->start();
  190. }
  191. $this->attributes['_flash'][$name] = $value;
  192. unset($this->oldFlashes[$name]);
  193. }
  194. public function hasFlash($name)
  195. {
  196. if (false === $this->started) {
  197. $this->start();
  198. }
  199. return array_key_exists($name, $this->attributes['_flash']);
  200. }
  201. public function save()
  202. {
  203. if (true === $this->started) {
  204. $this->attributes['_flash'] = array_diff_key($this->attributes['_flash'], $this->oldFlashes);
  205. $this->storage->write('_symfony2', $this->attributes);
  206. }
  207. }
  208. public function __destruct()
  209. {
  210. $this->save();
  211. }
  212. public function serialize()
  213. {
  214. return serialize(array($this->storage, $this->options));
  215. }
  216. public function unserialize($serialized)
  217. {
  218. list($this->storage, $this->options) = unserialize($serialized);
  219. $this->attributes = array();
  220. $this->started = false;
  221. }
  222. }