1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Faxity\Flash; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* DI module for creating and rendering flash messages |
10
|
|
|
* All messages are deferred to the next request by default |
11
|
|
|
*/ |
12
|
|
|
class Flash implements ContainerInjectableInterface |
13
|
|
|
{ |
14
|
|
|
use ContainerInjectableTrait; |
15
|
|
|
|
16
|
|
|
/** @var array $messages Messages buffer for this request */ |
17
|
|
|
private $messages = []; |
18
|
|
|
/** @var string $template Anax view template */ |
19
|
|
|
private $template; |
20
|
|
|
/** @var string $region Anax page region */ |
21
|
|
|
private $region; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Adds flash message to buffer of next request |
26
|
|
|
* @param string $type Message type (ok, error or warning) |
27
|
|
|
* @param string $text Message text, not required |
28
|
|
|
* @param bool $immediate Adds message to this request buffer |
29
|
|
|
* |
30
|
|
|
* @return $this |
31
|
|
|
*/ |
32
|
1 |
|
private function message(string $type, string $text, bool $immediate = false): Flash |
33
|
|
|
{ |
34
|
|
|
$message = (object)[ |
35
|
1 |
|
"type" => $type, |
36
|
1 |
|
"text" => $text, |
37
|
|
|
]; |
38
|
|
|
|
39
|
1 |
|
if ($immediate) { |
40
|
1 |
|
$this->messages[] = $message; |
41
|
|
|
} else { |
42
|
1 |
|
$messages = $this->di->session->get("flash", []); |
|
|
|
|
43
|
1 |
|
$messages[] = $message; |
44
|
|
|
|
45
|
1 |
|
$this->di->session->set("flash", $messages); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $template Template to render messages |
54
|
|
|
* @param string|null $region The region to render to, defaults to "flash" |
55
|
|
|
* |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
2 |
|
public function __construct(string $template, ?string $region = null) |
59
|
|
|
{ |
60
|
2 |
|
$this->template = $template; |
61
|
2 |
|
$this->region = $region ?? "flash"; |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Gets all messages within the current request buffer |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
1 |
|
public function getMessages(): array |
71
|
|
|
{ |
72
|
1 |
|
return $this->messages; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Renders flash messages in session to Anax views |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
1 |
|
public function render(): Flash |
82
|
|
|
{ |
83
|
1 |
|
$this->messages = $this->di->session->getOnce("flash", []); |
|
|
|
|
84
|
1 |
|
$data = [ "messages" => &$this->messages ]; |
85
|
|
|
|
86
|
1 |
|
$this->di->view->add($this->template, $data, $this->region); |
|
|
|
|
87
|
1 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Adds an ok message to render |
93
|
|
|
* @param string $text Message text |
94
|
|
|
* @param bool $immediate Adds message to this request buffer |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
1 |
|
public function ok(string $text, bool $immediate = false): Flash |
99
|
|
|
{ |
100
|
1 |
|
return $this->message("ok", $text, $immediate); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Adds a warning message to render |
106
|
|
|
* @param string $text Message text |
107
|
|
|
* @param bool $immediate Adds message to this request buffer |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
1 |
|
public function warn(string $text, bool $immediate = false): Flash |
112
|
|
|
{ |
113
|
1 |
|
return $this->message("warn", $text, $immediate); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Adds an error message to render |
119
|
|
|
* @param string $text Message text |
120
|
|
|
* @param bool $immediate Adds message to this request buffer |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
1 |
|
public function err(string $text, bool $immediate = false): Flash |
125
|
|
|
{ |
126
|
1 |
|
return $this->message("err", $text, $immediate); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|