1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padosoft\Laravel\Notification\Notifier; |
4
|
|
|
|
5
|
|
|
use Illuminate\Session\SessionManager; |
6
|
|
|
|
7
|
|
|
class Notifier |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Added notifications |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $notifications = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Flashable added notifications |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $flashedNotifications = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Added notifications |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $options = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Illuminate Session |
33
|
|
|
* |
34
|
|
|
* @var \Illuminate\Session\SessionManager |
35
|
|
|
*/ |
36
|
|
|
protected $session; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
* |
41
|
|
|
* @param \Illuminate\Session\SessionManager $session |
42
|
|
|
* |
43
|
|
|
* @internal param \Illuminate\Session\SessionManager $session |
44
|
|
|
*/ |
45
|
|
|
public function __construct(SessionManager $session) |
46
|
|
|
{ |
47
|
|
|
$this->session = $session; |
48
|
|
|
$flashedNotifications = null; |
49
|
|
|
|
50
|
|
|
if ($this->session->has('laravel::flash-notifications')) { |
51
|
|
|
$flashedNotifications = $this->session->get('laravel::flash-notifications'); |
52
|
|
|
} |
53
|
|
|
if ($flashedNotifications !== null) { |
54
|
|
|
$this->flashedNotifications = $flashedNotifications; |
55
|
|
|
} else { |
56
|
|
|
$this->flashedNotifications = []; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Render all the notifications' script to insert into script tag |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
|
|
public function render(): string |
67
|
|
|
{ |
68
|
|
|
$output = []; |
69
|
|
|
foreach ($this->notifications as $notification) { |
70
|
|
|
|
71
|
|
|
$output[] = $notification; |
72
|
|
|
} |
73
|
|
|
foreach ($this->flashedNotifications as $flNotification) { |
74
|
|
|
$output[] = $flNotification; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return "window.notifications = " . json_encode($output) . ";"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add a notification |
82
|
|
|
* |
83
|
|
|
* @param string $type Could be error, info, success, or warning. |
84
|
|
|
* @param string $text The notification's message |
85
|
|
|
* @param string $title The notification's title |
|
|
|
|
86
|
|
|
* @param array $options |
|
|
|
|
87
|
|
|
* @param bool $onlyNextRequest if true(default), se the notification in session only for the next request |
|
|
|
|
88
|
|
|
* |
89
|
|
|
*/ |
90
|
|
|
public function add($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null) |
91
|
|
|
{ |
92
|
|
|
if ($type == '') { |
93
|
|
|
$type = 'info'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($timeout === null) { |
97
|
|
|
$timeout = false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$notification = [ |
101
|
|
|
'theme' => $theme, |
102
|
|
|
'timeout' => $timeout, |
103
|
|
|
'type' => $type, |
104
|
|
|
'layout' => $layout, |
105
|
|
|
'text' => $text, |
106
|
|
|
]; |
107
|
|
View Code Duplication |
if ($sounds !== null) { |
|
|
|
|
108
|
|
|
$notification['sources'] = [ |
109
|
|
|
'sounds' => [$sounds], |
110
|
|
|
'soundsVolume' => $soundsVolume !== null ? $soundsVolume : 0.5, |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->notifications[] = $notification; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function addForNextRequest($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null) |
118
|
|
|
{ |
119
|
|
|
if ($type == '') { |
120
|
|
|
$type = 'info'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($timeout === null) { |
124
|
|
|
$timeout = false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$notification = [ |
128
|
|
|
'theme' => $theme, |
129
|
|
|
'timeout' => $timeout, |
130
|
|
|
'type' => $type, |
131
|
|
|
'layout' => $layout, |
132
|
|
|
'text' => $text, |
133
|
|
|
]; |
134
|
|
View Code Duplication |
if ($sounds !== null) { |
|
|
|
|
135
|
|
|
$notification['sources'] = [ |
136
|
|
|
'sounds' => [$sounds], |
137
|
|
|
'soundsVolume' => $soundsVolume !== null ? $soundsVolume : 0.5, |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($this->session->has('laravel::flash-notifications')) { |
142
|
|
|
$flashNotifications = $this->session->get('laravel::flash-notifications'); |
143
|
|
|
} else { |
144
|
|
|
$flashNotifications = []; |
145
|
|
|
} |
146
|
|
|
$this->session->forget('laravel::flash-notifications'); |
147
|
|
|
$flashNotifications[] = $notification; |
148
|
|
|
$this->session->flash('laravel::flash-notifications', $flashNotifications); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Shortcut for adding an info notification |
153
|
|
|
* |
154
|
|
|
* @param string $message The notification's message |
|
|
|
|
155
|
|
|
* @param string $title The notification's title |
|
|
|
|
156
|
|
|
* @param array $options |
157
|
|
|
*/ |
158
|
|
View Code Duplication |
public function info($text, $onlyNextRequest = true, array $options = []) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui'; |
161
|
|
|
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : false; |
162
|
|
|
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight'; |
163
|
|
|
|
164
|
|
|
$this->add($theme, $timeout, 'info', $layout, $text, null, null, $onlyNextRequest); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Shortcut for adding an error notification |
169
|
|
|
* |
170
|
|
|
* @param string $message The notification's message |
|
|
|
|
171
|
|
|
* @param string $title The notification's title |
|
|
|
|
172
|
|
|
* @param array $options |
173
|
|
|
*/ |
174
|
|
View Code Duplication |
public function error($text, $onlyNextRequest = true, array $options = []) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui'; |
177
|
|
|
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0; |
178
|
|
|
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight'; |
179
|
|
|
|
180
|
|
|
$this->add($theme, $timeout, 'error', $layout, $text, null, null, $onlyNextRequest); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Shortcut for adding a warning notification |
185
|
|
|
* |
186
|
|
|
* @param string $message The notification's message |
|
|
|
|
187
|
|
|
* @param string $title The notification's title |
|
|
|
|
188
|
|
|
* @param array $options |
189
|
|
|
*/ |
190
|
|
View Code Duplication |
public function warning($text, $onlyNextRequest = true, array $options = []) |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui'; |
193
|
|
|
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0; |
194
|
|
|
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight'; |
195
|
|
|
|
196
|
|
|
$this->add($theme, $timeout, 'warning', $layout, $text, null, null, $onlyNextRequest); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Shortcut for adding a success notification |
201
|
|
|
* |
202
|
|
|
* @param string $message The notification's message |
|
|
|
|
203
|
|
|
* @param string $title The notification's title |
|
|
|
|
204
|
|
|
* @param array $options |
205
|
|
|
*/ |
206
|
|
View Code Duplication |
public function success($text, $onlyNextRequest = true, array $options = []) |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui'; |
209
|
|
|
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0; |
210
|
|
|
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight'; |
211
|
|
|
$this->add($theme, $timeout, 'success', $layout, $text, null, null, $onlyNextRequest); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Clear all notifications |
217
|
|
|
* |
218
|
|
|
* @param bool $withSession if true (default) clean notifications in session too. |
|
|
|
|
219
|
|
|
*/ |
220
|
|
|
public function clear() |
221
|
|
|
{ |
222
|
|
|
$this->notifications = []; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function clearFlashed(bool $withSession = true) |
226
|
|
|
{ |
227
|
|
|
$this->flashedNotifications = []; |
228
|
|
|
|
229
|
|
|
if ($withSession) { |
230
|
|
|
$this->session->forget('laravel::flash-notifications'); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function clearAll(bool $withSession = true) |
235
|
|
|
{ |
236
|
|
|
$this->notifications = []; |
237
|
|
|
$this->flashedNotifications = []; |
238
|
|
|
|
239
|
|
|
if ($withSession) { |
240
|
|
|
$this->session->forget('laravel::flash-notifications'); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.