vendor/pimcore/pimcore/lib/Tool/Session.php line 169

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Pimcore
  5. *
  6. * This source file is available under two different licenses:
  7. * - GNU General Public License version 3 (GPLv3)
  8. * - Pimcore Commercial License (PCL)
  9. * Full copyright and license information is available in
  10. * LICENSE.md which is distributed with this source code.
  11. *
  12. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13. * @license http://www.pimcore.org/license GPLv3 and PCL
  14. */
  15. namespace Pimcore\Tool;
  16. use Pimcore\Bundle\AdminBundle\Session\Handler\AdminSessionHandler;
  17. use Pimcore\Bundle\AdminBundle\Session\Handler\AdminSessionHandlerInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
  20. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  21. final class Session
  22. {
  23. /**
  24. * @var AdminSessionHandlerInterface
  25. */
  26. private static $handler;
  27. /**
  28. * @desc This is for forward compatibility, the custom Session implementation is not being used anymore in Pimcore 11.
  29. * @desc For forward compatibility, you can use this class and pass the SessionInterface from the request, in Pimcore 10.6, the Admin Session will be used instead
  30. *
  31. * @param SessionInterface $session Parameter is not used here since the dedicated Admin Session is used. Please pass the Request SessionInterface here for forward compatibility
  32. * @param callable(AttributeBagInterface, SessionInterface):mixed $func
  33. *
  34. */
  35. public static function useBag(SessionInterface $session, callable $func, string $namespace = 'pimcore_admin'): mixed
  36. {
  37. return self::getSessionHandler()->useSessionAttributeBag($func, $namespace);
  38. }
  39. /**
  40. * @desc This is for forward compatibility, the custom Session implementation is not being used anymore in Pimcore 11.
  41. * @desc For forward compatibility, you can use this class and pass the SessionInterface from the request, in Pimcore 10.6, the Admin Session will be used instead
  42. *
  43. * @param SessionInterface $session Parameter is not used here since the dedicated Admin Session is used. Please pass the Request SessionInterface here for forward compatibility
  44. * @param string $namespace
  45. *
  46. */
  47. public static function getSessionBag(
  48. SessionInterface $session,
  49. string $namespace = 'pimcore_admin'
  50. ): ?AttributeBagInterface {
  51. $bag = self::getSessionHandler()->loadAttributeBag($namespace);
  52. if ($bag instanceof AttributeBagInterface) {
  53. return $bag;
  54. }
  55. return null;
  56. }
  57. /**
  58. * @deprecated
  59. */
  60. private static function getSessionHandler(): AdminSessionHandlerInterface
  61. {
  62. if (null === self::$handler) {
  63. self::$handler = \Pimcore::getContainer()->get(AdminSessionHandler::class);
  64. }
  65. return self::$handler;
  66. }
  67. /**
  68. * @deprecated
  69. */
  70. public static function getHandler(): AdminSessionHandlerInterface
  71. {
  72. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. No alternative given.', __METHOD__));
  73. return self::getSessionHandler();
  74. }
  75. /**
  76. * @deprecated
  77. */
  78. public static function setHandler(AdminSessionHandlerInterface $handler)
  79. {
  80. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. No alternative given.', __METHOD__));
  81. self::$handler = $handler;
  82. }
  83. /**
  84. * @param callable $func
  85. * @param string $namespace
  86. *
  87. * @return mixed
  88. *
  89. * @deprecated
  90. */
  91. public static function useSession($func, string $namespace = 'pimcore_admin')
  92. {
  93. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. Use \Pimcore\Tool\Session::useBag instead.', __METHOD__));
  94. return self::getSessionHandler()->useSessionAttributeBag($func, $namespace);
  95. }
  96. /**
  97. * @return string
  98. *
  99. * @deprecated
  100. */
  101. public static function getSessionId()
  102. {
  103. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. Use the Request SessionId instead.', __METHOD__));
  104. return self::getSessionHandler()->getSessionId();
  105. }
  106. /**
  107. * @return string
  108. *
  109. * @deprecated
  110. */
  111. public static function getSessionName()
  112. {
  113. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. Use the Request SessionName instead.', __METHOD__));
  114. return self::getSessionHandler()->getSessionName();
  115. }
  116. /**
  117. * @return bool
  118. *
  119. * @deprecated
  120. */
  121. public static function invalidate(): bool
  122. {
  123. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. Use the Request invalidate instead.', __METHOD__));
  124. return self::getSessionHandler()->invalidate();
  125. }
  126. /**
  127. * @return bool
  128. *
  129. * @deprecated
  130. */
  131. public static function regenerateId(): bool
  132. {
  133. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. Use the Request migrate instead.', __METHOD__));
  134. return self::getSessionHandler()->regenerateId();
  135. }
  136. /**
  137. * @param Request $request
  138. * @param bool $checkRequestParams
  139. *
  140. * @return bool
  141. *
  142. * @deprecated
  143. */
  144. public static function requestHasSessionId(Request $request, bool $checkRequestParams = false): bool
  145. {
  146. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. No alternative given, use Request Session instead.', __METHOD__));
  147. return self::getSessionHandler()->requestHasSessionId($request, $checkRequestParams);
  148. }
  149. /**
  150. * @param Request $request
  151. * @param bool $checkRequestParams
  152. *
  153. * @return string
  154. *
  155. * @deprecated
  156. */
  157. public static function getSessionIdFromRequest(Request $request, bool $checkRequestParams = false)
  158. {
  159. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. Use the Request SessionId instead.', __METHOD__));
  160. return self::getSessionHandler()->getSessionIdFromRequest($request, $checkRequestParams);
  161. }
  162. /**
  163. * Start session and get an attribute bag
  164. *
  165. * @param string $namespace
  166. *
  167. * @return AttributeBagInterface|null
  168. *
  169. * @deprecated
  170. */
  171. public static function get(string $namespace = 'pimcore_admin')
  172. {
  173. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. Use \Pimcore\Tool\Session::getSessionBag instead.', __METHOD__));
  174. $bag = self::getSessionHandler()->loadAttributeBag($namespace);
  175. if ($bag instanceof AttributeBagInterface) {
  176. return $bag;
  177. }
  178. return null;
  179. }
  180. /**
  181. * @param string $namespace
  182. *
  183. * @return AttributeBagInterface
  184. *
  185. * @deprecated
  186. */
  187. public static function getReadOnly(string $namespace = 'pimcore_admin'): AttributeBagInterface
  188. {
  189. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. No alternative given.', __METHOD__));
  190. return self::getSessionHandler()->getReadOnlyAttributeBag($namespace);
  191. }
  192. /**
  193. * Saves the session if it is the last admin session which was open
  194. *
  195. * @deprecated
  196. */
  197. public static function writeClose()
  198. {
  199. trigger_deprecation('pimcore/pimcore', '10.6', sprintf('Usage of method %s is deprecated since version 10.6 and will be removed in Pimcore 11. No alternative given.', __METHOD__));
  200. return self::getSessionHandler()->writeClose();
  201. }
  202. }