Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/Session/Session.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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\MetadataBag;
19 use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
20 use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
21
22 // Help opcache.preload discover always-needed symbols
23 class_exists(AttributeBag::class);
24 class_exists(FlashBag::class);
25 class_exists(SessionBagProxy::class);
26
27 /**
28 * @author Fabien Potencier <[email protected]>
29 * @author Drak <[email protected]>
30 *
31 * @implements \IteratorAggregate<string, mixed>
32 */
33 class Session implements FlashBagAwareSessionInterface, \IteratorAggregate, \Countable
34 {
35 protected SessionStorageInterface $storage;
36
37 private string $flashName;
38 private string $attributeName;
39 private array $data = [];
40 private int $usageIndex = 0;
41 private ?\Closure $usageReporter;
42
43 public function __construct(?SessionStorageInterface $storage = null, ?AttributeBagInterface $attributes = null, ?FlashBagInterface $flashes = null, ?callable $usageReporter = null)
44 {
45 $this->storage = $storage ?? new NativeSessionStorage();
46 $this->usageReporter = null === $usageReporter ? null : $usageReporter(...);
47
48 $attributes ??= new AttributeBag();
49 $this->attributeName = $attributes->getName();
50 $this->registerBag($attributes);
51
52 $flashes ??= new FlashBag();
53 $this->flashName = $flashes->getName();
54 $this->registerBag($flashes);
55 }
56
57 public function start(): bool
58 {
59 return $this->storage->start();
60 }
61
62 public function has(string $name): bool
63 {
64 return $this->getAttributeBag()->has($name);
65 }
66
67 public function get(string $name, mixed $default = null): mixed
68 {
69 return $this->getAttributeBag()->get($name, $default);
70 }
71
72 public function set(string $name, mixed $value): void
73 {
74 $this->getAttributeBag()->set($name, $value);
75 }
76
77 public function all(): array
78 {
79 return $this->getAttributeBag()->all();
80 }
81
82 public function replace(array $attributes): void
83 {
84 $this->getAttributeBag()->replace($attributes);
85 }
86
87 public function remove(string $name): mixed
88 {
89 return $this->getAttributeBag()->remove($name);
90 }
91
92 public function clear(): void
93 {
94 $this->getAttributeBag()->clear();
95 }
96
97 public function isStarted(): bool
98 {
99 return $this->storage->isStarted();
100 }
101
102 /**
103 * Returns an iterator for attributes.
104 *
105 * @return \ArrayIterator<string, mixed>
106 */
107 public function getIterator(): \ArrayIterator
108 {
109 return new \ArrayIterator($this->getAttributeBag()->all());
110 }
111
112 /**
113 * Returns the number of attributes.
114 */
115 public function count(): int
116 {
117 return \count($this->getAttributeBag()->all());
118 }
119
120 public function &getUsageIndex(): int
121 {
122 return $this->usageIndex;
123 }
124
125 /**
126 * @internal
127 */
128 public function isEmpty(): bool
129 {
130 if ($this->isStarted()) {
131 ++$this->usageIndex;
132 if ($this->usageReporter && 0 <= $this->usageIndex) {
133 ($this->usageReporter)();
134 }
135 }
136 foreach ($this->data as &$data) {
137 if ($data) {
138 return false;
139 }
140 }
141
142 return true;
143 }
144
145 public function invalidate(?int $lifetime = null): bool
146 {
147 $this->storage->clear();
148
149 return $this->migrate(true, $lifetime);
150 }
151
152 public function migrate(bool $destroy = false, ?int $lifetime = null): bool
153 {
154 return $this->storage->regenerate($destroy, $lifetime);
155 }
156
157 public function save(): void
158 {
159 $this->storage->save();
160 }
161
162 public function getId(): string
163 {
164 return $this->storage->getId();
165 }
166
167 public function setId(string $id): void
168 {
169 if ($this->storage->getId() !== $id) {
170 $this->storage->setId($id);
171 }
172 }
173
174 public function getName(): string
175 {
176 return $this->storage->getName();
177 }
178
179 public function setName(string $name): void
180 {
181 $this->storage->setName($name);
182 }
183
184 public function getMetadataBag(): MetadataBag
185 {
186 ++$this->usageIndex;
187 if ($this->usageReporter && 0 <= $this->usageIndex) {
188 ($this->usageReporter)();
189 }
190
191 return $this->storage->getMetadataBag();
192 }
193
194 public function registerBag(SessionBagInterface $bag): void
195 {
196 $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex, $this->usageReporter));
197 }
198
199 public function getBag(string $name): SessionBagInterface
200 {
201 $bag = $this->storage->getBag($name);
202
203 return method_exists($bag, 'getBag') ? $bag->getBag() : $bag;
204 }
205
206 /**
207 * Gets the flashbag interface.
208 */
209 public function getFlashBag(): FlashBagInterface
210 {
211 return $this->getBag($this->flashName);
212 }
213
214 /**
215 * Gets the attributebag interface.
216 *
217 * Note that this method was added to help with IDE autocompletion.
218 */
219 private function getAttributeBag(): AttributeBagInterface
220 {
221 return $this->getBag($this->attributeName);
222 }
223 }