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

https://github.com/Faianca/symfony · PHP · 405 lines · 200 code · 49 blank · 156 comment · 18 complexity · 89469ded515c90639708a96fc53edf9e MD5 · raw file

  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. * @api
  18. */
  19. class Session implements \Serializable
  20. {
  21. protected $storage;
  22. protected $started;
  23. protected $attributes;
  24. protected $flashes;
  25. protected $oldFlashes;
  26. protected $locale;
  27. protected $defaultLocale;
  28. protected $closed;
  29. /**
  30. * Constructor.
  31. *
  32. * @param SessionStorageInterface $storage A SessionStorageInterface instance
  33. * @param string $defaultLocale The default locale
  34. */
  35. public function __construct(SessionStorageInterface $storage, $defaultLocale = 'en')
  36. {
  37. $this->storage = $storage;
  38. $this->defaultLocale = $defaultLocale;
  39. $this->locale = $defaultLocale;
  40. $this->flashes = array();
  41. $this->oldFlashes = array();
  42. $this->attributes = array();
  43. $this->setPhpDefaultLocale($this->defaultLocale);
  44. $this->started = false;
  45. $this->closed = false;
  46. }
  47. /**
  48. * Starts the session storage.
  49. *
  50. * @api
  51. */
  52. public function start()
  53. {
  54. if (true === $this->started) {
  55. return;
  56. }
  57. $this->storage->start();
  58. $attributes = $this->storage->read('_symfony2');
  59. if (isset($attributes['attributes'])) {
  60. $this->attributes = $attributes['attributes'];
  61. $this->flashes = $attributes['flashes'];
  62. $this->locale = $attributes['locale'];
  63. $this->setPhpDefaultLocale($this->locale);
  64. // flag current flash messages to be removed at shutdown
  65. $this->oldFlashes = $this->flashes;
  66. }
  67. $this->started = true;
  68. }
  69. /**
  70. * Checks if an attribute is defined.
  71. *
  72. * @param string $name The attribute name
  73. *
  74. * @return Boolean true if the attribute is defined, false otherwise
  75. *
  76. * @api
  77. */
  78. public function has($name)
  79. {
  80. return array_key_exists($name, $this->attributes);
  81. }
  82. /**
  83. * Returns an attribute.
  84. *
  85. * @param string $name The attribute name
  86. * @param mixed $default The default value
  87. *
  88. * @return mixed
  89. *
  90. * @api
  91. */
  92. public function get($name, $default = null)
  93. {
  94. return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
  95. }
  96. /**
  97. * Sets an attribute.
  98. *
  99. * @param string $name
  100. * @param mixed $value
  101. *
  102. * @api
  103. */
  104. public function set($name, $value)
  105. {
  106. if (false === $this->started) {
  107. $this->start();
  108. }
  109. $this->attributes[$name] = $value;
  110. }
  111. /**
  112. * Returns attributes.
  113. *
  114. * @return array Attributes
  115. *
  116. * @api
  117. */
  118. public function all()
  119. {
  120. return $this->attributes;
  121. }
  122. /**
  123. * Sets attributes.
  124. *
  125. * @param array $attributes Attributes
  126. *
  127. * @api
  128. */
  129. public function replace(array $attributes)
  130. {
  131. if (false === $this->started) {
  132. $this->start();
  133. }
  134. $this->attributes = $attributes;
  135. }
  136. /**
  137. * Removes an attribute.
  138. *
  139. * @param string $name
  140. *
  141. * @api
  142. */
  143. public function remove($name)
  144. {
  145. if (false === $this->started) {
  146. $this->start();
  147. }
  148. if (array_key_exists($name, $this->attributes)) {
  149. unset($this->attributes[$name]);
  150. }
  151. }
  152. /**
  153. * Clears all attributes.
  154. *
  155. * @api
  156. */
  157. public function clear()
  158. {
  159. if (false === $this->started) {
  160. $this->start();
  161. }
  162. $this->attributes = array();
  163. $this->flashes = array();
  164. $this->setPhpDefaultLocale($this->locale = $this->defaultLocale);
  165. }
  166. /**
  167. * Invalidates the current session.
  168. *
  169. * @api
  170. */
  171. public function invalidate()
  172. {
  173. $this->clear();
  174. $this->storage->regenerate(true);
  175. }
  176. /**
  177. * Migrates the current session to a new session id while maintaining all
  178. * session attributes.
  179. *
  180. * @api
  181. */
  182. public function migrate()
  183. {
  184. $this->storage->regenerate();
  185. }
  186. /**
  187. * Returns the session ID
  188. *
  189. * @return mixed The session ID
  190. *
  191. * @api
  192. */
  193. public function getId()
  194. {
  195. if (false === $this->started) {
  196. $this->start();
  197. }
  198. return $this->storage->getId();
  199. }
  200. /**
  201. * Returns the locale
  202. *
  203. * @return string
  204. */
  205. public function getLocale()
  206. {
  207. return $this->locale;
  208. }
  209. /**
  210. * Sets the locale.
  211. *
  212. * @param string $locale
  213. */
  214. public function setLocale($locale)
  215. {
  216. if (false === $this->started) {
  217. $this->start();
  218. }
  219. $this->setPhpDefaultLocale($this->locale = $locale);
  220. }
  221. /**
  222. * Gets the flash messages.
  223. *
  224. * @return array
  225. */
  226. public function getFlashes()
  227. {
  228. return $this->flashes;
  229. }
  230. /**
  231. * Sets the flash messages.
  232. *
  233. * @param array $values
  234. */
  235. public function setFlashes($values)
  236. {
  237. if (false === $this->started) {
  238. $this->start();
  239. }
  240. $this->flashes = $values;
  241. $this->oldFlashes = array();
  242. }
  243. /**
  244. * Gets a flash message.
  245. *
  246. * @param string $name
  247. * @param string|null $default
  248. *
  249. * @return string
  250. */
  251. public function getFlash($name, $default = null)
  252. {
  253. return array_key_exists($name, $this->flashes) ? $this->flashes[$name] : $default;
  254. }
  255. /**
  256. * Sets a flash message.
  257. *
  258. * @param string $name
  259. * @param string $value
  260. */
  261. public function setFlash($name, $value)
  262. {
  263. if (false === $this->started) {
  264. $this->start();
  265. }
  266. $this->flashes[$name] = $value;
  267. unset($this->oldFlashes[$name]);
  268. }
  269. /**
  270. * Checks whether a flash message exists.
  271. *
  272. * @param string $name
  273. *
  274. * @return Boolean
  275. */
  276. public function hasFlash($name)
  277. {
  278. if (false === $this->started) {
  279. $this->start();
  280. }
  281. return array_key_exists($name, $this->flashes);
  282. }
  283. /**
  284. * Removes a flash message.
  285. *
  286. * @param string $name
  287. */
  288. public function removeFlash($name)
  289. {
  290. if (false === $this->started) {
  291. $this->start();
  292. }
  293. unset($this->flashes[$name]);
  294. }
  295. /**
  296. * Removes the flash messages.
  297. */
  298. public function clearFlashes()
  299. {
  300. if (false === $this->started) {
  301. $this->start();
  302. }
  303. $this->flashes = array();
  304. $this->oldFlashes = array();
  305. }
  306. public function save()
  307. {
  308. if (false === $this->started) {
  309. $this->start();
  310. }
  311. $this->flashes = array_diff_key($this->flashes, $this->oldFlashes);
  312. $this->storage->write('_symfony2', array(
  313. 'attributes' => $this->attributes,
  314. 'flashes' => $this->flashes,
  315. 'locale' => $this->locale,
  316. ));
  317. }
  318. /**
  319. * This method should be called when you don't want the session to be saved
  320. * when the Session object is garbaged collected (useful for instance when
  321. * you want to simulate the interaction of several users/sessions in a single
  322. * PHP process).
  323. */
  324. public function close()
  325. {
  326. $this->closed = true;
  327. }
  328. public function __destruct()
  329. {
  330. if (true === $this->started && !$this->closed) {
  331. $this->save();
  332. }
  333. }
  334. public function serialize()
  335. {
  336. return serialize(array($this->storage, $this->defaultLocale));
  337. }
  338. public function unserialize($serialized)
  339. {
  340. list($this->storage, $this->defaultLocale) = unserialize($serialized);
  341. $this->attributes = array();
  342. $this->started = false;
  343. }
  344. private function setPhpDefaultLocale($locale)
  345. {
  346. // if either the class Locale doesn't exist, or an exception is thrown when
  347. // setting the default locale, the intl module is not installed, and
  348. // the call can be ignored:
  349. try {
  350. if (class_exists('Locale', false)) {
  351. \Locale::setDefault($locale);
  352. }
  353. } catch (\Exception $e) {
  354. }
  355. }
  356. }