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