PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-foundation/Session/Storage/MockArraySessionStorage.php

https://gitlab.com/jjpa2018/dashboard
PHP | 252 lines | 121 code | 40 blank | 91 comment | 9 complexity | 7db27078df9939e27df21f7d8f649f06 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\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. public function setSessionData(array $array)
  60. {
  61. $this->data = $array;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function start()
  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. /**
  78. * {@inheritdoc}
  79. */
  80. public function regenerate(bool $destroy = false, int $lifetime = null)
  81. {
  82. if (!$this->started) {
  83. $this->start();
  84. }
  85. $this->metadataBag->stampNew($lifetime);
  86. $this->id = $this->generateId();
  87. return true;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getId()
  93. {
  94. return $this->id;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function setId(string $id)
  100. {
  101. if ($this->started) {
  102. throw new \LogicException('Cannot set session ID after the session has started.');
  103. }
  104. $this->id = $id;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getName()
  110. {
  111. return $this->name;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function setName(string $name)
  117. {
  118. $this->name = $name;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function save()
  124. {
  125. if (!$this->started || $this->closed) {
  126. throw new \RuntimeException('Trying to save a session that was not started yet or was already closed.');
  127. }
  128. // nothing to do since we don't persist the session data
  129. $this->closed = false;
  130. $this->started = false;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function clear()
  136. {
  137. // clear out the bags
  138. foreach ($this->bags as $bag) {
  139. $bag->clear();
  140. }
  141. // clear out the session
  142. $this->data = [];
  143. // reconnect the bags to the session
  144. $this->loadSession();
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function registerBag(SessionBagInterface $bag)
  150. {
  151. $this->bags[$bag->getName()] = $bag;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function getBag(string $name)
  157. {
  158. if (!isset($this->bags[$name])) {
  159. throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
  160. }
  161. if (!$this->started) {
  162. $this->start();
  163. }
  164. return $this->bags[$name];
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function isStarted()
  170. {
  171. return $this->started;
  172. }
  173. public function setMetadataBag(MetadataBag $bag = null)
  174. {
  175. if (null === $bag) {
  176. $bag = new MetadataBag();
  177. }
  178. $this->metadataBag = $bag;
  179. }
  180. /**
  181. * Gets the MetadataBag.
  182. *
  183. * @return MetadataBag
  184. */
  185. public function getMetadataBag()
  186. {
  187. return $this->metadataBag;
  188. }
  189. /**
  190. * Generates a session ID.
  191. *
  192. * This doesn't need to be particularly cryptographically secure since this is just
  193. * a mock.
  194. *
  195. * @return string
  196. */
  197. protected function generateId()
  198. {
  199. return hash('sha256', uniqid('ss_mock_', true));
  200. }
  201. protected function loadSession()
  202. {
  203. $bags = array_merge($this->bags, [$this->metadataBag]);
  204. foreach ($bags as $bag) {
  205. $key = $bag->getStorageKey();
  206. $this->data[$key] = $this->data[$key] ?? [];
  207. $bag->initialize($this->data[$key]);
  208. }
  209. $this->started = true;
  210. $this->closed = false;
  211. }
  212. }