Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/Session/Storage/MockArraySessionStorage.php

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