Further modules included.
[yaffs-website] / web / modules / contrib / libraries / src / ExternalLibrary / Asset / LocalRemoteAssetTrait.php
1 <?php
2
3 namespace Drupal\libraries\ExternalLibrary\Asset;
4
5 /**
6  * A trait for asset libraries that serve local and remote files.
7  *
8  * If the library files are available locally, they are served locally.
9  * Otherwise, the remote files are served, assuming a remote URL is specified.
10  *
11  * This trait should only be used in classes implementing LocalLibraryInterface
12  * and RemoteLibraryInterface.
13  *
14  * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface
15  * @see \Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface
16  */
17 trait LocalRemoteAssetTrait {
18
19   /**
20    * Checks whether this library can be attached.
21    *
22    * @return bool
23    *   TRUE if the library can be attached; FALSE otherwise.
24    *
25    * @see \Drupal\libraries\ExternalLibrary\Asset\SingleAssetLibraryTrait::canBeAttached()
26    */
27   protected function canBeAttached() {
28     /** @var \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface|\Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface $this */
29     return ($this->isInstalled() || $this->hasRemoteUrl());
30   }
31
32   /**
33    * Gets the prefix to prepend to file paths.
34    *
35    * For local libraries this is the library path, for remote libraries this is
36    * the remote URL.
37    *
38    * @return string
39    *   The path prefix.
40    */
41   protected function getPathPrefix() {
42     /** @var \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface|\Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface $this */
43     if ($this->isInstalled()) {
44       // LocalLibraryInterface::getLocalPath() returns the path relative to the
45       // app root. In order for the core core asset system to register the path
46       // as relative to the app root, a leading slash is required.
47       /** @see \Drupal\Core\Asset\LibraryDiscoveryParser::buildByExtension() */
48       return '/' . $this->getLocalPath();
49     }
50     elseif ($this->hasRemoteUrl()) {
51       return $this->getRemoteUrl();
52     }
53     else {
54       // @todo Throw an exception.
55     }
56   }
57
58   /**
59    * Gets the CSS assets attached to this library.
60    *
61    * @param array $assets
62    *
63    * @return array
64    *   An array of CSS assets of the library following the core library CSS
65    *   structure. The keys of the array must be among the SMACSS categories
66    *   'base', 'layout, 'component', 'state', and 'theme'. The value of each
67    *   category is in turn an array where the keys are the file paths of the CSS
68    *   files and values are CSS options.
69    *
70    * @see https://smacss.com/
71    *
72    * @see \Drupal\libraries\ExternalLibrary\Asset\SingleAssetLibraryTrait::getCssAssets()
73    */
74   protected function processCssAssets(array $assets) {
75     // @todo Consider somehow caching the processed information.
76     $processed_assets = [];
77     foreach ($assets as $category => $category_assets) {
78       // @todo Somehow consolidate this with getJsAssets().
79       foreach ($category_assets as $filename => $options) {
80         $processed_assets[$category][$this->getPathPrefix() . '/' . $filename] = $options;
81       }
82     }
83     return $processed_assets;
84   }
85
86   /**
87    * Gets the JavaScript assets attached to this library.
88    *
89    * @param array $assets
90    *
91    * @return array
92    *   An array of JavaScript assets of the library. The keys of the array are
93    *   the file paths of the JavaScript files and the values are JavaScript
94    *   options.
95    *
96    * @see \Drupal\libraries\ExternalLibrary\Asset\SingleAssetLibraryTrait::getJsAssets()
97    */
98   protected function processJsAssets(array $assets) {
99     // @todo Consider somehow caching the processed information.
100     $processed_assets = [];
101     // @todo Somehow consolidate this with getCssAssets().
102     foreach ($assets as $filename => $options) {
103       $processed_assets[$this->getPathPrefix() . '/' . $filename] = $options;
104     }
105     return $processed_assets;
106   }
107
108 }