X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Flibraries%2Fsrc%2FExternalLibrary%2FLibraryManager.php;fp=web%2Fmodules%2Fcontrib%2Flibraries%2Fsrc%2FExternalLibrary%2FLibraryManager.php;h=17a62236e292b2ddf0b2a71e3914bf97c080bb9e;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/libraries/src/ExternalLibrary/LibraryManager.php b/web/modules/contrib/libraries/src/ExternalLibrary/LibraryManager.php new file mode 100644 index 000000000..17a62236e --- /dev/null +++ b/web/modules/contrib/libraries/src/ExternalLibrary/LibraryManager.php @@ -0,0 +1,120 @@ +definitionDiscovery = $definition_disovery; + $this->libraryTypeFactory = $library_type_factory; + } + + /** + * {@inheritdoc} + */ + public function getLibrary($id) { + $definition = $this->definitionDiscovery->getDefinition($id); + return $this->getLibraryFromDefinition($id, $definition); + } + + /** + * {@inheritdoc} + */ + public function getRequiredLibraryIds() { + $library_ids = []; + foreach (['module', 'theme'] as $type) { + foreach (system_get_info($type) as $info) { + if (isset($info['library_dependencies'])) { + $library_ids = array_merge($library_ids, $info['library_dependencies']); + } + } + } + return array_unique($library_ids); + } + + /** + * {@inheritdoc} + */ + public function load($id) { + $definition = $this->definitionDiscovery->getDefinition($id); + $library_type = $this->getLibraryType($id, $definition); + // @todo Throw an exception instead of silently failing. + if ($library_type instanceof LibraryLoadingListenerInterface) { + $library_type->onLibraryLoad($this->getLibraryFromDefinition($id, $definition)); + } + } + + /** + * @param $id + * @param $definition + * @return mixed + */ + protected function getLibraryFromDefinition($id, $definition) { + $library_type = $this->getLibraryType($id, $definition); + + // @todo Make this alter-able. + $class = $library_type->getLibraryClass(); + + // @todo Make sure that the library class implements the correct interface. + $library = $class::create($id, $definition, $library_type); + + if ($library_type instanceof LibraryCreationListenerInterface) { + $library_type->onLibraryCreate($library); + return $library; + } + return $library; + } + + /** + * @param string $id + * @param array $definition + * + * @return \Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface + */ + protected function getLibraryType($id, $definition) { + // @todo Validate that the type is a string. + if (!isset($definition['type'])) { + throw new LibraryTypeNotFoundException($id); + } + return $this->libraryTypeFactory->createInstance($definition['type']); + } + +}