Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / http-foundation / Session / Session.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;
13
14 use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
15 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
16 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
17 use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
18 use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
19 use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
20
21 /**
22  * @author Fabien Potencier <fabien@symfony.com>
23  * @author Drak <drak@zikula.org>
24  */
25 class Session implements SessionInterface, \IteratorAggregate, \Countable
26 {
27     protected $storage;
28
29     private $flashName;
30     private $attributeName;
31     private $data = array();
32     private $hasBeenStarted;
33
34     /**
35      * @param SessionStorageInterface $storage    A SessionStorageInterface instance
36      * @param AttributeBagInterface   $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
37      * @param FlashBagInterface       $flashes    A FlashBagInterface instance (defaults null for default FlashBag)
38      */
39     public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
40     {
41         $this->storage = $storage ?: new NativeSessionStorage();
42
43         $attributes = $attributes ?: new AttributeBag();
44         $this->attributeName = $attributes->getName();
45         $this->registerBag($attributes);
46
47         $flashes = $flashes ?: new FlashBag();
48         $this->flashName = $flashes->getName();
49         $this->registerBag($flashes);
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     public function start()
56     {
57         return $this->storage->start();
58     }
59
60     /**
61      * {@inheritdoc}
62      */
63     public function has($name)
64     {
65         return $this->getAttributeBag()->has($name);
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     public function get($name, $default = null)
72     {
73         return $this->getAttributeBag()->get($name, $default);
74     }
75
76     /**
77      * {@inheritdoc}
78      */
79     public function set($name, $value)
80     {
81         $this->getAttributeBag()->set($name, $value);
82     }
83
84     /**
85      * {@inheritdoc}
86      */
87     public function all()
88     {
89         return $this->getAttributeBag()->all();
90     }
91
92     /**
93      * {@inheritdoc}
94      */
95     public function replace(array $attributes)
96     {
97         $this->getAttributeBag()->replace($attributes);
98     }
99
100     /**
101      * {@inheritdoc}
102      */
103     public function remove($name)
104     {
105         return $this->getAttributeBag()->remove($name);
106     }
107
108     /**
109      * {@inheritdoc}
110      */
111     public function clear()
112     {
113         $this->getAttributeBag()->clear();
114     }
115
116     /**
117      * {@inheritdoc}
118      */
119     public function isStarted()
120     {
121         return $this->storage->isStarted();
122     }
123
124     /**
125      * Returns an iterator for attributes.
126      *
127      * @return \ArrayIterator An \ArrayIterator instance
128      */
129     public function getIterator()
130     {
131         return new \ArrayIterator($this->getAttributeBag()->all());
132     }
133
134     /**
135      * Returns the number of attributes.
136      *
137      * @return int The number of attributes
138      */
139     public function count()
140     {
141         return count($this->getAttributeBag()->all());
142     }
143
144     /**
145      * @return bool
146      *
147      * @internal
148      */
149     public function hasBeenStarted()
150     {
151         return $this->hasBeenStarted;
152     }
153
154     /**
155      * @return bool
156      *
157      * @internal
158      */
159     public function isEmpty()
160     {
161         foreach ($this->data as &$data) {
162             if (!empty($data)) {
163                 return false;
164             }
165         }
166
167         return true;
168     }
169
170     /**
171      * {@inheritdoc}
172      */
173     public function invalidate($lifetime = null)
174     {
175         $this->storage->clear();
176
177         return $this->migrate(true, $lifetime);
178     }
179
180     /**
181      * {@inheritdoc}
182      */
183     public function migrate($destroy = false, $lifetime = null)
184     {
185         return $this->storage->regenerate($destroy, $lifetime);
186     }
187
188     /**
189      * {@inheritdoc}
190      */
191     public function save()
192     {
193         $this->storage->save();
194     }
195
196     /**
197      * {@inheritdoc}
198      */
199     public function getId()
200     {
201         return $this->storage->getId();
202     }
203
204     /**
205      * {@inheritdoc}
206      */
207     public function setId($id)
208     {
209         $this->storage->setId($id);
210     }
211
212     /**
213      * {@inheritdoc}
214      */
215     public function getName()
216     {
217         return $this->storage->getName();
218     }
219
220     /**
221      * {@inheritdoc}
222      */
223     public function setName($name)
224     {
225         $this->storage->setName($name);
226     }
227
228     /**
229      * {@inheritdoc}
230      */
231     public function getMetadataBag()
232     {
233         return $this->storage->getMetadataBag();
234     }
235
236     /**
237      * {@inheritdoc}
238      */
239     public function registerBag(SessionBagInterface $bag)
240     {
241         $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->hasBeenStarted));
242     }
243
244     /**
245      * {@inheritdoc}
246      */
247     public function getBag($name)
248     {
249         return $this->storage->getBag($name)->getBag();
250     }
251
252     /**
253      * Gets the flashbag interface.
254      *
255      * @return FlashBagInterface
256      */
257     public function getFlashBag()
258     {
259         return $this->getBag($this->flashName);
260     }
261
262     /**
263      * Gets the attributebag interface.
264      *
265      * Note that this method was added to help with IDE autocompletion.
266      *
267      * @return AttributeBagInterface
268      */
269     private function getAttributeBag()
270     {
271         return $this->getBag($this->attributeName);
272     }
273 }