-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
FlashComponent.php
186 lines (170 loc) · 6.49 KB
/
FlashComponent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Controller\Component;
use Cake\Controller\Component;
use Cake\Http\Exception\InternalErrorException;
use Cake\Http\FlashMessage;
use Cake\Utility\Inflector;
use Throwable;
/**
* The CakePHP FlashComponent provides a way for you to write a flash variable
* to the session from your controllers, to be rendered in a view with the
* FlashHelper.
*
* @method void success(string $message, array $options = []) Set a message using "success" element
* @method void info(string $message, array $options = []) Set a message using "info" element
* @method void warning(string $message, array $options = []) Set a message using "warning" element
* @method void error(string $message, array $options = []) Set a message using "error" element
*/
class FlashComponent extends Component
{
/**
* Default configuration
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
'key' => 'flash',
'element' => 'default',
'params' => [],
'clear' => false,
'duplicate' => true,
];
/**
* Used to set a session variable that can be used to output messages in the view.
* If you make consecutive calls to this method, the messages will stack (if they are
* set with the same flash key)
*
* In your controller: $this->Flash->set('This has been saved');
*
* ### Options:
*
* - `key` The key to set under the session's Flash key
* - `element` The element used to render the flash message. Default to 'default'.
* - `params` An array of variables to make available when using an element
* - `clear` A bool stating if the current stack should be cleared to start a new one
* - `escape` Set to false to allow templates to print out HTML content
*
* @param \Throwable|string $message Message to be flashed. If an instance
* of \Throwable the throwable message will be used and code will be set
* in params.
* @param array<string, mixed> $options An array of options
* @return void
*/
public function set(Throwable|string $message, array $options = []): void
{
if ($message instanceof Throwable) {
$this->flash()->setExceptionMessage($message, $options);
} else {
$this->flash()->set($message, $options);
}
}
/**
* Get flash message utility instance.
*
* @return \Cake\Http\FlashMessage
*/
protected function flash(): FlashMessage
{
return $this->getController()->getRequest()->getFlash();
}
/**
* Proxy method to FlashMessage instance.
*
* @param array<string, mixed>|string $key The key to set, or a complete array of configs.
* @param mixed $value The value to set.
* @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true.
* @return $this
* @throws \Cake\Core\Exception\CakeException When trying to set a key that is invalid.
*/
public function setConfig(array|string $key, mixed $value = null, bool $merge = true)
{
$this->flash()->setConfig($key, $value, $merge);
return $this;
}
/**
* Proxy method to FlashMessage instance.
*
* @param string|null $key The key to get or null for the whole config.
* @param mixed $default The return value when the key does not exist.
* @return mixed Configuration data at the named key or null if the key does not exist.
*/
public function getConfig(?string $key = null, mixed $default = null): mixed
{
return $this->flash()->getConfig($key, $default);
}
/**
* Proxy method to FlashMessage instance.
*
* @param string $key The key to get.
* @return mixed Configuration data at the named key
* @throws \InvalidArgumentException
*/
public function getConfigOrFail(string $key): mixed
{
return $this->flash()->getConfigOrFail($key);
}
/**
* Proxy method to FlashMessage instance.
*
* @param array<string, mixed>|string $key The key to set, or a complete array of configs.
* @param mixed $value The value to set.
* @return $this
*/
public function configShallow(array|string $key, mixed $value = null)
{
$this->flash()->configShallow($key, $value);
return $this;
}
/**
* Magic method for verbose flash methods based on element names.
*
* For example: $this->Flash->success('My message') would use the
* `success.php` element under `templates/element/flash/` for rendering the
* flash message.
*
* If you make consecutive calls to this method, the messages will stack (if they are
* set with the same flash key)
*
* Note that the parameter `element` will be always overridden. In order to call a
* specific element from a plugin, you should set the `plugin` option in $args.
*
* For example: `$this->Flash->warning('My message', ['plugin' => 'PluginName'])` would
* use the `warning.php` element under `plugins/PluginName/templates/element/flash/` for
* rendering the flash message.
*
* @param string $name Element name to use.
* @param array $args Parameters to pass when calling `FlashComponent::set()`.
* @return void
* @throws \Cake\Http\Exception\InternalErrorException If missing the flash message.
*/
public function __call(string $name, array $args): void
{
$element = Inflector::underscore($name);
if (count($args) < 1) {
throw new InternalErrorException('Flash message missing.');
}
$options = ['element' => $element];
if (!empty($args[1])) {
if (!empty($args[1]['plugin'])) {
$options = ['element' => $args[1]['plugin'] . '.' . $element];
unset($args[1]['plugin']);
}
$options += (array)$args[1];
}
$this->set($args[0], $options);
}
}