|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Alxarafe. Development of PHP applications in a flash! |
|
4
|
|
|
* Copyright (C) 2018-2020 Alxarafe <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Alxarafe\Core\Singletons; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class FlashMessages |
|
11
|
|
|
* |
|
12
|
|
|
* Gestiona los mensajes de aviso y error de la aplicación. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
15
|
|
|
* |
|
16
|
|
|
* @package Alxarafe\Core\Singletons |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class FlashMessages |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Contiene la lista actual de mensajes |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private static array $messagesListNow; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Contiene la lista de mensajes para la próxima sesión |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private static array $messagesListNext; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Inicializa las variables |
|
36
|
|
|
* |
|
37
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $index |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function load(string $index = 'main') |
|
42
|
|
|
{ |
|
43
|
|
|
Session::load($index); |
|
44
|
|
|
self::$messagesListNow = []; |
|
45
|
|
|
self::$messagesListNext = []; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Retorna el contenido completo de los mensajes de sesión. |
|
50
|
|
|
* |
|
51
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function getContainer(): array |
|
56
|
|
|
{ |
|
57
|
|
|
return Session::getFlash('messages') ?? []; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Registra un nuevo mensaje de error |
|
62
|
|
|
* |
|
63
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $msg |
|
66
|
|
|
* @param string $when |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function setError(string $msg, string $when = 'now'): void |
|
69
|
|
|
{ |
|
70
|
|
|
$message = ['type' => 'danger', 'msg' => $msg]; |
|
71
|
|
|
// Logger::getInstance()->getLogger()->addError($msg); |
|
72
|
|
|
self::setFlash($when, $message); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Registra un nuevo mensaje flash |
|
77
|
|
|
* |
|
78
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $when |
|
81
|
|
|
* @param array $message |
|
82
|
|
|
*/ |
|
83
|
|
|
private static function setFlash(string $when = 'now', array $message = []): void |
|
84
|
|
|
{ |
|
85
|
|
|
switch ($when) { |
|
86
|
|
|
case 'now': |
|
87
|
|
|
self::$messagesListNow[] = $message; |
|
88
|
|
|
Session::setFlashNow('messages', self::$messagesListNow); |
|
89
|
|
|
break; |
|
90
|
|
|
case 'next': |
|
91
|
|
|
default: |
|
92
|
|
|
self::$messagesListNext[] = $message; |
|
93
|
|
|
Session::setFlashNext('messages', self::$messagesListNext); |
|
94
|
|
|
break; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Registra un nuevo mensaje de aviso |
|
100
|
|
|
* |
|
101
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $msg |
|
104
|
|
|
* @param string $when |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function setWarning(string $msg, string $when = 'now'): void |
|
107
|
|
|
{ |
|
108
|
|
|
$message = ['type' => 'warning', 'msg' => $msg]; |
|
109
|
|
|
// Logger::getInstance()->getLogger()->addWarning($msg); |
|
110
|
|
|
self::setFlash($when, $message); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Registra un nuevo mensaje informativo |
|
115
|
|
|
* |
|
116
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $msg |
|
119
|
|
|
* @param string $when |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function setInfo(string $msg, string $when = 'now'): void |
|
122
|
|
|
{ |
|
123
|
|
|
$message = ['type' => 'info', 'msg' => $msg]; |
|
124
|
|
|
// Logger::getInstance()->getLogger()->addInfo($msg); |
|
125
|
|
|
self::setFlash($when, $message); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Registra un nuevo mensaje de error |
|
130
|
|
|
* |
|
131
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $msg |
|
134
|
|
|
* @param string $when |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function setSuccess(string $msg, string $when = 'now'): void |
|
137
|
|
|
{ |
|
138
|
|
|
$message = ['type' => 'success', 'msg' => $msg]; |
|
139
|
|
|
// Logger::getInstance()->getLogger()->addNotice($msg); |
|
140
|
|
|
self::setFlash($when, $message); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|