LibraryManager.php in Libraries API 8.3
File
src/ExternalLibrary/LibraryManager.php
View source
<?php
namespace Drupal\libraries\ExternalLibrary;
use Drupal\Component\Plugin\Factory\FactoryInterface;
use Drupal\libraries\ExternalLibrary\Exception\LibraryTypeNotFoundException;
use Drupal\libraries\ExternalLibrary\Type\LibraryCreationListenerInterface;
use Drupal\libraries\ExternalLibrary\Type\LibraryLoadingListenerInterface;
use Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface;
class LibraryManager implements LibraryManagerInterface {
protected $definitionDiscovery;
protected $libraryTypeFactory;
public function __construct(DefinitionDiscoveryInterface $definition_disovery, FactoryInterface $library_type_factory) {
$this->definitionDiscovery = $definition_disovery;
$this->libraryTypeFactory = $library_type_factory;
}
public function getLibrary($id) {
$definition = $this->definitionDiscovery
->getDefinition($id);
return $this
->getLibraryFromDefinition($id, $definition);
}
public function getRequiredLibraryIds() {
$library_ids = [];
foreach ([
'module',
'theme',
] as $type) {
$service_id = 'extension.list.' . $type;
$extension_list = \Drupal::service($service_id);
foreach ($extension_list
->getAllInstalledInfo() as $info) {
if (isset($info['library_dependencies'])) {
$library_ids = array_merge($library_ids, $info['library_dependencies']);
}
}
}
return array_unique($library_ids);
}
public function load($id) {
$definition = $this->definitionDiscovery
->getDefinition($id);
$library_type = $this
->getLibraryType($id, $definition);
if ($library_type instanceof LibraryLoadingListenerInterface) {
$library_type
->onLibraryLoad($this
->getLibraryFromDefinition($id, $definition));
}
}
protected function getLibraryFromDefinition($id, $definition) {
$library_type = $this
->getLibraryType($id, $definition);
$class = $library_type
->getLibraryClass();
$library = $class::create($id, $definition, $library_type);
if ($library_type instanceof LibraryCreationListenerInterface) {
$library_type
->onLibraryCreate($library);
return $library;
}
return $library;
}
protected function getLibraryType($id, $definition) {
if (!isset($definition['type'])) {
throw new LibraryTypeNotFoundException($id);
}
return $this->libraryTypeFactory
->createInstance($definition['type']);
}
}