forked from oetterer/BootstrapComponents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentLibrary.php
More file actions
407 lines (369 loc) · 11.7 KB
/
ComponentLibrary.php
File metadata and controls
407 lines (369 loc) · 11.7 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/**
* Contains class holding and distributing information about all available components.
*
* @copyright (C) 2018, Tobias Oetterer, Paderborn University
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later)
*
* This file is part of the MediaWiki extension BootstrapComponents.
* The BootstrapComponents extension is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The BootstrapComponents extension is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @file
* @ingroup BootstrapComponents
* @author Tobias Oetterer
*/
namespace BootstrapComponents;
use \MediaWiki\MediaWikiServices;
use \MWException;
/**
* Class ComponentLibrary
*
* Holds information about all registered components
*
* @since 1.0
*/
class ComponentLibrary {
/**
* @var array
*/
const DEFAULT_ATTRIBUTES = [ 'class', 'id', 'style' ];
/**
* File, that holds the component definition list.
*
* @var string
*/
const DEFINITIONS_FILE = __DIR__ . DIRECTORY_SEPARATOR . 'ComponentsDefinition.json';
/**
* @var string
*/
const HANDLER_TYPE_PARSER_FUNCTION = 'function';
/**
* @var string
*/
const HANDLER_TYPE_TAG_EXTENSION = 'tag';
/**
* @var string
*/
const PARSER_HOOK_PREFIX = 'bootstrap_';
/**
* This array holds all the data for all known components, whether they are registered or not.
*
* Array has form
* <pre>
* "componentIdentifier" => [
* "class" => <className>,
* "name" => <componentName>
* "handlerType" => <handlerType>,
* "attributes" => [ "attr1", "attr2", ... ],
* "aliases" => [ "alias" => "attribute", ... ]
* "modules" => [
* "default" => [ "module1", "module2", ... ],
* "<skin>" => [ "module1", "module2", ... ],
* ]
* ]
* </pre>
*
* @var array $componentDataStore
*/
private $componentDataStore;
/**
* The list of registered/allowed bootstrap components, name or alias
*
* @var string[] $registeredComponents
*/
private $registeredComponents;
/**
* @param string $componentName
*
* @return string
*/
public static function compileParserHookStringFor( $componentName ) {
return self::PARSER_HOOK_PREFIX . strtolower( $componentName );
}
/**
* ComponentLibrary constructor.
*
* Do not instantiate directly, but use {@see ApplicationFactory::getComponentLibrary} instead.
*
* @param bool|array $componentWhiteList (see {@see \BootstrapComponents\ComponentLibrary::$componentWhiteList})
*
* @throws \ConfigException cascading {@see \ConfigFactory::makeConfig} and
* @see ApplicationFactory::getComponentLibrary
*
*/
public function __construct( $componentWhiteList = null ) {
if ( is_null( $componentWhiteList ) ) {
$myConfig = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'BootstrapComponents' );
$componentWhiteList = $myConfig->has( 'BootstrapComponentsWhitelist' )
? $myConfig->get( 'BootstrapComponentsWhitelist' )
: true;
}
$componentWhiteList = $this->mangle( $componentWhiteList );
$this->registeredComponents = $this->registerComponents( $componentWhiteList );
}
/**
* Compiles an array for all bootstrap component parser functions to be uses in the BootstrapComponents.magic.php file.
*
* @return array
*/
public function compileMagicWordsArray() {
$magicWords = [];
foreach ( $this->getRegisteredComponents() as $componentName ) {
if ( $this->isParserFunction( $componentName ) ) {
$magicWords[self::compileParserHookStringFor( $componentName )]
= [ 0, self::compileParserHookStringFor( $componentName ) ];
}
}
return $magicWords;
}
/**
* Checks, if component $componentIdentifier is registered
*
* @param string $componentIdentifier
*
* @return bool
*/
public function isRegistered( $componentIdentifier ) {
return in_array( $componentIdentifier, $this->registeredComponents, true );
}
/**
* Returns the defined/allowed attribute aliases for component/alias $componentIdentifier.
*
* @param string $componentIdentifier
*
* @return array
* @throws MWException provided component is not known
*/
public function getAliasesFor( $componentIdentifier ) {
return $this->accessComponentDataStore( $componentIdentifier, 'aliases' );
}
/**
* Returns the defined/allowed attributes for component/alias $componentIdentifier.
*
* @param string $componentIdentifier
*
* @return array
* @throws MWException provided component is not known
*/
public function getAttributesFor( $componentIdentifier ) {
return $this->accessComponentDataStore( $componentIdentifier, 'attributes' );
}
/**
* Returns class name for a registered component/alias.
*
* @param string $componentIdentifier
*
* @return string
*/
public function getClassFor( $componentIdentifier ) {
return $this->accessComponentDataStore( $componentIdentifier, 'class' );
}
/**
* Returns handler type for a registered component/alias. 'UNKNOWN' if unknown component.
*
* @param string $componentIdentifier
*
* @return string
* @see \BootstrapComponents\ComponentLibrary::HANDLER_TYPE_PARSER_FUNCTION,
* \BootstrapComponents\ComponentLibrary::HANDLER_TYPE_TAG_EXTENSION
*
*/
public function getHandlerTypeFor( $componentIdentifier ) {
try {
return $this->accessComponentDataStore( $componentIdentifier, 'handlerType' );
} catch ( MWException $e ) {
return 'UNKNOWN';
}
}
/**
* Returns an array of all the known components' names, _excluding_ aliases,
*
* @return array
*/
public function getKnownComponents() {
return array_keys( $this->getComponentDataStore() );
}
/**
* Returns all the needed modules for a component/alias. False, if there are none.
* If skin is set, returns all modules especially registered for that skin as well
*
* @param string $componentIdentifier
* @param string $skin
*
* @return array
*/
public function getModulesFor( $componentIdentifier, $skin = null ) {
$allModules = $this->accessComponentDataStore( $componentIdentifier, 'modules' );
$modules = isset( $allModules['default'] )
? (array) $allModules['default']
: [];
if ( $skin === null || !isset( $allModules[$skin] ) ) {
return $modules;
}
return array_merge(
$modules,
(array) $allModules[$skin]
);
}
/**
* Returns the component name for a given class.
*
* @param string $componentClass
*
* @throws MWException if supplied class is not registered
*
* @return string
*/
public function getNameFor( $componentClass ) {
$component = null;
// if $componentClass is not in values in $this->registeredComponentClasses, this has to fail
foreach ( $this->getComponentDataStore() as $componentIdentifier => $componentData ) {
if ( isset( $componentData['class'] ) && ( $componentData['class'] == $componentClass ) && isset( $componentData['name'] ) ) {
$component = $componentData['name'];
break;
}
}
if ( is_null( $component ) ) {
throw new MWException( 'Trying to get a component name for unregistered class "' . (string) $componentClass . '"!' );
}
return $this->accessComponentDataStore( $component, 'name' );
}
/**
* Returns an array of all the registered component's names. Including aliases.
*
* @return string[]
*/
public function getRegisteredComponents() {
return $this->registeredComponents;
}
/**
* True, if referenced component is registered as parser function.
*
* @param string $componentName
*
* @return bool
*/
public function isParserFunction( $componentName ) {
return $this->getHandlerTypeFor( $componentName ) == self::HANDLER_TYPE_PARSER_FUNCTION;
}
/**
* True, if referenced component is registered as tag extension.
*
* @param string $componentName
*
* @return bool
*/
public function isTagExtension( $componentName ) {
return $this->getHandlerTypeFor( $componentName ) == self::HANDLER_TYPE_TAG_EXTENSION;
}
/**
* @param string $componentIdentifier
* @param string $field
*
* @throws MWException on non existing $componentIdentifier or $field
* @return mixed
*/
protected function accessComponentDataStore( $componentIdentifier, $field ) {
if ( !isset( $this->getComponentDataStore()[$componentIdentifier][$field] ) ) {
throw new MWException(
'Trying to access undefined field \'' . $field . '\' of component \'' . $componentIdentifier . '\'. Aborting'
);
}
return $this->getComponentDataStore()[$componentIdentifier][$field];
}
/**
* Sees to it, that the whitelist (if it is an array) contains only lowercase strings.
*
* @param bool|array $componentWhiteList
*
* @return bool|array
*/
private function mangle( $componentWhiteList ) {
if ( !is_array( $componentWhiteList ) ) {
return $componentWhiteList;
}
$newWhiteList = [];
foreach ( $componentWhiteList as $element ) {
$newWhiteList[] = strtolower( trim( $element ) );
}
return $newWhiteList;
}
/**
* This adds the default attributes to the attribute list.
*
* @param array $componentAttributes
*
* @return array
*/
private function normalizeAttributes( $componentAttributes ) {
$componentAttributes = (array) $componentAttributes;
$componentAttributes = array_unique(
array_merge(
$componentAttributes,
self::DEFAULT_ATTRIBUTES
)
);
return $componentAttributes;
}
/**
* Generates the array for registered components containing all whitelisted component identifiers
*
* @param bool|array $componentWhiteList
*
* @return string[] list of registered component identifiers
*/
private function registerComponents( $componentWhiteList ) {
$registeredComponents = [];
foreach ( $this->getKnownComponents() as $componentIdentifier ) {
if ( !$componentWhiteList || (is_array( $componentWhiteList ) && !in_array( $componentIdentifier, $componentWhiteList )) ) {
// if $componentWhiteList is false, or and array and does not contain the $componentIdentifier, we will not register it
continue;
}
$registeredComponents[] = $componentIdentifier;
}
return $registeredComponents;
}
/**
* Raw library data used in registration process.
*
* @return array
*/
private function getComponentDataStore() {
if ( !empty( $this->componentDataStore ) ) {
return $this->componentDataStore;
}
$rawData = json_decode( file_get_contents( self::DEFINITIONS_FILE ), JSON_OBJECT_AS_ARRAY );
$componentAliases = [];
$componentDataStore = [];
foreach ( $rawData as $componentName => $componentData ) {
if ( !is_array( $componentData ) ) {
$componentAliases[$componentName] = trim( (string) $componentData );
continue;
}
$componentData['name'] = $componentName;
$componentData['attributes'] = $this->normalizeAttributes(
(isset( $componentData['attributes'] ) ? $componentData['attributes'] : [])
);
$componentData['aliases'] = isset( $componentData['aliases'] ) ? $componentData['aliases'] : [];
$componentData['modules'] = isset( $componentData['modules'] ) ? $componentData['modules'] : [];
$componentDataStore[$componentName] = $componentData;
}
foreach ( $componentAliases as $alias => $componentName ) {
if ( isset( $componentDataStore[$componentName] ) ) {
$componentDataStore[$alias] = $componentDataStore[$componentName];
}
}
return $this->componentDataStore = $componentDataStore;
}
}