-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathComponentManager.php
More file actions
239 lines (204 loc) · 6.61 KB
/
ComponentManager.php
File metadata and controls
239 lines (204 loc) · 6.61 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php namespace Cms\Classes;
use Illuminate\Support\Facades\App;
use Str;
use System\Classes\Extensions\PluginManager;
use SystemException;
/**
* Component manager
*
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
class ComponentManager
{
use \Winter\Storm\Support\Traits\Singleton;
/**
* @var array Cache of registration callbacks.
*/
protected $callbacks = [];
/**
* @var array An array where keys are codes and values are class names.
*/
protected $codeMap;
/**
* @var array An array where keys are class names and values are codes.
*/
protected $classMap;
/**
* @var array An array containing references to a corresponding plugin for each component class.
*/
protected $pluginMap;
/**
* @var array A cached array of component details.
*/
protected $detailsCache;
/**
* Scans each plugin an loads it's components.
* @return void
*/
protected function loadComponents()
{
/*
* Load module components
*/
foreach ($this->callbacks as $callback) {
$callback($this);
}
/*
* Load plugin components
*/
$pluginManager = PluginManager::instance();
$plugins = $pluginManager->getPlugins();
foreach ($plugins as $plugin) {
$components = $plugin->registerComponents();
if (!is_array($components)) {
continue;
}
foreach ($components as $className => $code) {
$this->registerComponent($className, $code, $plugin);
}
}
}
/**
* Manually registers a component for consideration. Usage:
*
* ComponentManager::registerComponents(function ($manager) {
* $manager->registerComponent('Winter\Demo\Components\Test', 'testComponent');
* });
*
* @param callable $definitions
* @return array Array values are class names.
*/
public function registerComponents(callable $definitions)
{
$this->callbacks[] = $definitions;
}
/**
* Registers a single component.
*/
public function registerComponent($className, $code = null, $plugin = null)
{
if (!$this->classMap) {
$this->classMap = [];
}
if (!$this->codeMap) {
$this->codeMap = [];
}
if (!$code) {
$code = Str::getClassId($className);
}
if ($code == 'viewBag' && $className != 'Cms\Components\ViewBag') {
throw new SystemException(sprintf(
'The component code viewBag is reserved. Please use another code for the component class %s.',
$className
));
}
$className = Str::normalizeClassName($className);
$this->codeMap[$code] = $className;
$this->classMap[$className] = $code;
if ($plugin !== null) {
$this->pluginMap[$className] = $plugin;
}
}
/**
* Returns a list of registered components.
* @return array Array keys are codes, values are class names.
*/
public function listComponents()
{
if ($this->codeMap === null) {
$this->loadComponents();
}
return $this->codeMap;
}
/**
* Returns an array of all component detail definitions.
* @return array Array keys are component codes, values are the details defined in the component.
*/
public function listComponentDetails()
{
if ($this->detailsCache !== null) {
return $this->detailsCache;
}
$details = [];
foreach ($this->listComponents() as $componentAlias => $componentClass) {
$details[$componentAlias] = $this->makeComponent($componentClass)->componentDetails();
}
return $this->detailsCache = $details;
}
/**
* Returns a class name from a component code
* Normalizes a class name or converts an code to it's class name.
* @return string The class name resolved, or null.
*/
public function resolve($name)
{
$codes = $this->listComponents();
if (isset($codes[$name])) {
return $codes[$name];
}
$name = Str::normalizeClassName($name);
if (isset($this->classMap[$name])) {
return $name;
}
return null;
}
/**
* Checks to see if a component has been registered.
* @param string $name A component class name or code.
* @return bool Returns true if the component is registered, otherwise false.
*/
public function hasComponent($name)
{
$className = $this->resolve($name);
if (!$className) {
return false;
}
return isset($this->classMap[$className]);
}
/**
* Makes a component object with properties set.
*
* @param string $name A component class name or code.
* @param CmsObject $cmsObject The Cms object that spawned this component.
* @param array $properties The properties set by the Page or Layout.
* @param bool $isSoftComponent Defines if this is a soft component.
*
* @return ComponentBase The component object.
* @throws SystemException If the (hard) component cannot be found or is not registered.
*/
public function makeComponent($name, $cmsObject = null, $properties = [], $isSoftComponent = false)
{
$className = $this->resolve(ltrim($name, '@'));
if (!$className && !$isSoftComponent) {
throw new SystemException(sprintf(
'Class name is not registered for the component "%s". Check the component plugin.',
$name
));
}
if (!class_exists($className) && !$isSoftComponent) {
throw new SystemException(sprintf(
'Component class not found "%s". Check the component plugin.',
$className
));
}
if (class_exists($className)) {
$component = App::make($className, ['cmsObject' => $cmsObject, 'properties' => $properties]);
$component->name = $name;
return $component;
}
}
/**
* Returns a parent plugin for a specific component object.
* @param mixed $component A component to find the plugin for.
* @return mixed Returns the plugin object or null.
*/
public function findComponentPlugin($component)
{
$className = Str::normalizeClassName(get_class($component));
if (isset($this->pluginMap[$className])) {
return $this->pluginMap[$className];
}
return null;
}
}