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
|
|
|
$this->setNotifyFromSession(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set all notification that are present in session |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function setNotifyFromSession(): void |
67
|
|
|
{ |
68
|
|
|
$this->setNotifyErrorFromSession(); |
69
|
|
|
$this->setNotifyWarningFromSession(); |
70
|
|
|
$this->setNotifySuccessFromSession(); |
71
|
|
|
$this->setNotifyInfoFromSession(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
*/ |
76
|
|
|
protected function setNotifyErrorFromSession(): void |
77
|
|
|
{ |
78
|
|
|
$this->setNotifyFromSessionByLabel('errors'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
*/ |
83
|
|
|
protected function setNotifyWarningFromSession(): void |
84
|
|
|
{ |
85
|
|
|
$this->setNotifyFromSessionByLabel('warnings'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
*/ |
90
|
|
|
protected function setNotifyInfoFromSession(): void |
91
|
|
|
{ |
92
|
|
|
$this->setNotifyFromSessionByLabel('info'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
*/ |
97
|
|
|
protected function setNotifySuccessFromSession(): void |
98
|
|
|
{ |
99
|
|
|
$this->setNotifyFromSessionByLabel('success'); |
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* Set all notification of a label type that are present in session |
103
|
|
|
* |
104
|
|
|
* @param string $label |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
protected function setNotifyFromSessionByLabel(string $label): void |
109
|
|
|
{ |
110
|
|
|
|
111
|
|
|
if (!$this->session->has($label)) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($label == 'errors') { |
116
|
|
|
$errors = $this->session->get('errors')->all(); |
117
|
|
|
foreach ($errors as $error) { |
118
|
|
|
$this->error($error,false); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
$messages = session()->get($label); |
126
|
|
|
if ($label=='warnings'){ |
127
|
|
|
$label='warning'; |
128
|
|
|
} |
129
|
|
|
if (!is_array($messages)) { |
130
|
|
|
$this->$label($messages,false); |
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
foreach ($messages as $msg) { |
135
|
|
|
$this->$label($msg,false); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Render all the notifications' script to insert into script tag |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
* |
144
|
|
|
*/ |
145
|
|
|
public function render(): string |
146
|
|
|
{ |
147
|
|
|
$output = []; |
148
|
|
|
foreach ($this->notifications as $notification) { |
149
|
|
|
|
150
|
|
|
$output[] = $notification; |
151
|
|
|
} |
152
|
|
|
foreach ($this->flashedNotifications as $flNotification) { |
153
|
|
|
$output[] = $flNotification; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return "window.notifications = " . json_encode($output) . ";"; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Add a notification |
161
|
|
|
* |
162
|
|
|
* @param string $type Could be error, info, success, or warning. |
163
|
|
|
* @param string $text The notification's message |
164
|
|
|
* @param string $title The notification's title |
|
|
|
|
165
|
|
|
* @param array $options |
|
|
|
|
166
|
|
|
* @param bool $onlyNextRequest if true(default), se the notification in session only for the next request |
|
|
|
|
167
|
|
|
* |
168
|
|
|
*/ |
169
|
|
|
public function add($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null) |
170
|
|
|
{ |
171
|
|
|
if ($type == '') { |
172
|
|
|
$type = 'info'; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if ($timeout === null) { |
176
|
|
|
$timeout = false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$notification = [ |
180
|
|
|
'theme' => $theme, |
181
|
|
|
'timeout' => $timeout, |
182
|
|
|
'type' => $type, |
183
|
|
|
'layout' => $layout, |
184
|
|
|
'text' => $text, |
185
|
|
|
]; |
186
|
|
View Code Duplication |
if ($sounds !== null) { |
|
|
|
|
187
|
|
|
$notification['sources'] = [ |
188
|
|
|
'sounds' => [$sounds], |
189
|
|
|
'soundsVolume' => $soundsVolume !== null ? $soundsVolume : 0.5, |
190
|
|
|
]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$this->notifications[] = $notification; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function addForNextRequest($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null) |
197
|
|
|
{ |
198
|
|
|
if ($type == '') { |
199
|
|
|
$type = 'info'; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if ($timeout === null) { |
203
|
|
|
$timeout = false; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$notification = [ |
207
|
|
|
'theme' => $theme, |
208
|
|
|
'timeout' => $timeout, |
209
|
|
|
'type' => $type, |
210
|
|
|
'layout' => $layout, |
211
|
|
|
'text' => $text, |
212
|
|
|
]; |
213
|
|
View Code Duplication |
if ($sounds !== null) { |
|
|
|
|
214
|
|
|
$notification['sources'] = [ |
215
|
|
|
'sounds' => [$sounds], |
216
|
|
|
'soundsVolume' => $soundsVolume !== null ? $soundsVolume : 0.5, |
217
|
|
|
]; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if ($this->session->has('laravel::flash-notifications')) { |
221
|
|
|
$flashNotifications = $this->session->get('laravel::flash-notifications'); |
222
|
|
|
} else { |
223
|
|
|
$flashNotifications = []; |
224
|
|
|
} |
225
|
|
|
$this->session->forget('laravel::flash-notifications'); |
226
|
|
|
$flashNotifications[] = $notification; |
227
|
|
|
$this->session->flash('laravel::flash-notifications', $flashNotifications); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Shortcut for adding an info notification |
232
|
|
|
* |
233
|
|
|
* @param string $message The notification's message |
|
|
|
|
234
|
|
|
* @param string $title The notification's title |
|
|
|
|
235
|
|
|
* @param array $options |
236
|
|
|
*/ |
237
|
|
View Code Duplication |
public function info($text, $onlyNextRequest = false, array $options = []) |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui'; |
240
|
|
|
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : false; |
241
|
|
|
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight'; |
242
|
|
|
if ($onlyNextRequest){ |
243
|
|
|
$this->addForNextRequest($theme, $timeout, 'info', $layout, $text, null, null); |
244
|
|
|
return; |
245
|
|
|
} |
246
|
|
|
$this->add($theme, $timeout, 'info', $layout, $text, null, null); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Shortcut for adding an error notification |
251
|
|
|
* |
252
|
|
|
* @param string $message The notification's message |
|
|
|
|
253
|
|
|
* @param string $title The notification's title |
|
|
|
|
254
|
|
|
* @param array $options |
255
|
|
|
*/ |
256
|
|
View Code Duplication |
public function error($text, $onlyNextRequest = false, array $options = []) |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui'; |
259
|
|
|
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0; |
260
|
|
|
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight'; |
261
|
|
|
if ($onlyNextRequest){ |
262
|
|
|
$this->addForNextRequest($theme, $timeout, 'error', $layout, $text, null, null); |
263
|
|
|
return; |
264
|
|
|
} |
265
|
|
|
$this->add($theme, $timeout, 'error', $layout, $text, null, null, $onlyNextRequest); |
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Shortcut for adding a warning notification |
270
|
|
|
* |
271
|
|
|
* @param string $message The notification's message |
|
|
|
|
272
|
|
|
* @param string $title The notification's title |
|
|
|
|
273
|
|
|
* @param array $options |
274
|
|
|
*/ |
275
|
|
View Code Duplication |
public function warning($text, $onlyNextRequest = false, array $options = []) |
|
|
|
|
276
|
|
|
{ |
277
|
|
|
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui'; |
278
|
|
|
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0; |
279
|
|
|
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight'; |
280
|
|
|
if ($onlyNextRequest){ |
281
|
|
|
$this->addForNextRequest($theme, $timeout, 'warning', $layout, $text, null, null); |
282
|
|
|
return; |
283
|
|
|
} |
284
|
|
|
$this->add($theme, $timeout, 'warning', $layout, $text, null, null, $onlyNextRequest); |
|
|
|
|
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Shortcut for adding a success notification |
289
|
|
|
* |
290
|
|
|
* @param string $message The notification's message |
|
|
|
|
291
|
|
|
* @param string $title The notification's title |
|
|
|
|
292
|
|
|
* @param array $options |
293
|
|
|
*/ |
294
|
|
View Code Duplication |
public function success($text, $onlyNextRequest = false, array $options = []) |
|
|
|
|
295
|
|
|
{ |
296
|
|
|
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui'; |
297
|
|
|
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0; |
298
|
|
|
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight'; |
299
|
|
|
if ($onlyNextRequest){ |
300
|
|
|
$this->addForNextRequest($theme, $timeout, 'success', $layout, $text, null, null); |
301
|
|
|
return; |
302
|
|
|
} |
303
|
|
|
$this->add($theme, $timeout, 'success', $layout, $text, null, null, $onlyNextRequest); |
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Clear all notifications |
309
|
|
|
* |
310
|
|
|
* @param bool $withSession if true (default) clean notifications in session too. |
|
|
|
|
311
|
|
|
*/ |
312
|
|
|
public function clear() |
313
|
|
|
{ |
314
|
|
|
$this->notifications = []; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
public function clearFlashed(bool $withSession = true) |
318
|
|
|
{ |
319
|
|
|
$this->flashedNotifications = []; |
320
|
|
|
|
321
|
|
|
if ($withSession) { |
322
|
|
|
$this->session->forget('laravel::flash-notifications'); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function clearAll(bool $withSession = true) |
327
|
|
|
{ |
328
|
|
|
$this->notifications = []; |
329
|
|
|
$this->flashedNotifications = []; |
330
|
|
|
|
331
|
|
|
if ($withSession) { |
332
|
|
|
$this->session->forget('laravel::flash-notifications'); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
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.