PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/HttpFoundation/Session.php

https://github.com/sebio/symfony
PHP | 289 lines | 162 code | 43 blank | 84 comment | 13 complexity | 658df602282dc047b0c1442e888920c3 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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.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. * 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 $this->attributes['_flash'];
  187. }
  188. public function setFlashes($values)
  189. {
  190. if (false === $this->started) {
  191. $this->start();
  192. }
  193. $this->attributes['_flash'] = $values;
  194. }
  195. public function getFlash($name, $default = null)
  196. {
  197. return array_key_exists($name, $this->attributes['_flash']) ? $this->attributes['_flash'][$name] : $default;
  198. }
  199. public function setFlash($name, $value)
  200. {
  201. if (false === $this->started) {
  202. $this->start();
  203. }
  204. $this->attributes['_flash'][$name] = $value;
  205. unset($this->oldFlashes[$name]);
  206. }
  207. public function hasFlash($name)
  208. {
  209. return array_key_exists($name, $this->attributes['_flash']);
  210. }
  211. public function removeFlash($name)
  212. {
  213. unset($this->attributes['_flash'][$name]);
  214. }
  215. public function clearFlashes()
  216. {
  217. $this->attributes['_flash'] = array();
  218. }
  219. public function save()
  220. {
  221. if (true === $this->started) {
  222. if (isset($this->attributes['_flash'])) {
  223. $this->attributes['_flash'] = array_diff_key($this->attributes['_flash'], $this->oldFlashes);
  224. }
  225. $this->storage->write('_symfony2', $this->attributes);
  226. }
  227. }
  228. public function __destruct()
  229. {
  230. $this->save();
  231. }
  232. public function serialize()
  233. {
  234. return serialize(array($this->storage, $this->options));
  235. }
  236. public function unserialize($serialized)
  237. {
  238. list($this->storage, $this->options) = unserialize($serialized);
  239. $this->attributes = array();
  240. $this->started = false;
  241. }
  242. protected function getDefaultLocale()
  243. {
  244. return isset($this->options['default_locale']) ? $this->options['default_locale'] : 'en';
  245. }
  246. }