|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Faxity\DI; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* DI module for creating and rendering flash messages |
|
10
|
|
|
*/ |
|
11
|
|
|
class Flash implements ContainerInjectableInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use ContainerInjectableTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array $messages Messages buffer |
|
17
|
|
|
* @var string $template Anax view template |
|
18
|
|
|
* @var string $region Anax page region |
|
19
|
|
|
*/ |
|
20
|
|
|
private $messages = []; |
|
21
|
|
|
private $template; |
|
22
|
|
|
private $region; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Adds message to buffer |
|
27
|
|
|
* @param string $type Message type (ok, error or warning) |
|
28
|
|
|
* @param string $text Message text, not required |
|
29
|
|
|
*/ |
|
30
|
2 |
|
private function message(string $type, string $text) |
|
31
|
|
|
{ |
|
32
|
2 |
|
$this->messages[] = (object) [ |
|
33
|
2 |
|
"type" => $type, |
|
34
|
2 |
|
"text" => $text, |
|
35
|
|
|
]; |
|
36
|
2 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $template Template to render messages |
|
41
|
|
|
* @param string|null $region The region to render to, defaults to "flash" |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function __construct(string $template, ?string $region = null) |
|
44
|
|
|
{ |
|
45
|
3 |
|
$this->template = $template; |
|
46
|
3 |
|
$this->region = $region ?? "flash"; |
|
47
|
3 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Gets all messages within the buffer |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function getMessages() : array |
|
56
|
|
|
{ |
|
57
|
1 |
|
return $this->messages; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Renders flash messages into an Anax view |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function render() |
|
65
|
|
|
{ |
|
66
|
|
|
// Pass messages by reference, as all messages are created after this function call |
|
67
|
2 |
|
$data = [ "messages" => &$this->messages ]; |
|
68
|
2 |
|
$this->di->view->add($this->template, $data, $this->region); |
|
|
|
|
|
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Adds an ok message to render |
|
74
|
|
|
* @param string $text Message text, not required |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function ok(string $text) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$this->message("ok", $text); |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Adds a warning message to render |
|
84
|
|
|
* @param string $text Message text, not required |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function warn(string $text) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->message("warn", $text); |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Adds an error message to render |
|
94
|
|
|
* @param string $text Message text, not required |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function err(string $text) : void |
|
97
|
|
|
{ |
|
98
|
2 |
|
$this->message("err", $text); |
|
99
|
2 |
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|