vendor/contao/core-bundle/src/Resources/contao/library/Contao/Session.php line 17

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Contao.
  4. *
  5. * (c) Leo Feyer
  6. *
  7. * @license LGPL-3.0-or-later
  8. */
  9. namespace Contao;
  10. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
  11. use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
  12. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  13. trigger_deprecation('contao/core-bundle', '4.0', 'Using the "Contao\Session" class has been deprecated and will no longer work in Contao 5.0. Use the session service instead.');
  14. /**
  15. * Handles reading and updating the session data
  16. *
  17. * The class functions as an adapter for the PHP $_SESSION array and separates
  18. * back end from front end session data.
  19. *
  20. * Usage:
  21. *
  22. * $session = Session::getInstance();
  23. * $session->set('foo', 'bar');
  24. * echo $session->get('foo');
  25. *
  26. * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  27. * Use the session service instead.
  28. */
  29. class Session
  30. {
  31. /**
  32. * Object instance (Singleton)
  33. * @var Session
  34. */
  35. protected static $objInstance;
  36. /**
  37. * Symfony session object
  38. * @var SymfonySession
  39. */
  40. private $session;
  41. /**
  42. * Symfony session bag
  43. * @var AttributeBagInterface
  44. */
  45. private $sessionBag;
  46. /**
  47. * Session keys that are not stored in the parameter bag
  48. * @var array
  49. */
  50. private static $mappedKeys = array('referer', 'popupReferer', 'CURRENT_ID');
  51. /**
  52. * Get the session data
  53. */
  54. protected function __construct()
  55. {
  56. if (\PHP_SAPI == 'cli')
  57. {
  58. $this->session = new SymfonySession(new MockArraySessionStorage());
  59. }
  60. else
  61. {
  62. $this->session = System::getContainer()->get('session');
  63. }
  64. $this->sessionBag = $this->session->getBag($this->getSessionBagKey());
  65. }
  66. /**
  67. * Prevent cloning of the object (Singleton)
  68. */
  69. final public function __clone()
  70. {
  71. }
  72. /**
  73. * Return the object instance (Singleton)
  74. *
  75. * @return Session The object instance
  76. */
  77. public static function getInstance()
  78. {
  79. if (static::$objInstance === null)
  80. {
  81. static::$objInstance = new static();
  82. }
  83. return static::$objInstance;
  84. }
  85. /**
  86. * Return a session variable
  87. *
  88. * @param string $strKey The variable name
  89. *
  90. * @return mixed The variable value
  91. */
  92. public function get($strKey)
  93. {
  94. // Map the referer (see #281)
  95. if (\in_array($strKey, self::$mappedKeys))
  96. {
  97. return $this->session->get($strKey);
  98. }
  99. return $this->sessionBag->get($strKey);
  100. }
  101. /**
  102. * Set a session variable
  103. *
  104. * @param string $strKey The variable name
  105. * @param mixed $varValue The variable value
  106. */
  107. public function set($strKey, $varValue)
  108. {
  109. // Map the referer (see #281)
  110. if (\in_array($strKey, self::$mappedKeys))
  111. {
  112. $this->session->set($strKey, $varValue);
  113. }
  114. else
  115. {
  116. $this->sessionBag->set($strKey, $varValue);
  117. }
  118. }
  119. /**
  120. * Remove a session variable
  121. *
  122. * @param string $strKey The variable name
  123. */
  124. public function remove($strKey)
  125. {
  126. // Map the referer (see #281)
  127. if (\in_array($strKey, self::$mappedKeys))
  128. {
  129. $this->session->remove($strKey);
  130. }
  131. else
  132. {
  133. $this->sessionBag->remove($strKey);
  134. }
  135. }
  136. /**
  137. * Return the session data as array
  138. *
  139. * @return array The session data
  140. */
  141. public function getData()
  142. {
  143. $data = $this->sessionBag->all();
  144. // Map the referer (see #281)
  145. foreach (self::$mappedKeys as $strKey)
  146. {
  147. unset($data[$strKey]);
  148. if ($this->session->has($strKey))
  149. {
  150. $data[$strKey] = $this->session->get($strKey);
  151. }
  152. }
  153. return $data;
  154. }
  155. /**
  156. * Set the session data from an array
  157. *
  158. * @param array $arrData The session data
  159. *
  160. * @throws \Exception If $arrData is not an array
  161. */
  162. public function setData($arrData)
  163. {
  164. if (!\is_array($arrData))
  165. {
  166. throw new \Exception('Array required to set session data');
  167. }
  168. // Map the referer (see #281)
  169. foreach (self::$mappedKeys as $strKey)
  170. {
  171. if (isset($arrData[$strKey]))
  172. {
  173. $this->session->set($strKey, $arrData[$strKey]);
  174. unset($arrData[$strKey]);
  175. }
  176. }
  177. $this->sessionBag->replace($arrData);
  178. }
  179. /**
  180. * Append data to the session
  181. *
  182. * @param mixed $varData The data object or array
  183. *
  184. * @throws \Exception If $varData is not an array or object
  185. */
  186. public function appendData($varData)
  187. {
  188. if (\is_object($varData))
  189. {
  190. $varData = get_object_vars($varData);
  191. }
  192. if (!\is_array($varData))
  193. {
  194. throw new \Exception('Array or object required to append session data');
  195. }
  196. foreach ($varData as $k=>$v)
  197. {
  198. // Map the referer (see #281)
  199. if (\in_array($k, self::$mappedKeys))
  200. {
  201. $this->session->set($k, $v);
  202. }
  203. else
  204. {
  205. $this->sessionBag->set($k, $v);
  206. }
  207. }
  208. }
  209. /**
  210. * Gets the correct session bag key depending on the Contao environment
  211. *
  212. * @return string
  213. */
  214. private function getSessionBagKey()
  215. {
  216. switch (TL_MODE)
  217. {
  218. case 'BE':
  219. return 'contao_backend';
  220. case 'FE':
  221. return 'contao_frontend';
  222. default:
  223. return 'attributes';
  224. }
  225. }
  226. }
  227. class_alias(Session::class, 'Session');