Further modules included.
[yaffs-website] / web / modules / contrib / libraries / src / ExternalLibrary / Asset / MultipleAssetLibrary.php
1 <?php
2
3 namespace Drupal\libraries\ExternalLibrary\Asset;
4
5 use Drupal\Component\Plugin\Factory\FactoryInterface;
6 use Drupal\libraries\ExternalLibrary\Dependency\DependentLibraryInterface;
7 use Drupal\libraries\ExternalLibrary\Exception\LibraryNotInstalledException;
8 use Drupal\libraries\ExternalLibrary\LibraryBase;
9 use Drupal\libraries\ExternalLibrary\LibraryManagerInterface;
10 use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface;
11 use Drupal\libraries\ExternalLibrary\Local\LocalLibraryTrait;
12 use Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface;
13 use Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryTrait;
14 use Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface;
15 use Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface;
16
17 /**
18  * Provides a class for a library with multiple attachable asset libraries.
19  */
20 class MultipleAssetLibrary extends LibraryBase implements
21   MultipleAssetLibraryInterface,
22   VersionedLibraryInterface,
23   DependentLibraryInterface,
24   LocalLibraryInterface,
25   RemoteLibraryInterface
26 {
27
28   use
29     LocalLibraryTrait,
30     RemoteLibraryTrait,
31     LocalRemoteAssetTrait
32   ;
33
34   /**
35    * An array of attachable asset libraries.
36    */
37   protected $libraries = [];
38
39   /**
40    * Construct an external library.
41    *
42    * @param string $id
43    *   The library ID.
44    * @param array $definition
45    *   The library definition array.
46    * @param \Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface $library_type
47    *   The library type of the library.
48    */
49   public function __construct($id, array $definition, LibraryTypeInterface $library_type) {
50     parent::__construct($id, $definition, $library_type);
51     $this->remoteUrl = $definition['remote_url'];
52     $this->libraries = $definition['libraries'];
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   protected static function processDefinition(array &$definition) {
59     parent::processDefinition($definition);
60     $definition += [
61       'remote_url' => '',
62       'libraries' => [],
63     ];
64     foreach ($definition['libraries'] as &$library) {
65       $library += [
66         'css' => [],
67         'js' => [],
68         'dependencies' => [],
69       ];
70     }
71   }
72
73   /**
74    * Returns a core library array structure for this library.
75    *
76    * @param \Drupal\libraries\ExternalLibrary\LibraryManagerInterface $library_manager
77    *   The library manager that can be used to fetch dependencies.
78    *
79    * @return array
80    *
81    * @see \Drupal\libraries\ExternalLibrary\Asset\getAttachableAssetLibraries::getAttachableAssetLibraries()
82    *
83    * @throws \Drupal\libraries\ExternalLibrary\Exception\InvalidLibraryDependencyException
84    * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException
85    * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryTypeNotFoundException
86    * @throws \Drupal\Component\Plugin\Exception\PluginException
87    *
88    * @todo Document the return value.
89    */
90   public function getAttachableAssetLibraries(LibraryManagerInterface $library_manager) {
91     if (!$this->canBeAttached()) {
92       throw new LibraryNotInstalledException($this);
93     }
94     $attachable_libraries = [];
95     foreach ($this->libraries as $attachable_library_id => $attachable_library) {
96       $attachable_libraries[$attachable_library_id] = [
97         'version' => $this->getVersion(),
98         'css' => $this->processCssAssets($attachable_library['css']),
99         'js' => $this->processJsAssets($attachable_library['js']),
100         'dependencies' => $attachable_library['dependencies'],
101       ];
102     }
103     return $attachable_libraries;
104   }
105
106   /**
107    * Gets the locator of this library using the locator factory.
108    *
109    * @param \Drupal\Component\Plugin\Factory\FactoryInterface $locator_factory
110    *
111    * @return \Drupal\libraries\ExternalLibrary\Local\LocatorInterface
112    *
113    * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::getLocator()
114    */
115   public function getLocator(FactoryInterface $locator_factory) {
116     // @todo Consider consolidating the stream wrappers used here. For now we
117     // allow asset libs to live almost anywhere.
118     return $locator_factory->createInstance('chain')
119       ->addLocator($locator_factory->createInstance('uri', ['uri' => 'asset://']))
120       ->addLocator($locator_factory->createInstance('uri', ['uri' => 'php-file://']));
121   }
122
123 }