Skip to content
Snippets Groups Projects
Commit 73f8b99e authored by Ryan Jacobs's avatar Ryan Jacobs
Browse files

Issue #2774313 by rjacobs, tstoeckler: More gracefully deal with...

Issue #2774313 by rjacobs, tstoeckler: More gracefully deal with library-specific errors at runtime.

Squashed commit of the following:

commit cbd3b2f37baebd02b33648e92c31e59376d80e22
Author: Ryan Jacobs <rjacobs@422459.no-reply.drupal.org>
Date:   Thu Feb 2 21:47:51 2017 -0600

    Issue #2774313: Add exception handling to libraries_library_info_build().

commit 6f4f6a5f85d0bee12d474ab147fc2c9d7d1e49a1
Author: Ryan Jacobs <rjacobs@422459.no-reply.drupal.org>
Date:   Mon Jan 30 22:30:53 2017 -0600

    Issue #2774313: Skip local version detection for uninstalled local library.
parent 769adedd
No related branches found
No related tags found
No related merge requests found
Showing with 79 additions and 14 deletions
......@@ -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;
}
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
......@@ -26,7 +26,7 @@ trait RemoteLibraryTrait {
* @see \Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface::hasRemoteUrl()
*/
public function hasRemoteUrl() {
return isset($this->remoteUrl);
return !empty($this->remoteUrl);
}
/**
......
......@@ -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);
}
}
}
......
<?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();
}
<?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();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment