3 namespace Drupal\libraries\ExternalLibrary\Asset;
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;
16 * Provides a class for a single attachable asset library.
18 class AssetLibrary extends LibraryBase implements
19 AssetLibraryInterface,
20 LocalLibraryInterface,
21 RemoteLibraryInterface
31 * An array containing the CSS assets of the library.
35 protected $cssAssets = [];
38 * An array containing the JavaScript assets of the library.
42 protected $jsAssets = [];
45 * An array of attachable asset library IDs that this library depends on.
47 * @todo Explain the difference to regular dependencies.
49 protected $attachableDependencies = [];
52 * Construct an external library.
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.
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'];
72 protected static function processDefinition(array &$definition) {
73 parent::processDefinition($definition);
78 'attachable_dependencies' => [],
83 * Returns a core library array structure for this library.
85 * @param \Drupal\libraries\ExternalLibrary\LibraryManagerInterface $library_manager
86 * The library manager that can be used to fetch dependencies.
90 * @see \Drupal\libraries\ExternalLibrary\Asset\getAttachableAssetLibraries::getAttachableAssetLibraries()
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
97 * @todo Document the return value.
99 public function getAttachableAssetLibrary(LibraryManagerInterface $library_manager) {
100 if (!$this->canBeAttached()) {
101 throw new LibraryNotInstalledException($this);
104 'version' => $this->getVersion(),
105 'css' => $this->processCssAssets($this->cssAssets),
106 'js' => $this->processJsAssets($this->jsAssets),
107 'dependencies' => $this->attachableDependencies,
112 * Gets the locator of this library using the locator factory.
114 * @param \Drupal\Component\Plugin\Factory\FactoryInterface $locator_factory
116 * @return \Drupal\libraries\ExternalLibrary\Local\LocatorInterface
118 * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::getLocator()
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://']));