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