-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathSessionSection.php
More file actions
230 lines (188 loc) · 5.61 KB
/
SessionSection.php
File metadata and controls
230 lines (188 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Http;
use Nette;
/**
* Session section.
*/
class SessionSection implements \IteratorAggregate, \ArrayAccess
{
/**
* Do not call directly. Use Session::getSection().
*/
public function __construct(
private readonly Session $session,
private readonly string $name,
) {
}
/**
* Returns an iterator over all section variables.
*/
public function getIterator(): \Iterator
{
$this->session->autoStart(false);
return new \ArrayIterator($this->getData() ?? []);
}
/**
* Sets a variable in this session section.
*/
public function set(string $name, mixed $value, ?string $expire = null): void
{
if ($value === null) {
$this->remove($name);
} else {
$this->session->autoStart(true);
$this->getData()[$name] = $value;
$this->setExpiration($expire, $name);
}
}
/**
* Gets a variable from this session section.
*/
public function get(string $name): mixed
{
if (func_num_args() > 1) {
throw new \ArgumentCountError(__METHOD__ . '() expects 1 arguments, given more.');
}
$this->session->autoStart(false);
return $this->getData()[$name] ?? null;
}
/**
* Removes a variable or whole section.
* @param string|string[]|null $name
*/
public function remove(string|array|null $name = null): void
{
$this->session->autoStart(false);
if (func_num_args() > 1) {
throw new \ArgumentCountError(__METHOD__ . '() expects at most 1 arguments, given more.');
} elseif (func_num_args()) {
$data = &$this->getData();
$meta = &$this->getMeta();
foreach ((array) $name as $name) {
unset($data[$name], $meta[$name]);
}
} else {
unset($_SESSION['__NF']['DATA'][$this->name], $_SESSION['__NF']['META'][$this->name]);
}
}
/**
* Sets a variable in this session section.
* @deprecated use set() instead
*/
public function __set(string $name, $value): void
{
trigger_error("Writing to \$session->$name is deprecated, use \$session->set('$name', \$value) instead", E_USER_DEPRECATED);
$this->session->autoStart(true);
$this->getData()[$name] = $value;
}
/**
* Gets a variable from this session section.
* @deprecated use get() instead
*/
public function &__get(string $name): mixed
{
trigger_error("Reading from \$session->$name is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
$this->session->autoStart(true);
$data = &$this->getData();
return $data[$name];
}
/**
* Determines whether a variable in this session section is set.
* @deprecated use get() instead
*/
public function __isset(string $name): bool
{
trigger_error("Using \$session->$name is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
$this->session->autoStart(false);
return isset($this->getData()[$name]);
}
/**
* Unsets a variable in this session section.
* @deprecated use remove() instead
*/
public function __unset(string $name): void
{
trigger_error("Unset(\$session->$name) is deprecated, use \$session->remove('$name') instead", E_USER_DEPRECATED);
$this->remove($name);
}
/**
* Sets a variable in this session section.
* @deprecated use set() instead
*/
public function offsetSet($name, $value): void
{
trigger_error("Writing to \$session['$name'] is deprecated, use \$session->set('$name', \$value) instead", E_USER_DEPRECATED);
$this->__set($name, $value);
}
/**
* Gets a variable from this session section.
* @deprecated use get() instead
*/
public function offsetGet($name): mixed
{
trigger_error("Reading from \$session['$name'] is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
return $this->get($name);
}
/**
* Determines whether a variable in this session section is set.
* @deprecated use get() instead
*/
public function offsetExists($name): bool
{
trigger_error("Using \$session['$name'] is deprecated, use \$session->get('$name') instead", E_USER_DEPRECATED);
return $this->__isset($name);
}
/**
* Unsets a variable in this session section.
* @deprecated use remove() instead
*/
public function offsetUnset($name): void
{
trigger_error("Unset(\$session['$name']) is deprecated, use \$session->remove('$name') instead", E_USER_DEPRECATED);
$this->remove($name);
}
/**
* Sets the expiration of the section or specific variables.
* @param string|string[]|null $variables list of variables / single variable to expire
*/
public function setExpiration(?string $expire, string|array|null $variables = null): static
{
$this->session->autoStart((bool) $expire);
$meta = &$this->getMeta();
if ($expire) {
$expire = Nette\Utils\DateTime::from($expire)->format('U');
$max = (int) ini_get('session.gc_maxlifetime');
if (
$max !== 0 // 0 - unlimited in memcache handler
&& ($expire - time() > $max + 3) // 3 - bulgarian constant
) {
trigger_error("The expiration time is greater than the session expiration $max seconds");
}
}
foreach (is_array($variables) ? $variables : [$variables] as $variable) {
$meta[$variable]['T'] = $expire ?: null;
}
return $this;
}
/**
* Removes the expiration from the section or specific variables.
* @param string|string[]|null $variables list of variables / single variable to expire
*/
public function removeExpiration(string|array|null $variables = null): void
{
$this->setExpiration(null, $variables);
}
private function &getData()
{
return $_SESSION['__NF']['DATA'][$this->name];
}
private function &getMeta()
{
return $_SESSION['__NF']['META'][$this->name];
}
}