diff --git a/core/lib/Drupal/Core/Asset/Exception/IncompleteLibraryDefinitionException.php b/core/lib/Drupal/Core/Asset/Exception/IncompleteLibraryDefinitionException.php index 46a9d9e..2ef4c98 100644 --- a/core/lib/Drupal/Core/Asset/Exception/IncompleteLibraryDefinitionException.php +++ b/core/lib/Drupal/Core/Asset/Exception/IncompleteLibraryDefinitionException.php @@ -7,6 +7,9 @@ namespace Drupal\Core\Asset\Exception; -class IncompleteLibraryDefinitionException { +/** + * Defines a custom exception if a library has no js/css/setting specified. + */ +class IncompleteLibraryDefinitionException extends \RuntimeException { } diff --git a/core/lib/Drupal/Core/Asset/Exception/InvalidLibraryFileException.php b/core/lib/Drupal/Core/Asset/Exception/InvalidLibraryFileException.php index e22f361..33cf830 100644 --- a/core/lib/Drupal/Core/Asset/Exception/InvalidLibraryFileException.php +++ b/core/lib/Drupal/Core/Asset/Exception/InvalidLibraryFileException.php @@ -7,6 +7,9 @@ namespace Drupal\Core\Asset\Exception; -class InvalidLibraryFileException { +/** + * Defines an exception if the library file could not be parsed. + */ +class InvalidLibraryFileException extends \RunTimeException { } diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscovery.php b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php index d53f630..b68f348 100644 --- a/core/lib/Drupal/Core/Asset/LibraryDiscovery.php +++ b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Asset; +use Drupal\Core\Asset\Exception\IncompleteLibraryDefinitionException; +use Drupal\Core\Asset\Exception\InvalidLibraryFileException; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -72,6 +74,12 @@ public function getLibraryByName($extension, $name) { return isset($this->libraries[$extension][$name]) ? $this->libraries[$extension][$name] : FALSE; } + /** + * Ensures that the libraries property is filled. + * + * @param string $extension + * The name of the extension that registered a library. + */ protected function ensureLibraryInformation($extension) { $this->retrieveFromCache($extension); if (!isset($this->libraries[$extension])) { @@ -84,6 +92,12 @@ protected function ensureLibraryInformation($extension) { } } + /** + * Fills up the libraries property from cache, if available. + * + * @param string $extension + * The name of the extension that registered a library. + */ protected function retrieveFromCache($extension) { if (!isset($this->libraries[$extension])) { if ($cache = $this->cache->get('library:info:' . $extension)) { @@ -92,6 +106,15 @@ protected function retrieveFromCache($extension) { } } + /** + * Sets the library information into a cache entry. + * + * @param string $extension + * The name of the extension that registered a library. + * + * @param array $information + * All library definitions of the passed extension. + */ protected function setCache($extension, array $information) { $this->cache->set('library:info:' . $extension, $information, Cache::PERMANENT, array( 'extension' => array(TRUE, $extension), @@ -99,6 +122,20 @@ protected function setCache($extension, array $information) { )); } + /** + * Parses and builds up all the libraries information of an extension. + * + * @param string $extension + * The name of the extension that registered a library. + * + * @return array + * All library definitions of the passed extension. + * + * @throws \Drupal\Core\Asset\Exception\IncompleteLibraryDefinitionException + * Thrown when a library has no js/css/setting. + * @throws \UnexpectedValueException + * Thrown when a js file defines a positive weight. + */ protected function buildLibrariesByExtension($extension) { $this->libraries[$extension] = array(); if ($extension === 'core') { @@ -123,7 +160,7 @@ protected function buildLibrariesByExtension($extension) { foreach ($this->libraries[$extension] as $id => &$library) { if (!isset($library['js']) && !isset($library['css']) && !isset($library['settings'])) { - throw new \RuntimeException(sprintf("Incomplete library definition for '%s' in %s", $id, $library_file)); + throw new IncompleteLibraryDefinitionException(sprintf("Incomplete library definition for '%s' in %s", $id, $library_file)); } $library += array('dependencies' => array(), 'js' => array(), 'css' => array()); @@ -244,18 +281,24 @@ protected function drupalGetPath($type, $name) { } /** - * @param $source - * @return bool + * Wraps file_valid_uri(). */ protected function fileValidUri($source) { return file_valid_uri($source); } /** - * @param $extension - * @param $library_file - * @return mixed - * @throws \RuntimeException + * Parses a given library file and allows module to alter it. + * + * This method sets the parsed information onto the library property. + * + * @param string $extension + * The name of the extension that registered a library. + * @param string $library_file + * The relative filename to the DRUPAL_ROOT of the wanted library file. + * + * @throws \Drupal\Core\Asset\Exception\InvalidLibraryFileException + * Thrown when a parser exception got thrown. */ protected function parseLibraryInfo($extension, $library_file) { $parser = new Parser(); @@ -264,7 +307,7 @@ protected function parseLibraryInfo($extension, $library_file) { } catch (ParseException $e) { // Rethrow a more helpful exception, since ParseException lacks context. - throw new \RuntimeException(sprintf('Invalid library definition in %s: %s', $library_file, $e->getMessage()), 0, $e); + throw new InvalidLibraryFileException(sprintf('Invalid library definition in %s: %s', $library_file, $e->getMessage()), 0, $e); } // Allow modules to alter the module's registered libraries. $this->moduleHandler->alter('library_info', $this->libraries[$extension], $extension); diff --git a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php index cc67cfc..3dca871 100644 --- a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php @@ -169,7 +169,7 @@ public function testGetLibraryWithMissingLibraryFile() { /** * Tests that an exception is thrown when a libraries file couldn't be parsed. * - * @expectedException \RuntimeException + * @expectedException \Drupal\Core\Asset\Exception\InvalidLibraryFileException */ public function testInvalidLibrariesFile() { $this->moduleHandler->expects($this->atLeastOnce()) @@ -187,7 +187,7 @@ public function testInvalidLibrariesFile() { /** * Tests that an exception is thrown when no CSS/JS/setting is specified. * - * @expectedException \RuntimeException + * @expectedException \Drupal\Core\Asset\Exception\IncompleteLibraryDefinitionException * @expectedExceptionMessage Incomplete library definition for 'example' in core/tests/Drupal/Tests/Core/Asset/library_test_files/example_module_missing_information.libraries.yml */ public function testGetLibraryWithMissingInformation() { @@ -383,6 +383,9 @@ public function testExternalCache() { } +/** + * Wraps the tested class to mock the external dependencies. + */ class TestLibraryDiscovery extends LibraryDiscovery { protected $paths; diff --git a/core/tests/Drupal/Tests/Core/Asset/library_test_files/invalid_file.libraries.yml b/core/tests/Drupal/Tests/Core/Asset/library_test_files/invalid_file.libraries.yml index cb20993..b3cebbb 100644 --- a/core/tests/Drupal/Tests/Core/Asset/library_test_files/invalid_file.libraries.yml +++ b/core/tests/Drupal/Tests/Core/Asset/library_test_files/invalid_file.libraries.yml @@ -1,2 +1,2 @@ -example: + example: -=--##;'~ test