027f4efffce51b9958b3cda5133d162bfcad888e
[yaffs-website] / vendor / symfony / http-foundation / Session / Storage / MockArraySessionStorage.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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 <fabien@symfony.com>
25  * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
26  * @author Drak <drak@zikula.org>
27  */
28 class MockArraySessionStorage implements SessionStorageInterface
29 {
30     /**
31      * @var string
32      */
33     protected $id = '';
34
35     /**
36      * @var string
37      */
38     protected $name;
39
40     /**
41      * @var bool
42      */
43     protected $started = false;
44
45     /**
46      * @var bool
47      */
48     protected $closed = false;
49
50     /**
51      * @var array
52      */
53     protected $data = array();
54
55     /**
56      * @var MetadataBag
57      */
58     protected $metadataBag;
59
60     /**
61      * @var array|SessionBagInterface[]
62      */
63     protected $bags = array();
64
65     /**
66      * @param string      $name    Session name
67      * @param MetadataBag $metaBag MetadataBag instance
68      */
69     public function __construct($name = 'MOCKSESSID', MetadataBag $metaBag = null)
70     {
71         $this->name = $name;
72         $this->setMetadataBag($metaBag);
73     }
74
75     public function setSessionData(array $array)
76     {
77         $this->data = $array;
78     }
79
80     /**
81      * {@inheritdoc}
82      */
83     public function start()
84     {
85         if ($this->started) {
86             return true;
87         }
88
89         if (empty($this->id)) {
90             $this->id = $this->generateId();
91         }
92
93         $this->loadSession();
94
95         return true;
96     }
97
98     /**
99      * {@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 = array();
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, array($this->metadataBag));
246
247         foreach ($bags as $bag) {
248             $key = $bag->getStorageKey();
249             $this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : array();
250             $bag->initialize($this->data[$key]);
251         }
252
253         $this->started = true;
254         $this->closed = false;
255     }
256 }