Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 = [];
54
 
55
    /**
56
     * @var MetadataBag
57
     */
58
    protected $metadataBag;
59
 
60
    /**
61
     * @var array|SessionBagInterface[]
62
     */
63
    protected $bags = [];
64
 
65
    public function __construct(string $name = 'MOCKSESSID', MetadataBag $metaBag = null)
66
    {
67
        $this->name = $name;
68
        $this->setMetadataBag($metaBag);
69
    }
70
 
71
    public function setSessionData(array $array)
72
    {
73
        $this->data = $array;
74
    }
75
 
76
    public function start(): bool
77
    {
78
        if ($this->started) {
79
            return true;
80
        }
81
 
82
        if (empty($this->id)) {
83
            $this->id = $this->generateId();
84
        }
85
 
86
        $this->loadSession();
87
 
88
        return true;
89
    }
90
 
91
    public function regenerate(bool $destroy = false, int $lifetime = null): bool
92
    {
93
        if (!$this->started) {
94
            $this->start();
95
        }
96
 
97
        $this->metadataBag->stampNew($lifetime);
98
        $this->id = $this->generateId();
99
 
100
        return true;
101
    }
102
 
103
    public function getId(): string
104
    {
105
        return $this->id;
106
    }
107
 
108
    public function setId(string $id)
109
    {
110
        if ($this->started) {
111
            throw new \LogicException('Cannot set session ID after the session has started.');
112
        }
113
 
114
        $this->id = $id;
115
    }
116
 
117
    public function getName(): string
118
    {
119
        return $this->name;
120
    }
121
 
122
    public function setName(string $name)
123
    {
124
        $this->name = $name;
125
    }
126
 
127
    public function save()
128
    {
129
        if (!$this->started || $this->closed) {
130
            throw new \RuntimeException('Trying to save a session that was not started yet or was already closed.');
131
        }
132
        // nothing to do since we don't persist the session data
133
        $this->closed = false;
134
        $this->started = false;
135
    }
136
 
137
    public function clear()
138
    {
139
        // clear out the bags
140
        foreach ($this->bags as $bag) {
141
            $bag->clear();
142
        }
143
 
144
        // clear out the session
145
        $this->data = [];
146
 
147
        // reconnect the bags to the session
148
        $this->loadSession();
149
    }
150
 
151
    public function registerBag(SessionBagInterface $bag)
152
    {
153
        $this->bags[$bag->getName()] = $bag;
154
    }
155
 
156
    public function getBag(string $name): SessionBagInterface
157
    {
158
        if (!isset($this->bags[$name])) {
159
            throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
160
        }
161
 
162
        if (!$this->started) {
163
            $this->start();
164
        }
165
 
166
        return $this->bags[$name];
167
    }
168
 
169
    public function isStarted(): bool
170
    {
171
        return $this->started;
172
    }
173
 
174
    public function setMetadataBag(MetadataBag $bag = null)
175
    {
176
        if (1 > \func_num_args()) {
177
            trigger_deprecation('symfony/http-foundation', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
178
        }
179
        $this->metadataBag = $bag ?? new MetadataBag();
180
    }
181
 
182
    /**
183
     * Gets the MetadataBag.
184
     */
185
    public function getMetadataBag(): MetadataBag
186
    {
187
        return $this->metadataBag;
188
    }
189
 
190
    /**
191
     * Generates a session ID.
192
     *
193
     * This doesn't need to be particularly cryptographically secure since this is just
194
     * a mock.
195
     */
196
    protected function generateId(): string
197
    {
198
        return hash('sha256', uniqid('ss_mock_', true));
199
    }
200
 
201
    protected function loadSession()
202
    {
203
        $bags = array_merge($this->bags, [$this->metadataBag]);
204
 
205
        foreach ($bags as $bag) {
206
            $key = $bag->getStorageKey();
207
            $this->data[$key] ??= [];
208
            $bag->initialize($this->data[$key]);
209
        }
210
 
211
        $this->started = true;
212
        $this->closed = false;
213
    }
214
}