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