Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
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 = array();
054
055 /**
056 * @var MetadataBag
057 */
058 protected $metadataBag;
059
060 /**
061 * @var array
062 */
063 protected $bags;
064
065 /**
066 * Constructor.
067 *
068 * @param string $name Session name
069 * @param MetadataBag $metaBag MetadataBag instance.
070 */
071 public function __construct($name = 'MOCKSESSID', MetadataBag $metaBag = null)
072 {
073 $this->name = $name;
074 $this->setMetadataBag($metaBag);
075 }
076
077 /**
078 * Sets the session data.
079 *
080 * @param array $array
081 */
082 public function setSessionData(array $array)
083 {
084 $this->data = $array;
085 }
086
087 /**
088 * {@inheritdoc}
089 */
090 public function start()
091 {
092 if ($this->started && !$this->closed) {
093 return true;
094 }
095
096 if (empty($this->id)) {
097 $this->id = $this->generateId();
098 }
099
100 $this->loadSession();
101
102 return true;
103 }
104
105 /**
106 * {@inheritdoc}
107 */
108 public function regenerate($destroy = false, $lifetime = null)
109 {
110 if (!$this->started) {
111 $this->start();
112 }
113
114 $this->metadataBag->stampNew($lifetime);
115 $this->id = $this->generateId();
116
117 return true;
118 }
119
120 /**
121 * {@inheritdoc}
122 */
123 public function getId()
124 {
125 return $this->id;
126 }
127
128 /**
129 * {@inheritdoc}
130 */
131 public function setId($id)
132 {
133 if ($this->started) {
134 throw new \LogicException('Cannot set session ID after the session has started.');
135 }
136
137 $this->id = $id;
138 }
139
140 /**
141 * {@inheritdoc}
142 */
143 public function getName()
144 {
145 return $this->name;
146 }
147
148 /**
149 * {@inheritdoc}
150 */
151 public function setName($name)
152 {
153 $this->name = $name;
154 }
155
156 /**
157 * {@inheritdoc}
158 */
159 public function save()
160 {
161 if (!$this->started || $this->closed) {
162 throw new \RuntimeException("Trying to save a session that was not started yet or was already closed");
163 }
164 // nothing to do since we don't persist the session data
165 $this->closed = false;
166 $this->started = false;
167 }
168
169 /**
170 * {@inheritdoc}
171 */
172 public function clear()
173 {
174 // clear out the bags
175 foreach ($this->bags as $bag) {
176 $bag->clear();
177 }
178
179 // clear out the session
180 $this->data = array();
181
182 // reconnect the bags to the session
183 $this->loadSession();
184 }
185
186 /**
187 * {@inheritdoc}
188 */
189 public function registerBag(SessionBagInterface $bag)
190 {
191 $this->bags[$bag->getName()] = $bag;
192 }
193
194 /**
195 * {@inheritdoc}
196 */
197 public function getBag($name)
198 {
199 if (!isset($this->bags[$name])) {
200 throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
201 }
202
203 if (!$this->started) {
204 $this->start();
205 }
206
207 return $this->bags[$name];
208 }
209
210 /**
211 * {@inheritdoc}
212 */
213 public function isStarted()
214 {
215 return $this->started;
216 }
217
218 /**
219 * Sets the MetadataBag.
220 *
221 * @param MetadataBag $bag
222 */
223 public function setMetadataBag(MetadataBag $bag = null)
224 {
225 if (null === $bag) {
226 $bag = new MetadataBag();
227 }
228
229 $this->metadataBag = $bag;
230 }
231
232 /**
233 * Gets the MetadataBag.
234 *
235 * @return MetadataBag
236 */
237 public function getMetadataBag()
238 {
239 return $this->metadataBag;
240 }
241
242 /**
243 * Generates a session ID.
244 *
245 * This doesn't need to be particularly cryptographically secure since this is just
246 * a mock.
247 *
248 * @return string
249 */
250 protected function generateId()
251 {
252 return sha1(uniqid(mt_rand()));
253 }
254
255 protected function loadSession()
256 {
257 $bags = array_merge($this->bags, array($this->metadataBag));
258
259 foreach ($bags as $bag) {
260 $key = $bag->getStorageKey();
261 $this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : array();
262 $bag->initialize($this->data[$key]);
263 }
264
265 $this->started = true;
266 $this->closed = false;
267 }
268 }
269