17a62236e292b2ddf0b2a71e3914bf97c080bb9e
[yaffs-website] / web / modules / contrib / libraries / src / ExternalLibrary / LibraryManager.php
1 <?php
2
3 namespace Drupal\libraries\ExternalLibrary;
4
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;
10
11 /**
12  * Provides a manager for external libraries.
13  *
14  * @todo Dispatch events at various points in the library lifecycle.
15  * @todo Automatically load PHP file libraries that are required by modules or
16  *   themes.
17  */
18 class LibraryManager implements LibraryManagerInterface {
19
20   /**
21    * The library definition discovery.
22    *
23    * @var \Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface
24    */
25   protected $definitionDiscovery;
26
27   /**
28    * The library type factory.
29    *
30    * @var \Drupal\Component\Plugin\Factory\FactoryInterface
31    */
32   protected $libraryTypeFactory;
33
34   /**
35    * Constructs an external library manager.
36    *
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.
41    */
42   public function __construct(
43     DefinitionDiscoveryInterface $definition_disovery,
44     FactoryInterface $library_type_factory
45   ) {
46     $this->definitionDiscovery = $definition_disovery;
47     $this->libraryTypeFactory = $library_type_factory;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function getLibrary($id) {
54     $definition = $this->definitionDiscovery->getDefinition($id);
55     return $this->getLibraryFromDefinition($id, $definition);
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function getRequiredLibraryIds() {
62     $library_ids = [];
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']);
67         }
68       }
69     }
70     return array_unique($library_ids);
71   }
72
73   /**
74    * {@inheritdoc}
75    */
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));
82     }
83   }
84
85   /**
86    * @param $id
87    * @param $definition
88    * @return mixed
89    */
90   protected function getLibraryFromDefinition($id, $definition) {
91     $library_type = $this->getLibraryType($id, $definition);
92
93     // @todo Make this alter-able.
94     $class = $library_type->getLibraryClass();
95
96     // @todo Make sure that the library class implements the correct interface.
97     $library = $class::create($id, $definition, $library_type);
98
99     if ($library_type instanceof LibraryCreationListenerInterface) {
100       $library_type->onLibraryCreate($library);
101       return $library;
102     }
103     return $library;
104   }
105
106   /**
107    * @param string $id
108    * @param array $definition
109    *
110    * @return \Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface
111    */
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);
116     }
117     return $this->libraryTypeFactory->createInstance($definition['type']);
118   }
119
120 }