Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

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