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
|
22 |
|
public function __construct(Session $session, $name) |
48
|
|
|
{ |
49
|
22 |
|
$this->session = $session; |
50
|
22 |
|
$this->name = $name; |
51
|
22 |
|
} |
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
|
11 |
|
public function get($key, $alt = null) |
|
|
|
|
65
|
|
|
{ |
66
|
11 |
|
$this->resumeSession(); |
67
|
11 |
|
return isset($_SESSION[$this->name][$key]) |
68
|
11 |
|
? $_SESSION[$this->name][$key] |
69
|
11 |
|
: $alt; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* Returns the entire segment. |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
* |
78
|
|
|
*/ |
79
|
1 |
|
public function getSegment() |
|
|
|
|
80
|
|
|
{ |
81
|
1 |
|
$this->resumeSession(); |
82
|
1 |
|
return isset($_SESSION[$this->name]) |
83
|
1 |
|
? $_SESSION[$this->name] |
84
|
1 |
|
: null; |
85
|
|
|
} |
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
|
|
|
* |
95
|
|
|
*/ |
96
|
14 |
|
public function set($key, $val) |
|
|
|
|
97
|
|
|
{ |
98
|
14 |
|
$this->resumeOrStartSession(); |
99
|
14 |
|
$_SESSION[$this->name][$key] = $val; |
100
|
14 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* |
104
|
|
|
* Append a value to a numeric key in the segment. |
105
|
|
|
* |
106
|
|
|
* @param mixed $val The value to append. |
107
|
|
|
* |
108
|
|
|
*/ |
109
|
1 |
|
public function add($val) |
|
|
|
|
110
|
|
|
{ |
111
|
1 |
|
$this->resumeOrStartSession(); |
112
|
1 |
|
$_SESSION[$this->name][] = $val; |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
* Clear all data from the segment. |
118
|
|
|
* |
119
|
|
|
* @return null |
120
|
|
|
* |
121
|
|
|
*/ |
122
|
2 |
|
public function clear() |
|
|
|
|
123
|
|
|
{ |
124
|
2 |
|
if ($this->resumeSession()) { |
125
|
1 |
|
$_SESSION[$this->name] = array(); |
126
|
1 |
|
} |
127
|
2 |
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Remove a key from the segment, or remove the entire segment (including key) from the session |
132
|
|
|
* |
133
|
|
|
* @param null $key |
134
|
|
|
*/ |
135
|
2 |
|
public function remove($key = null) { |
|
|
|
|
136
|
2 |
|
if ($this->resumeSession()) { |
137
|
2 |
|
if($key){ |
138
|
1 |
|
if(isset($_SESSION[$this->name]) && array_key_exists($key, $_SESSION[$this->name])){ |
139
|
1 |
|
unset($_SESSION[$this->name][$key]); |
140
|
1 |
|
} |
141
|
1 |
|
} else { |
142
|
1 |
|
unset($_SESSION[$this->name]); |
143
|
|
|
} |
144
|
2 |
|
} |
145
|
2 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* |
149
|
|
|
* Sets a flash value for the *next* request. |
150
|
|
|
* |
151
|
|
|
* @param string $key The key for the flash value. |
152
|
|
|
* |
153
|
|
|
* @param mixed $val The flash value itself. |
154
|
|
|
* |
155
|
|
|
*/ |
156
|
3 |
|
public function setFlash($key, $val) |
|
|
|
|
157
|
|
|
{ |
158
|
3 |
|
$this->resumeOrStartSession(); |
159
|
3 |
|
$_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val; |
160
|
3 |
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* |
165
|
|
|
* Append a flash value with a numeric key for the *next* request. |
166
|
|
|
* |
167
|
|
|
* @param mixed $val The flash value itself. |
168
|
|
|
* |
169
|
|
|
*/ |
170
|
1 |
|
public function addFlash($val) |
|
|
|
|
171
|
|
|
{ |
172
|
1 |
|
$this->resumeOrStartSession(); |
173
|
1 |
|
$_SESSION[Session::FLASH_NEXT][$this->name][] = $val; |
174
|
1 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* |
178
|
|
|
* Gets the flash value for a key in the *current* request. |
179
|
|
|
* |
180
|
|
|
* @param string $key The key for the flash value. |
181
|
|
|
* |
182
|
|
|
* @param mixed $alt An alternative value to return if the key is not set. |
183
|
|
|
* |
184
|
|
|
* @return mixed The flash value itself. |
185
|
|
|
* |
186
|
|
|
*/ |
187
|
3 |
|
public function getFlash($key, $alt = null) |
|
|
|
|
188
|
|
|
{ |
189
|
3 |
|
$this->resumeSession(); |
190
|
3 |
|
return isset($_SESSION[Session::FLASH_NOW][$this->name][$key]) |
191
|
3 |
|
? $_SESSION[Session::FLASH_NOW][$this->name][$key] |
192
|
3 |
|
: $alt; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* |
197
|
|
|
* Gets all the flash values in the *current* request. |
198
|
|
|
* |
199
|
|
|
* @param mixed $alt An alternative value to return if no flash values are set. |
200
|
|
|
* |
201
|
|
|
* @return mixed The flash values themselves. |
202
|
|
|
* |
203
|
|
|
*/ |
204
|
1 |
|
public function getAllCurrentFlash($alt = array()) |
|
|
|
|
205
|
|
|
{ |
206
|
1 |
|
$this->resumeSession(); |
207
|
1 |
|
return isset($_SESSION[Session::FLASH_NOW][$this->name]) |
208
|
1 |
|
? $_SESSION[Session::FLASH_NOW][$this->name] |
209
|
1 |
|
: $alt; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* |
214
|
|
|
* Clears flash values for *only* the next request. |
215
|
|
|
* |
216
|
|
|
* @return null |
217
|
|
|
* |
218
|
|
|
*/ |
219
|
2 |
|
public function clearFlash() |
|
|
|
|
220
|
|
|
{ |
221
|
2 |
|
if ($this->resumeSession()) { |
222
|
2 |
|
$_SESSION[Session::FLASH_NEXT][$this->name] = array(); |
223
|
2 |
|
} |
224
|
2 |
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* |
228
|
|
|
* Gets the flash value for a key in the *next* request. |
229
|
|
|
* |
230
|
|
|
* @param string $key The key for the flash value. |
231
|
|
|
* |
232
|
|
|
* @param mixed $alt An alternative value to return if the key is not set. |
233
|
|
|
* |
234
|
|
|
* @return mixed The flash value itself. |
235
|
|
|
* |
236
|
|
|
*/ |
237
|
2 |
|
public function getFlashNext($key, $alt = null) |
|
|
|
|
238
|
|
|
{ |
239
|
2 |
|
$this->resumeSession(); |
240
|
2 |
|
return isset($_SESSION[Session::FLASH_NEXT][$this->name][$key]) |
241
|
2 |
|
? $_SESSION[Session::FLASH_NEXT][$this->name][$key] |
242
|
2 |
|
: $alt; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* |
247
|
|
|
* Gets all flash values for the *next* request. |
248
|
|
|
* |
249
|
|
|
* @param mixed $alt An alternative value to return if no flash values set. |
250
|
|
|
* |
251
|
|
|
* @return mixed The flash values themselves. |
252
|
|
|
* |
253
|
|
|
*/ |
254
|
1 |
|
public function getAllFlashNext($alt = array()) |
|
|
|
|
255
|
|
|
{ |
256
|
1 |
|
$this->resumeSession(); |
257
|
1 |
|
return isset($_SESSION[Session::FLASH_NEXT][$this->name]) |
258
|
1 |
|
? $_SESSION[Session::FLASH_NEXT][$this->name] |
259
|
1 |
|
: $alt; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* |
264
|
|
|
* Sets a flash value for the *next* request *and* the current one. |
265
|
|
|
* |
266
|
|
|
* @param string $key The key for the flash value. |
267
|
|
|
* |
268
|
|
|
* @param mixed $val The flash value itself. |
269
|
|
|
* |
270
|
|
|
*/ |
271
|
2 |
|
public function setFlashNow($key, $val) |
|
|
|
|
272
|
|
|
{ |
273
|
2 |
|
$this->resumeOrStartSession(); |
274
|
2 |
|
$_SESSION[Session::FLASH_NOW][$this->name][$key] = $val; |
275
|
2 |
|
$_SESSION[Session::FLASH_NEXT][$this->name][$key] = $val; |
276
|
2 |
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* |
280
|
|
|
* Append a flash value with a numeric key for the *next* request *and* the current one. |
281
|
|
|
* |
282
|
|
|
* @param mixed $val The flash value itself. |
283
|
|
|
* |
284
|
|
|
*/ |
285
|
1 |
|
public function addFlashNow($val) |
|
|
|
|
286
|
|
|
{ |
287
|
1 |
|
$this->resumeOrStartSession(); |
288
|
1 |
|
$_SESSION[Session::FLASH_NOW][$this->name][] = $val; |
289
|
1 |
|
$_SESSION[Session::FLASH_NEXT][$this->name][] = $val; |
290
|
1 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* |
294
|
|
|
* Clears flash values for *both* the next request *and* the current one. |
295
|
|
|
* |
296
|
|
|
* @return null |
297
|
|
|
* |
298
|
|
|
*/ |
299
|
2 |
|
public function clearFlashNow() |
|
|
|
|
300
|
|
|
{ |
301
|
2 |
|
if ($this->resumeSession()) { |
302
|
2 |
|
$_SESSION[Session::FLASH_NOW][$this->name] = array(); |
303
|
2 |
|
$_SESSION[Session::FLASH_NEXT][$this->name] = array(); |
304
|
2 |
|
} |
305
|
2 |
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* |
309
|
|
|
* Retains all the current flash values for the next request; values that |
310
|
|
|
* already exist for the next request take precedence. |
311
|
|
|
* |
312
|
|
|
* @return null |
313
|
|
|
* |
314
|
|
|
*/ |
315
|
2 |
|
public function keepFlash() |
|
|
|
|
316
|
|
|
{ |
317
|
2 |
|
if ($this->resumeSession()) { |
318
|
2 |
|
$_SESSION[Session::FLASH_NEXT][$this->name] = array_merge( |
319
|
2 |
|
$_SESSION[Session::FLASH_NEXT][$this->name], |
320
|
2 |
|
$_SESSION[Session::FLASH_NOW][$this->name] |
321
|
2 |
|
); |
322
|
2 |
|
} |
323
|
2 |
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* |
327
|
|
|
* Loads the segment only if the session has already been started, or if |
328
|
|
|
* a session is available (in which case it resumes the session first). |
329
|
|
|
* |
330
|
|
|
* @return bool |
331
|
|
|
* |
332
|
|
|
*/ |
333
|
21 |
|
protected function resumeSession() |
334
|
|
|
{ |
335
|
21 |
|
if ($this->session->isStarted() || $this->session->resume()) { |
336
|
15 |
|
$this->load(); |
337
|
15 |
|
return true; |
338
|
|
|
} |
339
|
|
|
|
340
|
18 |
|
return false; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* |
345
|
|
|
* Sets the segment properties to $_SESSION references. |
346
|
|
|
* |
347
|
|
|
* @return null |
348
|
|
|
* |
349
|
|
|
*/ |
350
|
18 |
|
protected function load() |
|
|
|
|
351
|
|
|
{ |
352
|
18 |
|
if (! isset($_SESSION[$this->name])) { |
353
|
18 |
|
$_SESSION[$this->name] = array(); |
354
|
18 |
|
} |
355
|
|
|
|
356
|
18 |
|
if (! isset($_SESSION[Session::FLASH_NOW][$this->name])) { |
357
|
18 |
|
$_SESSION[Session::FLASH_NOW][$this->name] = array(); |
358
|
18 |
|
} |
359
|
|
|
|
360
|
18 |
|
if (! isset($_SESSION[Session::FLASH_NEXT][$this->name])) { |
361
|
18 |
|
$_SESSION[Session::FLASH_NEXT][$this->name] = array(); |
362
|
18 |
|
} |
363
|
18 |
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* |
367
|
|
|
* Resumes a previous session, or starts a new one, and loads the segment. |
368
|
|
|
* |
369
|
|
|
* @return null |
370
|
|
|
* |
371
|
|
|
*/ |
372
|
17 |
|
protected function resumeOrStartSession() |
373
|
|
|
{ |
374
|
17 |
|
if (! $this->resumeSession()) { |
375
|
15 |
|
$this->session->start(); |
376
|
15 |
|
$this->load(); |
377
|
15 |
|
} |
378
|
17 |
|
} |
379
|
|
|
} |
380
|
|
|
|
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: