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