Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Asset;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Asset\Exception\IncompleteLibraryDefinitionException;
|
Chris@0
|
6 use Drupal\Core\Asset\Exception\InvalidLibrariesOverrideSpecificationException;
|
Chris@0
|
7 use Drupal\Core\Asset\Exception\InvalidLibraryFileException;
|
Chris@0
|
8 use Drupal\Core\Asset\Exception\LibraryDefinitionMissingLicenseException;
|
Chris@0
|
9 use Drupal\Core\Extension\ModuleHandlerInterface;
|
Chris@0
|
10 use Drupal\Core\Serialization\Yaml;
|
Chris@0
|
11 use Drupal\Core\Theme\ThemeManagerInterface;
|
Chris@0
|
12 use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
|
Chris@0
|
13 use Drupal\Component\Utility\NestedArray;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Parses library files to get extension data.
|
Chris@0
|
17 */
|
Chris@0
|
18 class LibraryDiscoveryParser {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The module handler.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var \Drupal\Core\Extension\ModuleHandlerInterface
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $moduleHandler;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The theme manager.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Drupal\Core\Theme\ThemeManagerInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $themeManager;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The app root.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $root;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Constructs a new LibraryDiscoveryParser instance.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @param string $root
|
Chris@0
|
45 * The app root.
|
Chris@0
|
46 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
47 * The module handler.
|
Chris@0
|
48 * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
|
Chris@0
|
49 * The theme manager.
|
Chris@0
|
50 */
|
Chris@0
|
51 public function __construct($root, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager) {
|
Chris@0
|
52 $this->root = $root;
|
Chris@0
|
53 $this->moduleHandler = $module_handler;
|
Chris@0
|
54 $this->themeManager = $theme_manager;
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Parses and builds up all the libraries information of an extension.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @param string $extension
|
Chris@0
|
61 * The name of the extension that registered a library.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @return array
|
Chris@0
|
64 * All library definitions of the passed extension.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @throws \Drupal\Core\Asset\Exception\IncompleteLibraryDefinitionException
|
Chris@0
|
67 * Thrown when a library has no js/css/setting.
|
Chris@0
|
68 * @throws \UnexpectedValueException
|
Chris@0
|
69 * Thrown when a js file defines a positive weight.
|
Chris@0
|
70 */
|
Chris@0
|
71 public function buildByExtension($extension) {
|
Chris@0
|
72 if ($extension === 'core') {
|
Chris@0
|
73 $path = 'core';
|
Chris@0
|
74 $extension_type = 'core';
|
Chris@0
|
75 }
|
Chris@0
|
76 else {
|
Chris@0
|
77 if ($this->moduleHandler->moduleExists($extension)) {
|
Chris@0
|
78 $extension_type = 'module';
|
Chris@0
|
79 }
|
Chris@0
|
80 else {
|
Chris@0
|
81 $extension_type = 'theme';
|
Chris@0
|
82 }
|
Chris@0
|
83 $path = $this->drupalGetPath($extension_type, $extension);
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 $libraries = $this->parseLibraryInfo($extension, $path);
|
Chris@0
|
87 $libraries = $this->applyLibrariesOverride($libraries, $extension);
|
Chris@0
|
88
|
Chris@0
|
89 foreach ($libraries as $id => &$library) {
|
Chris@0
|
90 if (!isset($library['js']) && !isset($library['css']) && !isset($library['drupalSettings'])) {
|
Chris@0
|
91 throw new IncompleteLibraryDefinitionException(sprintf("Incomplete library definition for definition '%s' in extension '%s'", $id, $extension));
|
Chris@0
|
92 }
|
Chris@0
|
93 $library += ['dependencies' => [], 'js' => [], 'css' => []];
|
Chris@0
|
94
|
Chris@0
|
95 if (isset($library['header']) && !is_bool($library['header'])) {
|
Chris@0
|
96 throw new \LogicException(sprintf("The 'header' key in the library definition '%s' in extension '%s' is invalid: it must be a boolean.", $id, $extension));
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 if (isset($library['version'])) {
|
Chris@0
|
100 // @todo Retrieve version of a non-core extension.
|
Chris@0
|
101 if ($library['version'] === 'VERSION') {
|
Chris@0
|
102 $library['version'] = \Drupal::VERSION;
|
Chris@0
|
103 }
|
Chris@0
|
104 // Remove 'v' prefix from external library versions.
|
Chris@0
|
105 elseif ($library['version'][0] === 'v') {
|
Chris@0
|
106 $library['version'] = substr($library['version'], 1);
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 // If this is a 3rd party library, the license info is required.
|
Chris@0
|
111 if (isset($library['remote']) && !isset($library['license'])) {
|
Chris@0
|
112 throw new LibraryDefinitionMissingLicenseException(sprintf("Missing license information in library definition for definition '%s' extension '%s': it has a remote, but no license.", $id, $extension));
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 // Assign Drupal's license to libraries that don't have license info.
|
Chris@0
|
116 if (!isset($library['license'])) {
|
Chris@0
|
117 $library['license'] = [
|
Chris@0
|
118 'name' => 'GNU-GPL-2.0-or-later',
|
Chris@0
|
119 'url' => 'https://www.drupal.org/licensing/faq',
|
Chris@0
|
120 'gpl-compatible' => TRUE,
|
Chris@0
|
121 ];
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 foreach (['js', 'css'] as $type) {
|
Chris@0
|
125 // Prepare (flatten) the SMACSS-categorized definitions.
|
Chris@0
|
126 // @todo After Asset(ic) changes, retain the definitions as-is and
|
Chris@0
|
127 // properly resolve dependencies for all (css) libraries per category,
|
Chris@0
|
128 // and only once prior to rendering out an HTML page.
|
Chris@0
|
129 if ($type == 'css' && !empty($library[$type])) {
|
Chris@14
|
130 assert(static::validateCssLibrary($library[$type]) < 2, 'CSS files should be specified as key/value pairs, where the values are configuration options. See https://www.drupal.org/node/2274843.');
|
Chris@14
|
131 assert(static::validateCssLibrary($library[$type]) === 0, 'CSS must be nested under a category. See https://www.drupal.org/node/2274843.');
|
Chris@0
|
132 foreach ($library[$type] as $category => $files) {
|
Chris@0
|
133 $category_weight = 'CSS_' . strtoupper($category);
|
Chris@14
|
134 assert(defined($category_weight), 'Invalid CSS category: ' . $category . '. See https://www.drupal.org/node/2274843.');
|
Chris@0
|
135 foreach ($files as $source => $options) {
|
Chris@0
|
136 if (!isset($options['weight'])) {
|
Chris@0
|
137 $options['weight'] = 0;
|
Chris@0
|
138 }
|
Chris@0
|
139 // Apply the corresponding weight defined by CSS_* constants.
|
Chris@0
|
140 $options['weight'] += constant($category_weight);
|
Chris@0
|
141 $library[$type][$source] = $options;
|
Chris@0
|
142 }
|
Chris@0
|
143 unset($library[$type][$category]);
|
Chris@0
|
144 }
|
Chris@0
|
145 }
|
Chris@0
|
146 foreach ($library[$type] as $source => $options) {
|
Chris@0
|
147 unset($library[$type][$source]);
|
Chris@0
|
148 // Allow to omit the options hashmap in YAML declarations.
|
Chris@0
|
149 if (!is_array($options)) {
|
Chris@0
|
150 $options = [];
|
Chris@0
|
151 }
|
Chris@0
|
152 if ($type == 'js' && isset($options['weight']) && $options['weight'] > 0) {
|
Chris@0
|
153 throw new \UnexpectedValueException("The $extension/$id library defines a positive weight for '$source'. Only negative weights are allowed (but should be avoided). Instead of a positive weight, specify accurate dependencies for this library.");
|
Chris@0
|
154 }
|
Chris@0
|
155 // Unconditionally apply default groups for the defined asset files.
|
Chris@0
|
156 // The library system is a dependency management system. Each library
|
Chris@0
|
157 // properly specifies its dependencies instead of relying on a custom
|
Chris@0
|
158 // processing order.
|
Chris@0
|
159 if ($type == 'js') {
|
Chris@0
|
160 $options['group'] = JS_LIBRARY;
|
Chris@0
|
161 }
|
Chris@0
|
162 elseif ($type == 'css') {
|
Chris@0
|
163 $options['group'] = $extension_type == 'theme' ? CSS_AGGREGATE_THEME : CSS_AGGREGATE_DEFAULT;
|
Chris@0
|
164 }
|
Chris@0
|
165 // By default, all library assets are files.
|
Chris@0
|
166 if (!isset($options['type'])) {
|
Chris@0
|
167 $options['type'] = 'file';
|
Chris@0
|
168 }
|
Chris@0
|
169 if ($options['type'] == 'external') {
|
Chris@0
|
170 $options['data'] = $source;
|
Chris@0
|
171 }
|
Chris@0
|
172 // Determine the file asset URI.
|
Chris@0
|
173 else {
|
Chris@0
|
174 if ($source[0] === '/') {
|
Chris@0
|
175 // An absolute path maps to DRUPAL_ROOT / base_path().
|
Chris@0
|
176 if ($source[1] !== '/') {
|
Chris@0
|
177 $options['data'] = substr($source, 1);
|
Chris@0
|
178 }
|
Chris@0
|
179 // A protocol-free URI (e.g., //cdn.com/example.js) is external.
|
Chris@0
|
180 else {
|
Chris@0
|
181 $options['type'] = 'external';
|
Chris@0
|
182 $options['data'] = $source;
|
Chris@0
|
183 }
|
Chris@0
|
184 }
|
Chris@0
|
185 // A stream wrapper URI (e.g., public://generated_js/example.js).
|
Chris@0
|
186 elseif ($this->fileValidUri($source)) {
|
Chris@0
|
187 $options['data'] = $source;
|
Chris@0
|
188 }
|
Chris@0
|
189 // A regular URI (e.g., http://example.com/example.js) without
|
Chris@0
|
190 // 'external' explicitly specified, which may happen if, e.g.
|
Chris@0
|
191 // libraries-override is used.
|
Chris@0
|
192 elseif ($this->isValidUri($source)) {
|
Chris@0
|
193 $options['type'] = 'external';
|
Chris@0
|
194 $options['data'] = $source;
|
Chris@0
|
195 }
|
Chris@0
|
196 // By default, file paths are relative to the registering extension.
|
Chris@0
|
197 else {
|
Chris@0
|
198 $options['data'] = $path . '/' . $source;
|
Chris@0
|
199 }
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 if (!isset($library['version'])) {
|
Chris@0
|
203 // @todo Get the information from the extension.
|
Chris@0
|
204 $options['version'] = -1;
|
Chris@0
|
205 }
|
Chris@0
|
206 else {
|
Chris@0
|
207 $options['version'] = $library['version'];
|
Chris@0
|
208 }
|
Chris@0
|
209
|
Chris@0
|
210 // Set the 'minified' flag on JS file assets, default to FALSE.
|
Chris@0
|
211 if ($type == 'js' && $options['type'] == 'file') {
|
Chris@0
|
212 $options['minified'] = isset($options['minified']) ? $options['minified'] : FALSE;
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 $library[$type][] = $options;
|
Chris@0
|
216 }
|
Chris@0
|
217 }
|
Chris@0
|
218 }
|
Chris@0
|
219
|
Chris@0
|
220 return $libraries;
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 /**
|
Chris@0
|
224 * Parses a given library file and allows modules and themes to alter it.
|
Chris@0
|
225 *
|
Chris@0
|
226 * This method sets the parsed information onto the library property.
|
Chris@0
|
227 *
|
Chris@0
|
228 * Library information is parsed from *.libraries.yml files; see
|
Chris@0
|
229 * editor.library.yml for an example. Every library must have at least one js
|
Chris@0
|
230 * or css entry. Each entry starts with a machine name and defines the
|
Chris@0
|
231 * following elements:
|
Chris@0
|
232 * - js: A list of JavaScript files to include. Each file is keyed by the file
|
Chris@0
|
233 * path. An item can have several attributes (like HTML
|
Chris@0
|
234 * attributes). For example:
|
Chris@0
|
235 * @code
|
Chris@0
|
236 * js:
|
Chris@0
|
237 * path/js/file.js: { attributes: { defer: true } }
|
Chris@0
|
238 * @endcode
|
Chris@0
|
239 * If the file has no special attributes, just use an empty object:
|
Chris@0
|
240 * @code
|
Chris@0
|
241 * js:
|
Chris@0
|
242 * path/js/file.js: {}
|
Chris@0
|
243 * @endcode
|
Chris@0
|
244 * The path of the file is relative to the module or theme directory, unless
|
Chris@0
|
245 * it starts with a /, in which case it is relative to the Drupal root. If
|
Chris@0
|
246 * the file path starts with //, it will be treated as a protocol-free,
|
Chris@0
|
247 * external resource (e.g., //cdn.com/library.js). Full URLs
|
Chris@0
|
248 * (e.g., http://cdn.com/library.js) as well as URLs that use a valid
|
Chris@0
|
249 * stream wrapper (e.g., public://path/to/file.js) are also supported.
|
Chris@0
|
250 * - css: A list of categories for which the library provides CSS files. The
|
Chris@0
|
251 * available categories are:
|
Chris@0
|
252 * - base
|
Chris@0
|
253 * - layout
|
Chris@0
|
254 * - component
|
Chris@0
|
255 * - state
|
Chris@0
|
256 * - theme
|
Chris@0
|
257 * Each category is itself a key for a sub-list of CSS files to include:
|
Chris@0
|
258 * @code
|
Chris@0
|
259 * css:
|
Chris@0
|
260 * component:
|
Chris@0
|
261 * css/file.css: {}
|
Chris@0
|
262 * @endcode
|
Chris@0
|
263 * Just like with JavaScript files, each CSS file is the key of an object
|
Chris@0
|
264 * that can define specific attributes. The format of the file path is the
|
Chris@0
|
265 * same as for the JavaScript files.
|
Chris@0
|
266 * - dependencies: A list of libraries this library depends on.
|
Chris@0
|
267 * - version: The library version. The string "VERSION" can be used to mean
|
Chris@0
|
268 * the current Drupal core version.
|
Chris@0
|
269 * - header: By default, JavaScript files are included in the footer. If the
|
Chris@0
|
270 * script must be included in the header (along with all its dependencies),
|
Chris@0
|
271 * set this to true. Defaults to false.
|
Chris@0
|
272 * - minified: If the file is already minified, set this to true to avoid
|
Chris@0
|
273 * minifying it again. Defaults to false.
|
Chris@0
|
274 * - remote: If the library is a third-party script, this provides the
|
Chris@0
|
275 * repository URL for reference.
|
Chris@0
|
276 * - license: If the remote property is set, the license information is
|
Chris@0
|
277 * required. It has 3 properties:
|
Chris@0
|
278 * - name: The human-readable name of the license.
|
Chris@0
|
279 * - url: The URL of the license file/information for the version of the
|
Chris@0
|
280 * library used.
|
Chris@0
|
281 * - gpl-compatible: A Boolean for whether this library is GPL compatible.
|
Chris@0
|
282 *
|
Chris@0
|
283 * See https://www.drupal.org/node/2274843#define-library for more
|
Chris@0
|
284 * information.
|
Chris@0
|
285 *
|
Chris@0
|
286 * @param string $extension
|
Chris@0
|
287 * The name of the extension that registered a library.
|
Chris@0
|
288 * @param string $path
|
Chris@0
|
289 * The relative path to the extension.
|
Chris@0
|
290 *
|
Chris@0
|
291 * @return array
|
Chris@0
|
292 * An array of parsed library data.
|
Chris@0
|
293 *
|
Chris@0
|
294 * @throws \Drupal\Core\Asset\Exception\InvalidLibraryFileException
|
Chris@0
|
295 * Thrown when a parser exception got thrown.
|
Chris@0
|
296 */
|
Chris@0
|
297 protected function parseLibraryInfo($extension, $path) {
|
Chris@0
|
298 $libraries = [];
|
Chris@0
|
299
|
Chris@0
|
300 $library_file = $path . '/' . $extension . '.libraries.yml';
|
Chris@0
|
301 if (file_exists($this->root . '/' . $library_file)) {
|
Chris@0
|
302 try {
|
Chris@0
|
303 $libraries = Yaml::decode(file_get_contents($this->root . '/' . $library_file));
|
Chris@0
|
304 }
|
Chris@0
|
305 catch (InvalidDataTypeException $e) {
|
Chris@0
|
306 // Rethrow a more helpful exception to provide context.
|
Chris@0
|
307 throw new InvalidLibraryFileException(sprintf('Invalid library definition in %s: %s', $library_file, $e->getMessage()), 0, $e);
|
Chris@0
|
308 }
|
Chris@0
|
309 }
|
Chris@0
|
310
|
Chris@0
|
311 // Allow modules to add dynamic library definitions.
|
Chris@0
|
312 $hook = 'library_info_build';
|
Chris@0
|
313 if ($this->moduleHandler->implementsHook($extension, $hook)) {
|
Chris@0
|
314 $libraries = NestedArray::mergeDeep($libraries, $this->moduleHandler->invoke($extension, $hook));
|
Chris@0
|
315 }
|
Chris@0
|
316
|
Chris@0
|
317 // Allow modules to alter the module's registered libraries.
|
Chris@0
|
318 $this->moduleHandler->alter('library_info', $libraries, $extension);
|
Chris@0
|
319 $this->themeManager->alter('library_info', $libraries, $extension);
|
Chris@0
|
320
|
Chris@0
|
321 return $libraries;
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 /**
|
Chris@0
|
325 * Apply libraries overrides specified for the current active theme.
|
Chris@0
|
326 *
|
Chris@0
|
327 * @param array $libraries
|
Chris@0
|
328 * The libraries definitions.
|
Chris@0
|
329 * @param string $extension
|
Chris@0
|
330 * The extension in which these libraries are defined.
|
Chris@0
|
331 *
|
Chris@0
|
332 * @return array
|
Chris@0
|
333 * The modified libraries definitions.
|
Chris@0
|
334 */
|
Chris@0
|
335 protected function applyLibrariesOverride($libraries, $extension) {
|
Chris@0
|
336 $active_theme = $this->themeManager->getActiveTheme();
|
Chris@0
|
337 // ActiveTheme::getLibrariesOverride() returns libraries-overrides for the
|
Chris@0
|
338 // current theme as well as all its base themes.
|
Chris@0
|
339 $all_libraries_overrides = $active_theme->getLibrariesOverride();
|
Chris@0
|
340 foreach ($all_libraries_overrides as $theme_path => $libraries_overrides) {
|
Chris@0
|
341 foreach ($libraries as $library_name => $library) {
|
Chris@0
|
342 // Process libraries overrides.
|
Chris@0
|
343 if (isset($libraries_overrides["$extension/$library_name"])) {
|
Chris@0
|
344 // Active theme defines an override for this library.
|
Chris@0
|
345 $override_definition = $libraries_overrides["$extension/$library_name"];
|
Chris@0
|
346 if (is_string($override_definition) || $override_definition === FALSE) {
|
Chris@0
|
347 // A string or boolean definition implies an override (or removal)
|
Chris@0
|
348 // for the whole library. Use the override key to specify that this
|
Chris@0
|
349 // library will be overridden when it is called.
|
Chris@0
|
350 // @see \Drupal\Core\Asset\LibraryDiscovery::getLibraryByName()
|
Chris@0
|
351 if ($override_definition) {
|
Chris@0
|
352 $libraries[$library_name]['override'] = $override_definition;
|
Chris@0
|
353 }
|
Chris@0
|
354 else {
|
Chris@0
|
355 $libraries[$library_name]['override'] = FALSE;
|
Chris@0
|
356 }
|
Chris@0
|
357 }
|
Chris@0
|
358 elseif (is_array($override_definition)) {
|
Chris@0
|
359 // An array definition implies an override for an asset within this
|
Chris@0
|
360 // library.
|
Chris@0
|
361 foreach ($override_definition as $sub_key => $value) {
|
Chris@0
|
362 // Throw an exception if the asset is not properly specified.
|
Chris@0
|
363 if (!is_array($value)) {
|
Chris@0
|
364 throw new InvalidLibrariesOverrideSpecificationException(sprintf('Library asset %s is not correctly specified. It should be in the form "extension/library_name/sub_key/path/to/asset.js".', "$extension/$library_name/$sub_key"));
|
Chris@0
|
365 }
|
Chris@0
|
366 if ($sub_key === 'drupalSettings') {
|
Chris@0
|
367 // drupalSettings may not be overridden.
|
Chris@0
|
368 throw new InvalidLibrariesOverrideSpecificationException(sprintf('drupalSettings may not be overridden in libraries-override. Trying to override %s. Use hook_library_info_alter() instead.', "$extension/$library_name/$sub_key"));
|
Chris@0
|
369 }
|
Chris@0
|
370 elseif ($sub_key === 'css') {
|
Chris@0
|
371 // SMACSS category should be incorporated into the asset name.
|
Chris@0
|
372 foreach ($value as $category => $overrides) {
|
Chris@0
|
373 $this->setOverrideValue($libraries[$library_name], [$sub_key, $category], $overrides, $theme_path);
|
Chris@0
|
374 }
|
Chris@0
|
375 }
|
Chris@0
|
376 else {
|
Chris@0
|
377 $this->setOverrideValue($libraries[$library_name], [$sub_key], $value, $theme_path);
|
Chris@0
|
378 }
|
Chris@0
|
379 }
|
Chris@0
|
380 }
|
Chris@0
|
381 }
|
Chris@0
|
382 }
|
Chris@0
|
383 }
|
Chris@0
|
384
|
Chris@0
|
385 return $libraries;
|
Chris@0
|
386 }
|
Chris@0
|
387
|
Chris@0
|
388 /**
|
Chris@0
|
389 * Wraps drupal_get_path().
|
Chris@0
|
390 */
|
Chris@0
|
391 protected function drupalGetPath($type, $name) {
|
Chris@0
|
392 return drupal_get_path($type, $name);
|
Chris@0
|
393 }
|
Chris@0
|
394
|
Chris@0
|
395 /**
|
Chris@0
|
396 * Wraps file_valid_uri().
|
Chris@0
|
397 */
|
Chris@0
|
398 protected function fileValidUri($source) {
|
Chris@0
|
399 return file_valid_uri($source);
|
Chris@0
|
400 }
|
Chris@0
|
401
|
Chris@0
|
402 /**
|
Chris@0
|
403 * Determines if the supplied string is a valid URI.
|
Chris@0
|
404 */
|
Chris@0
|
405 protected function isValidUri($string) {
|
Chris@0
|
406 return count(explode('://', $string)) === 2;
|
Chris@0
|
407 }
|
Chris@0
|
408
|
Chris@0
|
409 /**
|
Chris@0
|
410 * Overrides the specified library asset.
|
Chris@0
|
411 *
|
Chris@0
|
412 * @param array $library
|
Chris@0
|
413 * The containing library definition.
|
Chris@0
|
414 * @param array $sub_key
|
Chris@0
|
415 * An array containing the sub-keys specifying the library asset, e.g.
|
Chris@0
|
416 * @code['js']@endcode or @code['css', 'component']@endcode
|
Chris@0
|
417 * @param array $overrides
|
Chris@0
|
418 * Specifies the overrides, this is an array where the key is the asset to
|
Chris@0
|
419 * be overridden while the value is overriding asset.
|
Chris@0
|
420 */
|
Chris@0
|
421 protected function setOverrideValue(array &$library, array $sub_key, array $overrides, $theme_path) {
|
Chris@0
|
422 foreach ($overrides as $original => $replacement) {
|
Chris@0
|
423 // Get the attributes of the asset to be overridden. If the key does
|
Chris@0
|
424 // not exist, then throw an exception.
|
Chris@0
|
425 $key_exists = NULL;
|
Chris@0
|
426 $parents = array_merge($sub_key, [$original]);
|
Chris@0
|
427 // Save the attributes of the library asset to be overridden.
|
Chris@0
|
428 $attributes = NestedArray::getValue($library, $parents, $key_exists);
|
Chris@0
|
429 if ($key_exists) {
|
Chris@0
|
430 // Remove asset to be overridden.
|
Chris@0
|
431 NestedArray::unsetValue($library, $parents);
|
Chris@0
|
432 // No need to replace if FALSE is specified, since that is a removal.
|
Chris@0
|
433 if ($replacement) {
|
Chris@0
|
434 // Ensure the replacement path is relative to drupal root.
|
Chris@0
|
435 $replacement = $this->resolveThemeAssetPath($theme_path, $replacement);
|
Chris@0
|
436 $new_parents = array_merge($sub_key, [$replacement]);
|
Chris@0
|
437 // Replace with an override if specified.
|
Chris@0
|
438 NestedArray::setValue($library, $new_parents, $attributes);
|
Chris@0
|
439 }
|
Chris@0
|
440 }
|
Chris@0
|
441 }
|
Chris@0
|
442 }
|
Chris@0
|
443
|
Chris@0
|
444 /**
|
Chris@0
|
445 * Ensures that a full path is returned for an overriding theme asset.
|
Chris@0
|
446 *
|
Chris@0
|
447 * @param string $theme_path
|
Chris@0
|
448 * The theme or base theme.
|
Chris@0
|
449 * @param string $overriding_asset
|
Chris@0
|
450 * The overriding library asset.
|
Chris@0
|
451 *
|
Chris@0
|
452 * @return string
|
Chris@0
|
453 * A fully resolved theme asset path relative to the Drupal directory.
|
Chris@0
|
454 */
|
Chris@0
|
455 protected function resolveThemeAssetPath($theme_path, $overriding_asset) {
|
Chris@0
|
456 if ($overriding_asset[0] !== '/' && !$this->isValidUri($overriding_asset)) {
|
Chris@0
|
457 // The destination is not an absolute path and it's not a URI (e.g.
|
Chris@0
|
458 // public://generated_js/example.js or http://example.com/js/my_js.js), so
|
Chris@0
|
459 // it's relative to the theme.
|
Chris@0
|
460 return '/' . $theme_path . '/' . $overriding_asset;
|
Chris@0
|
461 }
|
Chris@0
|
462 return $overriding_asset;
|
Chris@0
|
463 }
|
Chris@0
|
464
|
Chris@0
|
465 /**
|
Chris@0
|
466 * Validates CSS library structure.
|
Chris@0
|
467 *
|
Chris@0
|
468 * @param array $library
|
Chris@0
|
469 * The library definition array.
|
Chris@0
|
470 *
|
Chris@0
|
471 * @return int
|
Chris@0
|
472 * Returns based on validity:
|
Chris@0
|
473 * - 0 if the library definition is valid
|
Chris@0
|
474 * - 1 if the library definition has improper nesting
|
Chris@0
|
475 * - 2 if the library definition specifies files as an array
|
Chris@0
|
476 */
|
Chris@0
|
477 public static function validateCssLibrary($library) {
|
Chris@0
|
478 $categories = [];
|
Chris@0
|
479 // Verify options first and return early if invalid.
|
Chris@0
|
480 foreach ($library as $category => $files) {
|
Chris@0
|
481 if (!is_array($files)) {
|
Chris@0
|
482 return 2;
|
Chris@0
|
483 }
|
Chris@0
|
484 $categories[] = $category;
|
Chris@0
|
485 foreach ($files as $source => $options) {
|
Chris@0
|
486 if (!is_array($options)) {
|
Chris@0
|
487 return 1;
|
Chris@0
|
488 }
|
Chris@0
|
489 }
|
Chris@0
|
490 }
|
Chris@0
|
491
|
Chris@0
|
492 return 0;
|
Chris@0
|
493 }
|
Chris@0
|
494
|
Chris@0
|
495 }
|