Session.php 7.0 KB

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