Handler
1 year ago
Proxy
1 year ago
MetadataBag.php
2 years ago
MockArraySessionStorage.php
1 year ago
MockFileSessionStorage.php
1 year ago
MockFileSessionStorageFactory.php
2 years ago
NativeSessionStorage.php
1 year ago
NativeSessionStorageFactory.php
1 year ago
PhpBridgeSessionStorage.php
1 year ago
PhpBridgeSessionStorageFactory.php
1 year ago
ServiceSessionFactory.php
1 year ago
SessionStorageFactoryInterface.php
2 years ago
SessionStorageInterface.php
1 year ago
MetadataBag.php
148 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\Storage; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Session\SessionBagInterface; |
| 14 | /** |
| 15 | * Metadata container. |
| 16 | * |
| 17 | * Adds metadata to the session. |
| 18 | * |
| 19 | * @author Drak <drak@zikula.org> |
| 20 | */ |
| 21 | class MetadataBag implements SessionBagInterface |
| 22 | { |
| 23 | public const CREATED = 'c'; |
| 24 | public const UPDATED = 'u'; |
| 25 | public const LIFETIME = 'l'; |
| 26 | /** |
| 27 | * @var string |
| 28 | */ |
| 29 | private $name = '__metadata'; |
| 30 | /** |
| 31 | * @var string |
| 32 | */ |
| 33 | private $storageKey; |
| 34 | /** |
| 35 | * @var array |
| 36 | */ |
| 37 | protected $meta = [self::CREATED => 0, self::UPDATED => 0, self::LIFETIME => 0]; |
| 38 | /** |
| 39 | * Unix timestamp. |
| 40 | * |
| 41 | * @var int |
| 42 | */ |
| 43 | private $lastUsed; |
| 44 | /** |
| 45 | * @var int |
| 46 | */ |
| 47 | private $updateThreshold; |
| 48 | /** |
| 49 | * @param string $storageKey The key used to store bag in the session |
| 50 | * @param int $updateThreshold The time to wait between two UPDATED updates |
| 51 | */ |
| 52 | public function __construct(string $storageKey = '_sf2_meta', int $updateThreshold = 0) |
| 53 | { |
| 54 | $this->storageKey = $storageKey; |
| 55 | $this->updateThreshold = $updateThreshold; |
| 56 | } |
| 57 | /** |
| 58 | * {@inheritdoc} |
| 59 | */ |
| 60 | public function initialize(array &$array) |
| 61 | { |
| 62 | $this->meta =& $array; |
| 63 | if (isset($array[self::CREATED])) { |
| 64 | $this->lastUsed = $this->meta[self::UPDATED]; |
| 65 | $timeStamp = time(); |
| 66 | if ($timeStamp - $array[self::UPDATED] >= $this->updateThreshold) { |
| 67 | $this->meta[self::UPDATED] = $timeStamp; |
| 68 | } |
| 69 | } else { |
| 70 | $this->stampCreated(); |
| 71 | } |
| 72 | } |
| 73 | /** |
| 74 | * Gets the lifetime that the session cookie was set with. |
| 75 | * |
| 76 | * @return int |
| 77 | */ |
| 78 | public function getLifetime() |
| 79 | { |
| 80 | return $this->meta[self::LIFETIME]; |
| 81 | } |
| 82 | /** |
| 83 | * Stamps a new session's metadata. |
| 84 | * |
| 85 | * @param int|null $lifetime Sets the cookie lifetime for the session cookie. A null value |
| 86 | * will leave the system settings unchanged, 0 sets the cookie |
| 87 | * to expire with browser session. Time is in seconds, and is |
| 88 | * not a Unix timestamp. |
| 89 | */ |
| 90 | public function stampNew(?int $lifetime = null) |
| 91 | { |
| 92 | $this->stampCreated($lifetime); |
| 93 | } |
| 94 | /** |
| 95 | * {@inheritdoc} |
| 96 | */ |
| 97 | public function getStorageKey() |
| 98 | { |
| 99 | return $this->storageKey; |
| 100 | } |
| 101 | /** |
| 102 | * Gets the created timestamp metadata. |
| 103 | * |
| 104 | * @return int Unix timestamp |
| 105 | */ |
| 106 | public function getCreated() |
| 107 | { |
| 108 | return $this->meta[self::CREATED]; |
| 109 | } |
| 110 | /** |
| 111 | * Gets the last used metadata. |
| 112 | * |
| 113 | * @return int Unix timestamp |
| 114 | */ |
| 115 | public function getLastUsed() |
| 116 | { |
| 117 | return $this->lastUsed; |
| 118 | } |
| 119 | /** |
| 120 | * {@inheritdoc} |
| 121 | */ |
| 122 | public function clear() |
| 123 | { |
| 124 | // nothing to do |
| 125 | return null; |
| 126 | } |
| 127 | /** |
| 128 | * {@inheritdoc} |
| 129 | */ |
| 130 | public function getName() |
| 131 | { |
| 132 | return $this->name; |
| 133 | } |
| 134 | /** |
| 135 | * Sets name. |
| 136 | */ |
| 137 | public function setName(string $name) |
| 138 | { |
| 139 | $this->name = $name; |
| 140 | } |
| 141 | private function stampCreated(?int $lifetime = null) : void |
| 142 | { |
| 143 | $timeStamp = time(); |
| 144 | $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp; |
| 145 | $this->meta[self::LIFETIME] = $lifetime ?? (int) \ini_get('session.cookie_lifetime'); |
| 146 | } |
| 147 | } |
| 148 |