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']); } }