| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation\Session\Storage; |
| 13 |
|
| 14 |
use Symfony\Component\HttpFoundation\Session\SessionBagInterface; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class MetadataBag implements SessionBagInterface |
| 24 |
{ |
| 25 |
public const CREATED = 'c'; |
| 26 |
public const UPDATED = 'u'; |
| 27 |
public const LIFETIME = 'l'; |
| 28 |
|
| 29 |
protected array $meta = [self::CREATED => 0, self::UPDATED => 0, self::LIFETIME => 0]; |
| 30 |
|
| 31 |
private string $name = '__metadata'; |
| 32 |
private int $lastUsed; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct( |
| 40 |
private string $storageKey = '_sf2_meta', |
| 41 |
private int $updateThreshold = 0, |
| 42 |
private ?int $cookieLifetime = null, |
| 43 |
) { |
| 44 |
} |
| 45 |
|
| 46 |
public function initialize(array &$array): void |
| 47 |
{ |
| 48 |
$this->meta = &$array; |
| 49 |
|
| 50 |
if (isset($array[self::CREATED])) { |
| 51 |
$this->lastUsed = $this->meta[self::UPDATED]; |
| 52 |
|
| 53 |
$timeStamp = time(); |
| 54 |
if ($timeStamp - $array[self::UPDATED] >= $this->updateThreshold) { |
| 55 |
$this->meta[self::UPDATED] = $timeStamp; |
| 56 |
} |
| 57 |
} else { |
| 58 |
$this->stampCreated(); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function getLifetime(): int |
| 66 |
{ |
| 67 |
return $this->meta[self::LIFETIME]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function stampNew(?int $lifetime = null): void |
| 79 |
{ |
| 80 |
$this->stampCreated($lifetime); |
| 81 |
} |
| 82 |
|
| 83 |
public function getStorageKey(): string |
| 84 |
{ |
| 85 |
return $this->storageKey; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function getCreated(): int |
| 94 |
{ |
| 95 |
return $this->meta[self::CREATED]; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function getLastUsed(): int |
| 104 |
{ |
| 105 |
return $this->lastUsed; |
| 106 |
} |
| 107 |
|
| 108 |
public function clear(): mixed |
| 109 |
{ |
| 110 |
|
| 111 |
return null; |
| 112 |
} |
| 113 |
|
| 114 |
public function getName(): string |
| 115 |
{ |
| 116 |
return $this->name; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function setName(string $name): void |
| 123 |
{ |
| 124 |
$this->name = $name; |
| 125 |
} |
| 126 |
|
| 127 |
private function stampCreated(?int $lifetime = null): void |
| 128 |
{ |
| 129 |
$timeStamp = time(); |
| 130 |
$this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp; |
| 131 |
$this->meta[self::LIFETIME] = $lifetime ?? $this->cookieLifetime ?? (int) \ini_get('session.cookie_lifetime'); |
| 132 |
} |
| 133 |
} |