-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
FlashComponent.php
141 lines (126 loc) · 4.51 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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Controller\Component;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Network\Exception\InternalErrorException;
use Cake\Utility\Inflector;
/**
* 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.
*/
class FlashComponent extends Component
{
/**
* The Session object instance
*
* @var \Cake\Network\Session
*/
protected $_session;
/**
* Default configuration
*
* @var array
*/
protected $_defaultConfig = [
'key' => 'flash',
'element' => 'default',
'params' => []
];
/**
* Constructor
*
* @param ComponentRegistry $registry A ComponentRegistry for this component
* @param array $config Array of config.
*/
public function __construct(ComponentRegistry $registry, array $config = [])
{
parent::__construct($registry, $config);
$this->_session = $registry->getController()->request->session();
}
/**
* Used to set a session variable that can be used to output messages in the view.
*
* 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
*
* @param string|\Exception $message Message to be flashed. If an instance
* of \Exception the exception message will be used and code will be set
* in params.
* @param array $options An array of options
* @return void
*/
public function set($message, array $options = [])
{
$options += $this->config();
if ($message instanceof \Exception) {
$options['params'] += ['code' => $message->getCode()];
$message = $message->getMessage();
}
list($plugin, $element) = pluginSplit($options['element']);
if ($plugin) {
$options['element'] = $plugin . '.Flash/' . $element;
} else {
$options['element'] = 'Flash/' . $element;
}
$this->_session->write('Flash.' . $options['key'], [
'message' => $message,
'key' => $options['key'],
'element' => $options['element'],
'params' => $options['params']
]);
}
/**
* Magic method for verbose flash methods based on element names.
*
* For example: $this->Flash->success('My message') would use the
* success.ctp element under `src/Template/Element/Flash` for rendering the
* flash message.
*
* 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.ctp element under `plugins/src/Template/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\Network\Exception\InternalErrorException If missing the flash message.
*/
public function __call($name, $args)
{
$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);
}
}