1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of Aura for PHP. |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
namespace Aura\Session; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* A session segment; lazy-loads from the session. |
14
|
|
|
* |
15
|
|
|
* @package Aura.Session |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class Segment implements SegmentInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* The session manager. |
23
|
|
|
* |
24
|
|
|
* @var Session |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
protected $session; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* The segment name. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
* |
35
|
|
|
*/ |
36
|
|
|
protected $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* Constructor. |
41
|
|
|
* |
42
|
|
|
* @param Session $session The session manager. |
43
|
|
|
* |
44
|
|
|
* @param string $name The segment name. |
45
|
|
|
* |
46
|
|
|
*/ |
47
|
20 |
|
public function __construct(Session $session, $name) |
48
|
|
|
{ |
49
|
20 |
|
$this->session = $session; |
50
|
20 |
|
$this->name = $name; |
51
|
20 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
* Returns the value of a key in the segment. |
56
|
|
|
* |
57
|
|
|
* @param string $key The key in the segment. |
58
|
|
|
* |
59
|
|
|
* @param mixed $alt An alternative value to return if the key is not set. |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
* |
63
|
|
|
*/ |
64
|
10 |
|
public function get($key, $alt = null) |
|
|
|
|
65
|
|
|
{ |
66
|
10 |
|
$this->resumeSession(); |
67
|
10 |
|
return isset($_SESSION[$this->name][$key]) |
68
|
10 |
|
? $_SESSION[$this->name][$key] |
69
|
10 |
|
: $alt; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* Returns the entire segment. |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
* |
78
|
|
|
*/ |
79
|
|
|
public function getSegment() |
|
|
|
|
80
|
|
|
{ |
81
|
11 |
|
$this->resumeSession(); |
82
|
|
|
return isset($_SESSION[$this->name]) |
83
|
11 |
|
? $_SESSION[$this->name] |
84
|
11 |
|
: null; |
85
|
11 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
* Sets the value of a key in the segment. |
90
|
|
|
* |
91
|
|
|
* @param string $key The key to set. |
92
|
|
|
* |
93
|
|
|
* @param mixed $val The value to set it to. |
94
|
2 |
|
* |
95
|
|
|
*/ |
96
|
2 |
|
public function set($key, $val) |
|
|
|
|
97
|
1 |
|
{ |
98
|
1 |
|
$this->resumeOrStartSession(); |
99
|
2 |
|
$_SESSION[$this->name][$key] = $val; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* |
104
|
|
|
* Clear all data from the segment. |
105
|
|
|
* |
106
|
|
|
* @return null |
107
|
|
|
* |
108
|
|
|
*/ |
109
|
|
|
public function clear() |
|
|
|
|
110
|
3 |
|
{ |
111
|
|
|
if ($this->resumeSession()) { |
112
|
3 |
|
$_SESSION[$this->name] = array(); |
113
|
3 |
|
} |
114
|
3 |
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Remove a key from the segment, or remove the entire segment (including key) from the session |
119
|
|
|
* |
120
|
|
|
* @param null $key |
121
|
|
|
*/ |
122
|
|
|
public function remove($key = null) { |
|
|
|
|
123
|
|
|
if ($this->resumeSession()) { |
124
|
|
|
if($key){ |
125
|
|
|
if(isset($_SESSION[$this->name]) && array_key_exists($key, $_SESSION[$this->name])){ |
126
|
|
|
unset($_SESSION[$this->name][$key]); |
127
|
3 |
|
} |
128
|
|
|
} else { |
129
|
3 |
|
unset($_SESSION[$this->name]); |
130
|
3 |
|
} |
131
|
3 |
|
} |
132
|
3 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* |
136
|
|
|
* Sets a flash value for the *next* request. |
137
|
|
|
* |
138
|
|
|
* @param string $key The key for the flash value. |
139
|
|
|
* |
140
|
|
|
* @param mixed $val The flash value itself. |
141
|
|
|
* |
142
|
2 |
|
*/ |
143
|
|
|
public function setFlash($key, $val) |
|
|
|
|
144
|
2 |
|
{ |
145
|
2 |
|
$this->resumeOrStartSession(); |
146
|
2 |
|
$_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val; |
147
|
2 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* |
151
|
|
|
* Gets the flash value for a key in the *current* request. |
152
|
|
|
* |
153
|
|
|
* @param string $key The key for the flash value. |
154
|
|
|
* |
155
|
|
|
* @param mixed $alt An alternative value to return if the key is not set. |
156
|
|
|
* |
157
|
|
|
* @return mixed The flash value itself. |
158
|
|
|
* |
159
|
|
|
*/ |
160
|
2 |
|
public function getFlash($key, $alt = null) |
|
|
|
|
161
|
|
|
{ |
162
|
2 |
|
$this->resumeSession(); |
163
|
2 |
|
return isset($_SESSION[Session::FLASH_NOW][$this->name][$key]) |
164
|
2 |
|
? $_SESSION[Session::FLASH_NOW][$this->name][$key] |
165
|
2 |
|
: $alt; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* |
170
|
|
|
* Clears flash values for *only* the next request. |
171
|
|
|
* |
172
|
|
|
* @return null |
173
|
|
|
* |
174
|
|
|
*/ |
175
|
|
|
public function clearFlash() |
|
|
|
|
176
|
|
|
{ |
177
|
2 |
|
if ($this->resumeSession()) { |
178
|
|
|
$_SESSION[Session::FLASH_NEXT][$this->name] = array(); |
179
|
2 |
|
} |
180
|
2 |
|
} |
181
|
2 |
|
|
182
|
2 |
|
/** |
183
|
|
|
* |
184
|
|
|
* Gets the flash value for a key in the *next* request. |
185
|
|
|
* |
186
|
|
|
* @param string $key The key for the flash value. |
187
|
|
|
* |
188
|
|
|
* @param mixed $alt An alternative value to return if the key is not set. |
189
|
|
|
* |
190
|
|
|
* @return mixed The flash value itself. |
191
|
2 |
|
* |
192
|
|
|
*/ |
193
|
2 |
|
public function getFlashNext($key, $alt = null) |
|
|
|
|
194
|
2 |
|
{ |
195
|
2 |
|
$this->resumeSession(); |
196
|
2 |
|
return isset($_SESSION[Session::FLASH_NEXT][$this->name][$key]) |
197
|
2 |
|
? $_SESSION[Session::FLASH_NEXT][$this->name][$key] |
198
|
|
|
: $alt; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* |
203
|
|
|
* Sets a flash value for the *next* request *and* the current one. |
204
|
|
|
* |
205
|
|
|
* @param string $key The key for the flash value. |
206
|
|
|
* |
207
|
2 |
|
* @param mixed $val The flash value itself. |
208
|
|
|
* |
209
|
2 |
|
*/ |
210
|
2 |
|
public function setFlashNow($key, $val) |
|
|
|
|
211
|
2 |
|
{ |
212
|
2 |
|
$this->resumeOrStartSession(); |
213
|
2 |
|
$_SESSION[Session::FLASH_NOW][$this->name][$key] = $val; |
214
|
2 |
|
$_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val; |
215
|
2 |
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* |
219
|
|
|
* Clears flash values for *both* the next request *and* the current one. |
220
|
|
|
* |
221
|
|
|
* @return null |
222
|
|
|
* |
223
|
|
|
*/ |
224
|
|
|
public function clearFlashNow() |
|
|
|
|
225
|
18 |
|
{ |
226
|
|
|
if ($this->resumeSession()) { |
227
|
18 |
|
$_SESSION[Session::FLASH_NOW][$this->name] = array(); |
228
|
12 |
|
$_SESSION[Session::FLASH_NEXT][$this->name] = array(); |
229
|
12 |
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
15 |
|
/** |
233
|
|
|
* |
234
|
|
|
* Retains all the current flash values for the next request; values that |
235
|
|
|
* already exist for the next request take precedence. |
236
|
|
|
* |
237
|
|
|
* @return null |
238
|
|
|
* |
239
|
|
|
*/ |
240
|
|
|
public function keepFlash() |
|
|
|
|
241
|
|
|
{ |
242
|
15 |
|
if ($this->resumeSession()) { |
243
|
|
|
$_SESSION[Session::FLASH_NEXT][$this->name] = array_merge( |
244
|
15 |
|
$_SESSION[Session::FLASH_NEXT][$this->name], |
245
|
15 |
|
$_SESSION[Session::FLASH_NOW][$this->name] |
246
|
15 |
|
); |
247
|
|
|
} |
248
|
15 |
|
} |
249
|
15 |
|
|
250
|
15 |
|
/** |
251
|
|
|
* |
252
|
15 |
|
* Loads the segment only if the session has already been started, or if |
253
|
15 |
|
* a session is available (in which case it resumes the session first). |
254
|
15 |
|
* |
255
|
15 |
|
* @return bool |
256
|
|
|
* |
257
|
|
|
*/ |
258
|
|
|
protected function resumeSession() |
259
|
|
|
{ |
260
|
|
|
if ($this->session->isStarted() || $this->session->resume()) { |
261
|
|
|
$this->load(); |
262
|
|
|
return true; |
263
|
|
|
} |
264
|
14 |
|
|
265
|
|
|
return false; |
266
|
14 |
|
} |
267
|
12 |
|
|
268
|
12 |
|
/** |
269
|
12 |
|
* |
270
|
14 |
|
* Sets the segment properties to $_SESSION references. |
271
|
|
|
* |
272
|
|
|
* @return null |
273
|
|
|
* |
274
|
|
|
*/ |
275
|
|
|
protected function load() |
|
|
|
|
276
|
|
|
{ |
277
|
|
|
if (! isset($_SESSION[$this->name])) { |
278
|
|
|
$_SESSION[$this->name] = array(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
if (! isset($_SESSION[Session::FLASH_NOW][$this->name])) { |
282
|
|
|
$_SESSION[Session::FLASH_NOW][$this->name] = array(); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
if (! isset($_SESSION[Session::FLASH_NEXT][$this->name])) { |
286
|
|
|
$_SESSION[Session::FLASH_NEXT][$this->name] = array(); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* |
292
|
|
|
* Resumes a previous session, or starts a new one, and loads the segment. |
293
|
|
|
* |
294
|
|
|
* @return null |
295
|
|
|
* |
296
|
|
|
*/ |
297
|
|
|
protected function resumeOrStartSession() |
298
|
|
|
{ |
299
|
|
|
if (! $this->resumeSession()) { |
300
|
|
|
$this->session->start(); |
301
|
|
|
$this->load(); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: