Session.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 $started;
  21. protected $attributes;
  22. protected $flashes;
  23. protected $oldFlashes;
  24. protected $locale;
  25. protected $defaultLocale;
  26. /**
  27. * Constructor.
  28. *
  29. * @param SessionStorageInterface $storage A SessionStorageInterface instance
  30. * @param string $defaultLocale The default locale
  31. */
  32. public function __construct(SessionStorageInterface $storage, $defaultLocale = 'en')
  33. {
  34. $this->storage = $storage;
  35. $this->defaultLocale = $defaultLocale;
  36. $this->locale = $defaultLocale;
  37. $this->flashes = array();
  38. $this->oldFlashes = array();
  39. $this->attributes = array();
  40. $this->setPhpDefaultLocale($this->defaultLocale);
  41. $this->started = false;
  42. }
  43. /**
  44. * Starts the session storage.
  45. */
  46. public function start()
  47. {
  48. if (true === $this->started) {
  49. return;
  50. }
  51. $this->storage->start();
  52. $attributes = $this->storage->read('_symfony2');
  53. if (isset($attributes['attributes'])) {
  54. $this->attributes = $attributes['attributes'];
  55. $this->flashes = $attributes['flashes'];
  56. $this->locale = $attributes['locale'];
  57. $this->setPhpDefaultLocale($this->locale);
  58. // flag current flash messages to be removed at shutdown
  59. $this->oldFlashes = $this->flashes;
  60. }
  61. $this->started = true;
  62. }
  63. /**
  64. * Checks if an attribute is defined.
  65. *
  66. * @param string $name The attribute name
  67. *
  68. * @return Boolean true if the attribute is defined, false otherwise
  69. */
  70. public function has($name)
  71. {
  72. return array_key_exists($name, $this->attributes);
  73. }
  74. /**
  75. * Returns an attribute.
  76. *
  77. * @param string $name The attribute name
  78. * @param mixed $default The default value
  79. *
  80. * @return mixed
  81. */
  82. public function get($name, $default = null)
  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. 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. $this->attributes = $attributes;
  119. }
  120. /**
  121. * Removes an attribute.
  122. *
  123. * @param string $name
  124. */
  125. public function remove($name)
  126. {
  127. if (false === $this->started) {
  128. $this->start();
  129. }
  130. if (array_key_exists($name, $this->attributes)) {
  131. unset($this->attributes[$name]);
  132. }
  133. }
  134. /**
  135. * Clears all attributes.
  136. */
  137. public function clear()
  138. {
  139. if (false === $this->started) {
  140. $this->start();
  141. }
  142. $this->attributes = array();
  143. $this->flashes = array();
  144. $this->setPhpDefaultLocale($this->locale = $this->defaultLocale);
  145. }
  146. /**
  147. * Invalidates the current session.
  148. */
  149. public function invalidate()
  150. {
  151. $this->clear();
  152. $this->storage->regenerate();
  153. }
  154. /**
  155. * Migrates the current session to a new session id while maintaining all
  156. * session attributes.
  157. */
  158. public function migrate()
  159. {
  160. $this->storage->regenerate();
  161. }
  162. /**
  163. * Returns the session ID
  164. *
  165. * @return mixed The session ID
  166. */
  167. public function getId()
  168. {
  169. if (false === $this->started) {
  170. $this->start();
  171. }
  172. return $this->storage->getId();
  173. }
  174. /**
  175. * Returns the locale
  176. *
  177. * @return string
  178. */
  179. public function getLocale()
  180. {
  181. return $this->locale;
  182. }
  183. /**
  184. * Sets the locale.
  185. *
  186. * @param string $locale
  187. */
  188. public function setLocale($locale)
  189. {
  190. if (false === $this->started) {
  191. $this->start();
  192. }
  193. $this->setPhpDefaultLocale($this->locale = $locale);
  194. }
  195. /**
  196. * Gets the flash messages.
  197. *
  198. * @return array
  199. */
  200. public function getFlashes()
  201. {
  202. return $this->flashes;
  203. }
  204. /**
  205. * Sets the flash messages.
  206. *
  207. * @param array $values
  208. */
  209. public function setFlashes($values)
  210. {
  211. if (false === $this->started) {
  212. $this->start();
  213. }
  214. $this->flashes = $values;
  215. $this->oldFlashes = array();
  216. }
  217. /**
  218. * Gets a flash message.
  219. *
  220. * @param string $name
  221. * @param string|null $default
  222. *
  223. * @return string
  224. */
  225. public function getFlash($name, $default = null)
  226. {
  227. return array_key_exists($name, $this->flashes) ? $this->flashes[$name] : $default;
  228. }
  229. /**
  230. * Sets a flash message.
  231. *
  232. * @param string $name
  233. * @param string $value
  234. */
  235. public function setFlash($name, $value)
  236. {
  237. if (false === $this->started) {
  238. $this->start();
  239. }
  240. $this->flashes[$name] = $value;
  241. unset($this->oldFlashes[$name]);
  242. }
  243. /**
  244. * Checks whether a flash message exists.
  245. *
  246. * @param string $name
  247. *
  248. * @return Boolean
  249. */
  250. public function hasFlash($name)
  251. {
  252. if (false === $this->started) {
  253. $this->start();
  254. }
  255. return array_key_exists($name, $this->flashes);
  256. }
  257. /**
  258. * Removes a flash message.
  259. *
  260. * @param string $name
  261. */
  262. public function removeFlash($name)
  263. {
  264. if (false === $this->started) {
  265. $this->start();
  266. }
  267. unset($this->flashes[$name]);
  268. }
  269. /**
  270. * Removes the flash messages.
  271. */
  272. public function clearFlashes()
  273. {
  274. if (false === $this->started) {
  275. $this->start();
  276. }
  277. $this->flashes = array();
  278. $this->oldFlashes = array();
  279. }
  280. public function save()
  281. {
  282. if (false === $this->started) {
  283. $this->start();
  284. }
  285. $this->flashes = array_diff_key($this->flashes, $this->oldFlashes);
  286. $this->storage->write('_symfony2', array(
  287. 'attributes' => $this->attributes,
  288. 'flashes' => $this->flashes,
  289. 'locale' => $this->locale,
  290. ));
  291. }
  292. public function __destruct()
  293. {
  294. if (true === $this->started) {
  295. $this->save();
  296. }
  297. }
  298. public function serialize()
  299. {
  300. return serialize(array($this->storage, $this->defaultLocale));
  301. }
  302. public function unserialize($serialized)
  303. {
  304. list($this->storage, $this->defaultLocale) = unserialize($serialized);
  305. $this->attributes = array();
  306. $this->started = false;
  307. }
  308. private function setPhpDefaultLocale($locale)
  309. {
  310. try {
  311. \Locale::setDefault($this->locale);
  312. } catch (\Exception $e) {
  313. // means that intl is not installed.
  314. }
  315. }
  316. }