Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/Session/Storage/MetadataBag.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\Storage;
13
14 use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
15
16 /**
17 * Metadata container.
18 *
19 * Adds metadata to the session.
20 *
21 * @author Drak <[email protected]>
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 * @param string $storageKey The key used to store bag in the session
36 * @param int $updateThreshold The time to wait between two UPDATED updates
37 * @param int|null $cookieLifetime The configured cookie lifetime; null to read from php.ini
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 * Gets the lifetime that the session cookie was set with.
64 */
65 public function getLifetime(): int
66 {
67 return $this->meta[self::LIFETIME];
68 }
69
70 /**
71 * Stamps a new session's metadata.
72 *
73 * @param int|null $lifetime Sets the cookie lifetime for the session cookie. A null value
74 * will leave the system settings unchanged, 0 sets the cookie
75 * to expire with browser session. Time is in seconds, and is
76 * not a Unix timestamp.
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 * Gets the created timestamp metadata.
90 *
91 * @return int Unix timestamp
92 */
93 public function getCreated(): int
94 {
95 return $this->meta[self::CREATED];
96 }
97
98 /**
99 * Gets the last used metadata.
100 *
101 * @return int Unix timestamp
102 */
103 public function getLastUsed(): int
104 {
105 return $this->lastUsed;
106 }
107
108 public function clear(): mixed
109 {
110 // nothing to do
111 return null;
112 }
113
114 public function getName(): string
115 {
116 return $this->name;
117 }
118
119 /**
120 * Sets name.
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 }