/
WidgetRegistry.php
200 lines (188 loc) · 5.76 KB
/
WidgetRegistry.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?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\View\Widget;
use Cake\Core\App;
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\View\StringTemplate;
use Cake\View\View;
use Cake\View\Widget\WidgetInterface;
use \ReflectionClass;
/**
* A registry/factory for input widgets.
*
* Can be used by helpers/view logic to build form widgets
* and other HTML widgets.
*
* This class handles the mapping between names and concrete classes.
* It also has a basic name based dependency resolver that allows
* widgets to depend on each other.
*
* Each widget should expect a StringTemplate instance as their first
* argument. All other dependencies will be included after.
*
* Widgets can ask for the current view by using the `_view` widget.
*/
class WidgetRegistry {
/**
* Array of widgets + widget configuration.
*
* @var array
*/
protected $_widgets = [
'button' => ['Cake\View\Widget\Button'],
'checkbox' => ['Cake\View\Widget\Checkbox'],
'file' => ['Cake\View\Widget\File'],
'label' => ['Cake\View\Widget\Label'],
'nestingLabel' => ['Cake\View\Widget\NestingLabel'],
'multicheckbox' => ['Cake\View\Widget\MultiCheckbox', 'nestingLabel'],
'radio' => ['Cake\View\Widget\Radio', 'nestingLabel'],
'select' => ['Cake\View\Widget\SelectBox'],
'textarea' => ['Cake\View\Widget\Textarea'],
'datetime' => ['Cake\View\Widget\DateTime', 'select'],
'_default' => ['Cake\View\Widget\Basic'],
];
/**
* Templates to use.
*
* @var \Cake\View\StringTemplate
*/
protected $_templates;
/**
* Constructor
*
* @param \Cake\View\StringTemplate $templates Templates instance to use.
* @param \Cake\View\View $view The view instance to set as a widget.
* @param string|array $widgets See add() method for more information.
*/
public function __construct(StringTemplate $templates, View $view, $widgets = []) {
$this->_templates = $templates;
if (!empty($widgets)) {
if (is_string($widgets)) {
$this->load($widgets);
} else {
$this->add($widgets);
}
}
$this->add(['_view' => $view]);
}
/**
* Load a config file containing widgets.
*
* Widget files should define a `$config` variable containing
* all the widgets to load. Loaded widgets will be merged with existing
* widgets.
*
* @param string $file The file to load
* @return void
*/
public function load($file) {
$loader = new PhpConfig();
$widgets = $loader->read($file);
$this->add($widgets);
}
/**
* Adds or replaces existing widget instances/configuration with new ones.
*
* Widget arrays can either be descriptions or instances. For example:
*
* {{{
* $registry->add([
* 'label' => new MyLabel($templates),
* 'checkbox' => ['Fancy.MyCheckbox', 'label']
* ]);
* }}}
*
* The above shows how to define widgets as instances or as
* descriptions including dependencies. Classes can be defined
* with plugin notation, or fully namespaced class names.
*
* @param array $widgets Array of widgets to use.
* @return void
*/
public function add(array $widgets) {
$this->_widgets = $widgets + $this->_widgets;
}
/**
* Get a widget.
*
* Will either fetch an already created widget, or create a new instance
* if the widget has been defined. If the widget is undefined an instance of
* the `_default` widget will be returned. An exception will be thrown if
* the `_default` widget is undefined.
*
* @param string $name The widget name to get.
* @return WidgetInterface widget interface class.
* @throws \RuntimeException when widget is undefined.
*/
public function get($name) {
if (!isset($this->_widgets[$name]) && empty($this->_widgets['_default'])) {
throw new \RuntimeException(sprintf('Unknown widget "%s"', $name));
}
if (!isset($this->_widgets[$name])) {
$name = '_default';
}
$this->_widgets[$name] = $this->_resolveWidget($this->_widgets[$name]);
return $this->_widgets[$name];
}
/**
* Clear the registry and reset the widgets.
*
* @return void
*/
public function clear() {
$this->_widgets = [];
}
/**
* Resolves a widget spec into an instance.
*
* @param mixed $widget The widget to get
* @return WidgetInterface
* @throws \RuntimeException when class cannot be loaded or does not
* implement WidgetInterface.
*/
protected function _resolveWidget($widget) {
$type = gettype($widget);
if ($type === 'object' && $widget instanceof WidgetInterface) {
return $widget;
}
if ($type === 'object') {
throw new \RuntimeException(
'Input objects must implement Cake\View\Widget\WidgetInterface.'
);
}
if ($type === 'string') {
$widget = [$widget];
}
$class = array_shift($widget);
$className = App::className($class, 'View/Input');
if ($className === false || !class_exists($className)) {
throw new \RuntimeException(sprintf('Unable to locate widget class "%s"', $class));
}
if ($type === 'array' && count($widget)) {
$reflection = new ReflectionClass($className);
$arguments = [$this->_templates];
foreach ($widget as $requirement) {
$arguments[] = $this->get($requirement);
}
$instance = $reflection->newInstanceArgs($arguments);
} else {
$instance = new $className($this->_templates);
}
if (!($instance instanceof WidgetInterface)) {
throw new \RuntimeException(sprintf('"%s" does not implement the WidgetInterface', $className));
}
return $instance;
}
}