vendor/se7enxweb/legacy-bridge/mvc/Session/LegacySessionStorage.php line 136

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. namespace eZ\Publish\Core\MVC\Legacy\Session;
  7. use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
  8. use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
  9. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  10. use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
  11. use ezpEvent;
  12. use eZSession;
  13. use eZDB;
  14. use Closure;
  15. /**
  16. * Session storage proxy for legacy.
  17. * Ensures that appropriate legacy session events are triggered whenever needed.
  18. *
  19. * Note that it extends NativeSessionStorage. This is only a workaround to strictly respect its full interface, as it
  20. * has methods that are not part of an interface.
  21. * See https://jira.ez.no/browse/EZP-24017
  22. */
  23. class LegacySessionStorage extends NativeSessionStorage
  24. {
  25. /**
  26. * @var SessionStorageInterface|NativeSessionStorage
  27. */
  28. private $innerSessionStorage;
  29. /**
  30. * @var callable
  31. */
  32. private $legacyKernelClosure;
  33. public function __construct(Closure $legacyKernelClosure, SessionStorageInterface $innerSessionStorage)
  34. {
  35. $this->innerSessionStorage = $innerSessionStorage;
  36. $this->legacyKernelClosure = $legacyKernelClosure;
  37. }
  38. public function start()
  39. {
  40. return $this->innerSessionStorage->start();
  41. }
  42. public function isStarted()
  43. {
  44. return $this->innerSessionStorage->isStarted();
  45. }
  46. public function getId()
  47. {
  48. return $this->innerSessionStorage->getId();
  49. }
  50. public function setId($id)
  51. {
  52. $this->innerSessionStorage->setId($id);
  53. }
  54. public function getName()
  55. {
  56. return $this->innerSessionStorage->getName();
  57. }
  58. public function setName($name)
  59. {
  60. $this->innerSessionStorage->setName($name);
  61. }
  62. /**
  63. * Ensures appropriate legacy events are sent when migrating the session.
  64. *
  65. * {@inheritdoc}
  66. */
  67. public function regenerate($destroy = false, $lifetime = null)
  68. {
  69. $oldSessionId = $this->getId();
  70. $success = $this->innerSessionStorage->regenerate($destroy, $lifetime);
  71. $newSessionId = $this->getId();
  72. // Callbacks cannot be called once the session is destroyed.
  73. if ($success && !$destroy) {
  74. $kernelClosure = $this->legacyKernelClosure;
  75. $kernelClosure()->runCallback(
  76. static function () use ($oldSessionId, $newSessionId) {
  77. ezpEvent::getInstance()->notify('session/regenerate', [$oldSessionId, $newSessionId]);
  78. $db = eZDB::instance();
  79. $escOldKey = $db->escapeString($oldSessionId);
  80. $escNewKey = $db->escapeString($newSessionId);
  81. $escUserID = $db->escapeString(eZSession::userID());
  82. eZSession::triggerCallback('regenerate_pre', [$db, $escNewKey, $escOldKey, $escUserID]);
  83. eZSession::triggerCallback('regenerate_post', [$db, $escNewKey, $escOldKey, $escUserID]);
  84. },
  85. false,
  86. false
  87. );
  88. }
  89. return $success;
  90. }
  91. public function save()
  92. {
  93. $this->innerSessionStorage->save();
  94. }
  95. /**
  96. * Clear all session data in memory.
  97. */
  98. public function clear()
  99. {
  100. $this->innerSessionStorage->clear();
  101. }
  102. public function getBag($name)
  103. {
  104. return $this->innerSessionStorage->getBag($name);
  105. }
  106. public function registerBag(SessionBagInterface $bag)
  107. {
  108. $this->innerSessionStorage->registerBag($bag);
  109. }
  110. public function getMetadataBag()
  111. {
  112. return $this->innerSessionStorage->getMetadataBag();
  113. }
  114. // Below reimplementation of public methods from NativeSessionStorage.
  115. public function setMetadataBag(MetadataBag $metaBag = null)
  116. {
  117. if ($this->innerSessionStorage instanceof NativeSessionStorage) {
  118. $this->innerSessionStorage->setMetadataBag($metaBag);
  119. }
  120. }
  121. public function getSaveHandler()
  122. {
  123. if ($this->innerSessionStorage instanceof NativeSessionStorage) {
  124. return $this->innerSessionStorage->getSaveHandler();
  125. }
  126. }
  127. public function setSaveHandler($saveHandler = null)
  128. {
  129. if ($this->innerSessionStorage instanceof NativeSessionStorage) {
  130. $this->innerSessionStorage->setSaveHandler($saveHandler);
  131. }
  132. }
  133. public function setOptions(array $options)
  134. {
  135. if ($this->innerSessionStorage instanceof NativeSessionStorage) {
  136. $this->innerSessionStorage->setOptions($options);
  137. }
  138. }
  139. }