Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
MockArraySessionStorage.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\HttpFoundation\Session\Storage;
013
014 use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
015
016 /**
017 * MockArraySessionStorage mocks the session for unit tests.
018 *
019 * No PHP session is actually started since a session can be initialized
020 * and shutdown only once per PHP execution cycle.
021 *
022 * When doing functional testing, you should use MockFileSessionStorage instead.
023 *
024 * @author Fabien Potencier <fabien@symfony.com>
025 * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
026 * @author Drak <drak@zikula.org>
027 */
028 class MockArraySessionStorage implements SessionStorageInterface
029 {
030 /**
031 * @var string
032 */
033 protected $id = '';
034
035 /**
036 * @var string
037 */
038 protected $name;
039
040 /**
041 * @var bool
042 */
043 protected $started = false;
044
045 /**
046 * @var bool
047 */
048 protected $closed = false;
049
050 /**
051 * @var array
052 */
053 protected $data = [];
054
055 /**
056 * @var MetadataBag
057 */
058 protected $metadataBag;
059
060 /**
061 * @var array|SessionBagInterface[]
062 */
063 protected $bags = [];
064
065 /**
066 * @param string $name Session name
067 * @param MetadataBag $metaBag MetadataBag instance
068 */
069 public function __construct($name = 'MOCKSESSID', MetadataBag $metaBag = null)
070 {
071 $this->name = $name;
072 $this->setMetadataBag($metaBag);
073 }
074
075 public function setSessionData(array $array)
076 {
077 $this->data = $array;
078 }
079
080 /**
081 * {@inheritdoc}
082 */
083 public function start()
084 {
085 if ($this->started) {
086 return true;
087 }
088
089 if (empty($this->id)) {
090 $this->id = $this->generateId();
091 }
092
093 $this->loadSession();
094
095 return true;
096 }
097
098 /**
099 * {@inheritdoc}
100 */
101 public function regenerate($destroy = false, $lifetime = null)
102 {
103 if (!$this->started) {
104 $this->start();
105 }
106
107 $this->metadataBag->stampNew($lifetime);
108 $this->id = $this->generateId();
109
110 return true;
111 }
112
113 /**
114 * {@inheritdoc}
115 */
116 public function getId()
117 {
118 return $this->id;
119 }
120
121 /**
122 * {@inheritdoc}
123 */
124 public function setId($id)
125 {
126 if ($this->started) {
127 throw new \LogicException('Cannot set session ID after the session has started.');
128 }
129
130 $this->id = $id;
131 }
132
133 /**
134 * {@inheritdoc}
135 */
136 public function getName()
137 {
138 return $this->name;
139 }
140
141 /**
142 * {@inheritdoc}
143 */
144 public function setName($name)
145 {
146 $this->name = $name;
147 }
148
149 /**
150 * {@inheritdoc}
151 */
152 public function save()
153 {
154 if (!$this->started || $this->closed) {
155 throw new \RuntimeException('Trying to save a session that was not started yet or was already closed.');
156 }
157 // nothing to do since we don't persist the session data
158 $this->closed = false;
159 $this->started = false;
160 }
161
162 /**
163 * {@inheritdoc}
164 */
165 public function clear()
166 {
167 // clear out the bags
168 foreach ($this->bags as $bag) {
169 $bag->clear();
170 }
171
172 // clear out the session
173 $this->data = [];
174
175 // reconnect the bags to the session
176 $this->loadSession();
177 }
178
179 /**
180 * {@inheritdoc}
181 */
182 public function registerBag(SessionBagInterface $bag)
183 {
184 $this->bags[$bag->getName()] = $bag;
185 }
186
187 /**
188 * {@inheritdoc}
189 */
190 public function getBag($name)
191 {
192 if (!isset($this->bags[$name])) {
193 throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
194 }
195
196 if (!$this->started) {
197 $this->start();
198 }
199
200 return $this->bags[$name];
201 }
202
203 /**
204 * {@inheritdoc}
205 */
206 public function isStarted()
207 {
208 return $this->started;
209 }
210
211 public function setMetadataBag(MetadataBag $bag = null)
212 {
213 if (null === $bag) {
214 $bag = new MetadataBag();
215 }
216
217 $this->metadataBag = $bag;
218 }
219
220 /**
221 * Gets the MetadataBag.
222 *
223 * @return MetadataBag
224 */
225 public function getMetadataBag()
226 {
227 return $this->metadataBag;
228 }
229
230 /**
231 * Generates a session ID.
232 *
233 * This doesn't need to be particularly cryptographically secure since this is just
234 * a mock.
235 *
236 * @return string
237 */
238 protected function generateId()
239 {
240 return hash('sha256', uniqid('ss_mock_', true));
241 }
242
243 protected function loadSession()
244 {
245 $bags = array_merge($this->bags, [$this->metadataBag]);
246
247 foreach ($bags as $bag) {
248 $key = $bag->getStorageKey();
249 $this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : [];
250 $bag->initialize($this->data[$key]);
251 }
252
253 $this->started = true;
254 $this->closed = false;
255 }
256 }
257