3 namespace Drupal\libraries\ExternalLibrary;
5 use Drupal\Component\Plugin\Factory\FactoryInterface;
6 use Drupal\libraries\ExternalLibrary\Exception\LibraryTypeNotFoundException;
7 use Drupal\libraries\ExternalLibrary\Type\LibraryCreationListenerInterface;
8 use Drupal\libraries\ExternalLibrary\Type\LibraryLoadingListenerInterface;
9 use Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface;
12 * Provides a manager for external libraries.
14 * @todo Dispatch events at various points in the library lifecycle.
15 * @todo Automatically load PHP file libraries that are required by modules or
18 class LibraryManager implements LibraryManagerInterface {
21 * The library definition discovery.
23 * @var \Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface
25 protected $definitionDiscovery;
28 * The library type factory.
30 * @var \Drupal\Component\Plugin\Factory\FactoryInterface
32 protected $libraryTypeFactory;
35 * Constructs an external library manager.
37 * @param \Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface $definition_disovery
38 * The library registry.
39 * @param \Drupal\Component\Plugin\Factory\FactoryInterface $library_type_factory
40 * The library type factory.
42 public function __construct(
43 DefinitionDiscoveryInterface $definition_disovery,
44 FactoryInterface $library_type_factory
46 $this->definitionDiscovery = $definition_disovery;
47 $this->libraryTypeFactory = $library_type_factory;
53 public function getLibrary($id) {
54 $definition = $this->definitionDiscovery->getDefinition($id);
55 return $this->getLibraryFromDefinition($id, $definition);
61 public function getRequiredLibraryIds() {
63 foreach (['module', 'theme'] as $type) {
64 foreach (system_get_info($type) as $info) {
65 if (isset($info['library_dependencies'])) {
66 $library_ids = array_merge($library_ids, $info['library_dependencies']);
70 return array_unique($library_ids);
76 public function load($id) {
77 $definition = $this->definitionDiscovery->getDefinition($id);
78 $library_type = $this->getLibraryType($id, $definition);
79 // @todo Throw an exception instead of silently failing.
80 if ($library_type instanceof LibraryLoadingListenerInterface) {
81 $library_type->onLibraryLoad($this->getLibraryFromDefinition($id, $definition));
90 protected function getLibraryFromDefinition($id, $definition) {
91 $library_type = $this->getLibraryType($id, $definition);
93 // @todo Make this alter-able.
94 $class = $library_type->getLibraryClass();
96 // @todo Make sure that the library class implements the correct interface.
97 $library = $class::create($id, $definition, $library_type);
99 if ($library_type instanceof LibraryCreationListenerInterface) {
100 $library_type->onLibraryCreate($library);
108 * @param array $definition
110 * @return \Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface
112 protected function getLibraryType($id, $definition) {
113 // @todo Validate that the type is a string.
114 if (!isset($definition['type'])) {
115 throw new LibraryTypeNotFoundException($id);
117 return $this->libraryTypeFactory->createInstance($definition['type']);