diff --git a/libraries.module b/libraries.module index 709e562c9af5bf487ff3dcc7f3b4b5c49ad76223..3006c345ceb1716bce03c98045f108430fb6a39f 100644 --- a/libraries.module +++ b/libraries.module @@ -8,23 +8,46 @@ use Drupal\Core\DrupalKernel; use Drupal\Core\Extension\ModuleHandler; use Drupal\libraries\ExternalLibrary\Asset\AttachableAssetLibraryRegistrationInterface; +use Drupal\libraries\ExternalLibrary\Utility\LibraryAccessorInterface; +use Drupal\libraries\ExternalLibrary\Utility\LibraryIdAccessorInterface; use Symfony\Component\Yaml\Parser; /** * Implements hook_library_info_build(). + * + * Register external asset libraries with Drupal core's library APIs. */ function libraries_library_info_build() { /** @var \Drupal\libraries\ExternalLibrary\LibraryManagerInterface $library_manager */ $library_manager = \Drupal::service('libraries.manager'); - $attachable_libraries = []; + $libraries_with_errors = []; foreach ($library_manager->getRequiredLibraryIds() as $external_library_id) { - $external_library = $library_manager->getLibrary($external_library_id); - $library_type = $external_library->getType(); - if ($library_type instanceof AttachableAssetLibraryRegistrationInterface) { - $attachable_libraries += $library_type->getAttachableAssetLibraries($external_library, $library_manager); + try { + $external_library = $library_manager->getLibrary($external_library_id); + $library_type = $external_library->getType(); + if ($library_type instanceof AttachableAssetLibraryRegistrationInterface) { + $attachable_libraries += $library_type->getAttachableAssetLibraries($external_library, $library_manager); + } + } + catch (\Exception $e) { + // Library-specific exceptions should not be allowed to kill the rest of + // the build process, but should be logged. + if ($e instanceof LibraryIdAccessorInterface || $e instanceof LibraryAccessorInterface) { + $libraries_with_errors[] = $external_library_id; + watchdog_exception('libraries', $e); + } + else { + // Re-throw exceptions that are not library-specific. + throw $e; + } } } + // If we had library specific errors also log an informative message to + // tell admins that detection will not be run again without a cache clear. + if ($libraries_with_errors) { + \Drupal::logger('libraries')->error('The following external libraries could not successfully be registered with Drupal core: @libs. See earlier log entries for more details. Once these issues are addressed please be sure to clear your Drupal library cache to ensure external library detection is run again.', ['@libs' => implode(',', $libraries_with_errors)]); + } return $attachable_libraries; } diff --git a/src/ExternalLibrary/Exception/InvalidLibraryDependencyException.php b/src/ExternalLibrary/Exception/InvalidLibraryDependencyException.php index 9b741e16ffb80bf2dfc52d2c7d4ccc8d7ab12080..7ab3eeb7e4ce73dd3b3bbef97f2571ab13b214e0 100644 --- a/src/ExternalLibrary/Exception/InvalidLibraryDependencyException.php +++ b/src/ExternalLibrary/Exception/InvalidLibraryDependencyException.php @@ -5,11 +5,12 @@ namespace Drupal\libraries\ExternalLibrary\Exception; use Drupal\libraries\ExternalLibrary\Utility\DependencyAccessorTrait; use Drupal\libraries\ExternalLibrary\LibraryInterface; use Drupal\libraries\ExternalLibrary\Utility\LibraryAccessorTrait; +use Drupal\libraries\ExternalLibrary\Utility\LibraryAccessorInterface; /** * Provides an exception for an invalid library exception. */ -class InvalidLibraryDependencyException extends \UnexpectedValueException { +class InvalidLibraryDependencyException extends \UnexpectedValueException implements LibraryAccessorInterface { use LibraryAccessorTrait; use DependencyAccessorTrait; diff --git a/src/ExternalLibrary/Exception/LibraryDefinitionNotFoundException.php b/src/ExternalLibrary/Exception/LibraryDefinitionNotFoundException.php index 0e5e3386d594b1a0c63d2d436c3779aa4e5ecf06..c274302d699892a8b2950f7964b3f90af253f571 100644 --- a/src/ExternalLibrary/Exception/LibraryDefinitionNotFoundException.php +++ b/src/ExternalLibrary/Exception/LibraryDefinitionNotFoundException.php @@ -3,11 +3,12 @@ namespace Drupal\libraries\ExternalLibrary\Exception; use Drupal\libraries\ExternalLibrary\Utility\LibraryIdAccessorTrait; +use Drupal\libraries\ExternalLibrary\Utility\LibraryIdAccessorInterface; /** * Provides an exception for a library definition that cannot be found. */ -class LibraryDefinitionNotFoundException extends \RuntimeException { +class LibraryDefinitionNotFoundException extends \RuntimeException implements LibraryIdAccessorInterface { use LibraryIdAccessorTrait; diff --git a/src/ExternalLibrary/Exception/LibraryNotInstalledException.php b/src/ExternalLibrary/Exception/LibraryNotInstalledException.php index c65821f814cd8b850175d8469c452e0ddd6a994f..ed48a629c458165cd5a30e0584e6cc70e92a9a2d 100644 --- a/src/ExternalLibrary/Exception/LibraryNotInstalledException.php +++ b/src/ExternalLibrary/Exception/LibraryNotInstalledException.php @@ -4,11 +4,12 @@ namespace Drupal\libraries\ExternalLibrary\Exception; use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface; use Drupal\libraries\ExternalLibrary\Utility\LibraryAccessorTrait; +use Drupal\libraries\ExternalLibrary\Utility\LibraryAccessorInterface; /** * Provides an exception for a library that is not installed. */ -class LibraryNotInstalledException extends \RuntimeException { +class LibraryNotInstalledException extends \RuntimeException implements LibraryAccessorInterface { use LibraryAccessorTrait; diff --git a/src/ExternalLibrary/Exception/LibraryTypeNotFoundException.php b/src/ExternalLibrary/Exception/LibraryTypeNotFoundException.php index 7531bf39fde23bbf25e26c0701c59f5fdff9b348..b5655bba09a626a1d7b73a60b2971a1fc19c5a83 100644 --- a/src/ExternalLibrary/Exception/LibraryTypeNotFoundException.php +++ b/src/ExternalLibrary/Exception/LibraryTypeNotFoundException.php @@ -3,11 +3,12 @@ namespace Drupal\libraries\ExternalLibrary\Exception; use Drupal\libraries\ExternalLibrary\Utility\LibraryIdAccessorTrait; +use Drupal\libraries\ExternalLibrary\Utility\LibraryIdAccessorInterface; /** * Provides an exception for a library definition without a type declaration. */ -class LibraryTypeNotFoundException extends \RuntimeException { +class LibraryTypeNotFoundException extends \RuntimeException implements LibraryAccessorInterface { use LibraryIdAccessorTrait; diff --git a/src/ExternalLibrary/Exception/UnknownLibraryVersionException.php b/src/ExternalLibrary/Exception/UnknownLibraryVersionException.php index 72bd153b1e0165563c1a7cf6ab76141e02282938..4afe0593feb4f48770fdbd1651164bf337f5af8b 100644 --- a/src/ExternalLibrary/Exception/UnknownLibraryVersionException.php +++ b/src/ExternalLibrary/Exception/UnknownLibraryVersionException.php @@ -3,12 +3,13 @@ namespace Drupal\libraries\ExternalLibrary\Exception; use Drupal\libraries\ExternalLibrary\Utility\LibraryAccessorTrait; +use Drupal\libraries\ExternalLibrary\Utility\LibraryAccessorInterface; use Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface; /** * Provides an exception for libraries whose version has not been detected. */ -class UnknownLibraryVersionException extends \RuntimeException { +class UnknownLibraryVersionException extends \RuntimeException implements LibraryAccessorInterface { use LibraryAccessorTrait; diff --git a/src/ExternalLibrary/Remote/RemoteLibraryTrait.php b/src/ExternalLibrary/Remote/RemoteLibraryTrait.php index 0fb82c0f87eb3f237b0132f70d39d3304c2523c7..d55f3c23891f4f5cb6922889bb5511ef21d4abba 100644 --- a/src/ExternalLibrary/Remote/RemoteLibraryTrait.php +++ b/src/ExternalLibrary/Remote/RemoteLibraryTrait.php @@ -26,7 +26,7 @@ trait RemoteLibraryTrait { * @see \Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface::hasRemoteUrl() */ public function hasRemoteUrl() { - return isset($this->remoteUrl); + return !empty($this->remoteUrl); } /** diff --git a/src/ExternalLibrary/Type/LibraryTypeBase.php b/src/ExternalLibrary/Type/LibraryTypeBase.php index 08cd61a80b2ee4b84cb1c664e66f7d5b18d1a112..9774e978e09fd559b0b05be69bc61c870a9d2204 100644 --- a/src/ExternalLibrary/Type/LibraryTypeBase.php +++ b/src/ExternalLibrary/Type/LibraryTypeBase.php @@ -74,9 +74,10 @@ abstract class LibraryTypeBase implements if (!$library->isInstalled()) { $this->locatorFactory->createInstance('global')->locate($library); } - } - if ($library instanceof VersionedLibraryInterface) { - $library->getVersionDetector($this->detectorFactory)->detectVersion($library); + // Also fetch version information. + if ($library->isInstalled() && $library instanceof VersionedLibraryInterface) { + $library->getVersionDetector($this->detectorFactory)->detectVersion($library); + } } } diff --git a/src/ExternalLibrary/Utility/LibraryAccessorInterface.php b/src/ExternalLibrary/Utility/LibraryAccessorInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..72b0093c4dcb732ae8f4d21aa03a320408cd9e22 --- /dev/null +++ b/src/ExternalLibrary/Utility/LibraryAccessorInterface.php @@ -0,0 +1,18 @@ +<?php + +namespace Drupal\libraries\ExternalLibrary\Utility; + +/** + * Provides an interface for classes giving access to a library. + */ +interface LibraryAccessorInterface { + + /** + * Returns the library. + * + * @return \Drupal\libraries\ExternalLibrary\LibraryInterface + * The library. + */ + public function getLibrary(); + +} diff --git a/src/ExternalLibrary/Utility/LibraryIdAccessorInterface.php b/src/ExternalLibrary/Utility/LibraryIdAccessorInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..98928e9deccd67a28b7734199456885bf43087c0 --- /dev/null +++ b/src/ExternalLibrary/Utility/LibraryIdAccessorInterface.php @@ -0,0 +1,18 @@ +<?php + +namespace Drupal\libraries\ExternalLibrary\Utility; + +/** + * Provides an interface for classes giving access to a library ID. + */ +interface LibraryAccessorIdInterface { + + /** + * Returns the ID of the library. + * + * @return string + * The library ID. + */ + public function getLibraryId(); + +}