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
|
<?php
namespace Trails;
use Trails\Exceptions\SessionRequiredException;
/**
* The flash provides a way to pass temporary objects between actions.
* Anything you place in the flash will be exposed to the very next action and
* then cleared out. This is a great way of doing notices and alerts, such as
* a create action that sets
* <tt>$flash->set('notice', "Successfully created")</tt>
* before redirecting to a display action that can then expose the flash to its
* template.
*
* @package trails
*
* @author mlunzena
* @copyright (c) Authors
* @version $Id: trails.php 7001 2008-04-04 11:20:27Z mlunzena $
*/
final class Flash implements \ArrayAccess
{
private array $flash = [];
private array $used = [];
/**
* @return self
* @throws SessionRequiredException
*/
public static function instance()
{
if (!isset($_SESSION)) {
throw new Exceptions\SessionRequiredException();
}
if (!isset($_SESSION[self::class])) {
$_SESSION[self::class] = new self();
}
return $_SESSION[self::class];
}
public function offsetExists($offset): bool
{
return isset($this->flash[$offset]);
}
public function offsetGet($offset): mixed
{
return $this->get($offset);
}
public function offsetSet($offset, $value): void
{
$this->set($offset, $value);
}
public function offsetUnset($offset): void
{
unset($this->flash[$offset], $this->used[$offset]);
}
/**
* Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
* use() # marks the entire flash as used
* use('msg') # marks the "msg" entry as used
* use(null, false) # marks the entire flash as unused
* # (keeps it around for one more action)
* use('msg', false) # marks the "msg" entry as unused
* # (keeps it around for one more action)
*
*/
private function use(?string $key = null, bool $isUsed = true): void
{
if ($key) {
$this->used[$key] = $isUsed;
} else {
foreach (array_keys($this->used) as $key) {
$this->use($key, $isUsed);
}
}
}
/**
* Marks the entire flash or a single flash entry to be discarded by the end
* of the current action.
*
* $flash->discard() # discards entire flash
* # (it'll still be available for the
* # current action)
* $flash->discard('warning') # discard the "warning" entry
* # (it'll still be available for the
* # current action)
*/
public function discard(?string $key = null): void
{
$this->use($key);
}
/**
* Returns the value to the specified key.
*
* @return mixed the key's value.
*/
public function &get(string $key): mixed
{
$return = null;
if (isset($this->flash[$key])) {
$return =& $this->flash[$key];
}
return $return;
}
/**
* Keeps either the entire current flash or a specific flash entry available
* for the next action:
*
* $flash->keep() # keeps the entire flash
* $flash->keep('notice') # keeps only the "notice" entry, the rest of
* # the flash is discarded
*/
public function keep(?string $key = null): void
{
$this->use($key, false);
}
/**
* Sets a key's value.
*/
public function set(string $key, mixed $value): void
{
$this->keep($key);
$this->flash[$key] = $value;
}
/**
* Sets a key's value by reference.
*/
public function set_ref(string $key, mixed &$value): void
{
$this->keep($key);
$this->flash[$key] =& $valze;
}
/**
* Removes all used values
*/
public function sweep(): void
{
foreach (array_keys($this->flash) as $k) {
if ($this->used[$k]) {
unset($this->flash[$k], $this->used[$k]);
} else {
$this->use($k);
}
}
}
public function __toString()
{
$values = [];
foreach ($this->flash as $key => $value) {
$values[] = sprintf(
"'%s': [%s, '%s']",
$key,
var_export($value, true),
!empty($this->used[$key]) ? 'used' : 'unused'
);
}
return '{' . implode(', ', $values) . '}'. "\n";
}
public function __sleep(): array
{
$this->sweep();
return ['flash', 'used'];
}
public function __wakeUp()
{
$this->discard();
}
}
|