MockArraySessionStorage.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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\Session\Storage;
  11. use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
  12. /**
  13. * MockArraySessionStorage mocks the session for unit tests.
  14. *
  15. * No PHP session is actually started since a session can be initialized
  16. * and shutdown only once per PHP execution cycle.
  17. *
  18. * When doing functional testing, you should use MockFileSessionStorage instead.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  22. * @author Drak <drak@zikula.org>
  23. */
  24. class MockArraySessionStorage implements SessionStorageInterface
  25. {
  26. /**
  27. * @var string
  28. */
  29. protected $id = '';
  30. /**
  31. * @var string
  32. */
  33. protected $name;
  34. /**
  35. * @var bool
  36. */
  37. protected $started = false;
  38. /**
  39. * @var bool
  40. */
  41. protected $closed = false;
  42. /**
  43. * @var array
  44. */
  45. protected $data = [];
  46. /**
  47. * @var MetadataBag
  48. */
  49. protected $metadataBag;
  50. /**
  51. * @var array|SessionBagInterface[]
  52. */
  53. protected $bags = [];
  54. public function __construct(string $name = 'MOCKSESSID', ?MetadataBag $metaBag = null)
  55. {
  56. $this->name = $name;
  57. $this->setMetadataBag($metaBag);
  58. }
  59. /**
  60. * @return void
  61. */
  62. public function setSessionData(array $array)
  63. {
  64. $this->data = $array;
  65. }
  66. public function start(): bool
  67. {
  68. if ($this->started) {
  69. return true;
  70. }
  71. if (empty($this->id)) {
  72. $this->id = $this->generateId();
  73. }
  74. $this->loadSession();
  75. return true;
  76. }
  77. public function regenerate(bool $destroy = false, ?int $lifetime = null): bool
  78. {
  79. if (!$this->started) {
  80. $this->start();
  81. }
  82. $this->metadataBag->stampNew($lifetime);
  83. $this->id = $this->generateId();
  84. return true;
  85. }
  86. public function getId(): string
  87. {
  88. return $this->id;
  89. }
  90. /**
  91. * @return void
  92. */
  93. public function setId(string $id)
  94. {
  95. if ($this->started) {
  96. throw new \LogicException('Cannot set session ID after the session has started.');
  97. }
  98. $this->id = $id;
  99. }
  100. public function getName(): string
  101. {
  102. return $this->name;
  103. }
  104. /**
  105. * @return void
  106. */
  107. public function setName(string $name)
  108. {
  109. $this->name = $name;
  110. }
  111. /**
  112. * @return void
  113. */
  114. public function save()
  115. {
  116. if (!$this->started || $this->closed) {
  117. throw new \RuntimeException('Trying to save a session that was not started yet or was already closed.');
  118. }
  119. // nothing to do since we don't persist the session data
  120. $this->closed = false;
  121. $this->started = false;
  122. }
  123. /**
  124. * @return void
  125. */
  126. public function clear()
  127. {
  128. // clear out the bags
  129. foreach ($this->bags as $bag) {
  130. $bag->clear();
  131. }
  132. // clear out the session
  133. $this->data = [];
  134. // reconnect the bags to the session
  135. $this->loadSession();
  136. }
  137. /**
  138. * @return void
  139. */
  140. public function registerBag(SessionBagInterface $bag)
  141. {
  142. $this->bags[$bag->getName()] = $bag;
  143. }
  144. public function getBag(string $name): SessionBagInterface
  145. {
  146. if (!isset($this->bags[$name])) {
  147. throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
  148. }
  149. if (!$this->started) {
  150. $this->start();
  151. }
  152. return $this->bags[$name];
  153. }
  154. public function isStarted(): bool
  155. {
  156. return $this->started;
  157. }
  158. /**
  159. * @return void
  160. */
  161. public function setMetadataBag(?MetadataBag $bag = null)
  162. {
  163. if (1 > \func_num_args()) {
  164. trigger_deprecation('symfony/http-foundation', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
  165. }
  166. $this->metadataBag = $bag ?? new MetadataBag();
  167. }
  168. /**
  169. * Gets the MetadataBag.
  170. */
  171. public function getMetadataBag(): MetadataBag
  172. {
  173. return $this->metadataBag;
  174. }
  175. /**
  176. * Generates a session ID.
  177. *
  178. * This doesn't need to be particularly cryptographically secure since this is just
  179. * a mock.
  180. */
  181. protected function generateId(): string
  182. {
  183. return hash('sha256', uniqid('ss_mock_', true));
  184. }
  185. /**
  186. * @return void
  187. */
  188. protected function loadSession()
  189. {
  190. $bags = array_merge($this->bags, [$this->metadataBag]);
  191. foreach ($bags as $bag) {
  192. $key = $bag->getStorageKey();
  193. $this->data[$key] ??= [];
  194. $bag->initialize($this->data[$key]);
  195. }
  196. $this->started = true;
  197. $this->closed = false;
  198. }
  199. }