diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscovery.php b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php index c1266d7..9b5c6e8 100644 --- a/core/lib/Drupal/Core/Asset/LibraryDiscovery.php +++ b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Asset; -use Drupal\Core\Asset\Exception\InvalidLibrariesOverrideSpecificationException; use Drupal\Core\Cache\CacheCollectorInterface; use Drupal\Core\Cache\CacheTagsInvalidatorInterface; @@ -61,14 +60,23 @@ public function __construct(CacheCollectorInterface $library_discovery_collector * {@inheritdoc} */ public function getLibrariesByExtension($extension) { - return $this->getLibraryDefinitions($extension); + if (!isset($this->libraryDefinitions[$extension])) { + $libraries = $this->collector->get($extension); + $this->libraryDefinitions[$extension] = []; + foreach ($libraries as $name => $definition) { + $library_name = "$extension/$name"; + $this->libraryDefinitions[$extension][$name] = $definition; + } + } + + return $this->libraryDefinitions[$extension]; } /** * {@inheritdoc} */ public function getLibraryByName($extension, $name) { - $extension = $this->getLibraryDefinitions($extension); + $extension = $this->getLibrariesByExtension($extension); return isset($extension[$name]) ? $extension[$name] : FALSE; } @@ -81,47 +89,4 @@ public function clearCachedDefinitions() { $this->collector->clear(); } - /** - * Returns the library definitions for a given extension. - * - * This also implements libraries-overrides for entire libraries that have - * been specified by the LibraryDiscoveryParser. - * - * @param string $extension - * The name of the extension for which library definitions will be returned. - * - * @return array - * The library definitions for $extension will overrides applied. - */ - protected function getLibraryDefinitions($extension) { - if (!isset($this->libraryDefinitions[$extension])) { - $libraries = $this->collector->get($extension); - $this->libraryDefinitions[$extension] = []; - foreach ($libraries as $name => $definition) { - // Handle libraries that are marked for override or removal. - // @see \Drupal\Core\Asset\LibraryDiscoveryParser::applyLibrariesOverride() - if (isset($definition['override'])) { - if ($definition['override'] === FALSE) { - // Remove the library definition if FALSE is given. - unset($libraries[$name]); - } - else { - // Otherwise replace with existing library definition if it exists. - // Throw an exception if it doesn't. - list($replacement_extension, $replacement_name) = explode('/', $definition['override']); - $replacement_definition = $this->getLibraryDefinitions($replacement_extension); - if (isset($replacement_definition[$replacement_name])) { - $libraries[$name] = $replacement_definition[$replacement_name]; - } - else { - throw new InvalidLibrariesOverrideSpecificationException(sprintf('The specified library %s does not exist.', $definition['override'])); - } - } - } - } - $this->libraryDefinitions[$extension] = $libraries; - } - return $this->libraryDefinitions[$extension]; - } - } diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscoveryCollector.php b/core/lib/Drupal/Core/Asset/LibraryDiscoveryCollector.php index 7ab83d5..016a498 100644 --- a/core/lib/Drupal/Core/Asset/LibraryDiscoveryCollector.php +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryCollector.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Asset; +use Drupal\Core\Asset\Exception\InvalidLibrariesOverrideSpecificationException; use Drupal\Core\Cache\CacheCollector; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Lock\LockBackendInterface; @@ -79,9 +80,52 @@ protected function getCid() { * {@inheritdoc} */ protected function resolveCacheMiss($key) { - $this->storage[$key] = $this->discoveryParser->buildByExtension($key); + $this->storage[$key] = $this->getLibraryDefinitions($key); $this->persist($key); return $this->storage[$key]; } + + + /** + * Returns the library definitions for a given extension. + * + * This also implements libraries-overrides for entire libraries that have + * been specified by the LibraryDiscoveryParser. + * + * @param string $extension + * The name of the extension for which library definitions will be returned. + * + * @return array + * The library definitions for $extension will overrides applied. + * + * @throws \Drupal\Core\Asset\Exception\InvalidLibrariesOverrideSpecificationException + */ + protected function getLibraryDefinitions($extension) { + $libraries = $this->discoveryParser->buildByExtension($extension); + foreach ($libraries as $name => $definition) { + // Handle libraries that are marked for override or removal. + // @see \Drupal\Core\Asset\LibraryDiscoveryParser::applyLibrariesOverride() + if (isset($definition['override'])) { + if ($definition['override'] === FALSE) { + // Remove the library definition if FALSE is given. + unset($libraries[$name]); + } + else { + // Otherwise replace with existing library definition if it exists. + // Throw an exception if it doesn't. + list($replacement_extension, $replacement_name) = explode('/', $definition['override']); + $replacement_definition = $this->get($replacement_extension); + if (isset($replacement_definition[$replacement_name])) { + $libraries[$name] = $replacement_definition[$replacement_name]; + } + else { + throw new InvalidLibrariesOverrideSpecificationException(sprintf('The specified library %s does not exist.', $definition['override'])); + } + } + } + } + return $libraries; + } + } diff --git a/core/modules/system/src/Tests/Asset/LibraryDiscoveryIntegrationTest.php b/core/modules/system/src/Tests/Asset/LibraryDiscoveryIntegrationTest.php index 07a2ecb..59e3532 100644 --- a/core/modules/system/src/Tests/Asset/LibraryDiscoveryIntegrationTest.php +++ b/core/modules/system/src/Tests/Asset/LibraryDiscoveryIntegrationTest.php @@ -11,7 +11,7 @@ use Drupal\simpletest\KernelTestBase; /** - * Tests the element info. + * Tests the library discovery and library discovery parser. * * @group Render */ @@ -130,20 +130,20 @@ public function testLibrariesOverrideMalformedAsset() { * Tests library assets with other ways for specifying paths. */ public function testLibrariesOverrideOtherAssetLibraryNames() { - // Activate test theme that defines libraries overrides on other types of + // Activate a test theme that defines libraries overrides on other types of // assets. $this->activateTheme('test_theme'); - // Assert Drupal relative paths. + // Assert Drupal-relative paths. $this->assertAssetInLibrary('themes/my_theme/css/dropbutton.css', 'core', 'drupal.dropbutton', 'css'); // Assert stream wrapper paths. $this->assertAssetInLibrary('public://my_css/vertical-tabs.css', 'core', 'drupal.vertical-tabs', 'css'); - // Assert protocol-relative URI. + // Assert a protocol-relative URI. $this->assertAssetInLibrary('//my-server/my_theme/css/jquery_ui.css', 'core', 'jquery.ui', 'css'); - // Assert regular URI. + // Assert an absolute URI. $this->assertAssetInLibrary('http://example.com/my_theme/css/farbtastic.css', 'core', 'jquery.farbtastic', 'css'); } @@ -151,11 +151,11 @@ public function testLibrariesOverrideOtherAssetLibraryNames() { * Tests that base theme libraries-override still apply in sub themes. */ public function testBaseThemeLibrariesOverrideInSubTheme() { - // Activate test theme that has subthemes. + // Activate a test theme that has subthemes. $this->activateTheme('test_subtheme'); // Assert that libraries-override specified in the base theme still applies - // in sub theme. + // in the sub theme. $this->assertNoAssetInLibrary('core/misc/dialog/dialog.js', 'core', 'drupal.dialog', 'js'); $this->assertAssetInLibrary('core/modules/system/tests/themes/test_basetheme/css/farbtastic.css', 'core', 'jquery.farbtastic', 'css'); } @@ -188,11 +188,11 @@ protected function activateTheme($theme_name) { * @param string $asset * The asset file with the path for the file. * @param string $extension - * The extension wherein the $library is defined. + * The extension in which the $library is defined. * @param string $library_name * Name of the library. * @param mixed $sub_key - * The library sub_key where the given asset is defined. + * The library sub key where the given asset is defined. * @param string $message * (optional) A message to display with the assertion. * @@ -218,11 +218,11 @@ protected function assertAssetInLibrary($asset, $extension, $library_name, $sub_ * @param string $asset * The asset file with the path for the file. * @param string $extension - * The extension where-in the $library_name is defined. + * The extension in which the $library_name is defined. * @param string $library_name * Name of the library. * @param mixed $sub_key - * The library sub_key where the given asset is defined. + * The library sub key where the given asset is defined. * @param string $message * (optional) A message to display with the assertion. * diff --git a/core/modules/system/tests/themes/test_theme/test_theme.info.yml b/core/modules/system/tests/themes/test_theme/test_theme.info.yml index f0525f4..d872e33 100644 --- a/core/modules/system/tests/themes/test_theme/test_theme.info.yml +++ b/core/modules/system/tests/themes/test_theme/test_theme.info.yml @@ -40,7 +40,7 @@ libraries-override: core/jquery: js: assets/vendor/jquery/jquery.min.js: js/collapse.js - # Use Drupal relative paths. + # Use Drupal-relative paths. core/drupal.dropbutton: css: component: @@ -50,12 +50,12 @@ libraries-override: css: component: misc/vertical-tabs.css: public://my_css/vertical-tabs.css - # Use protocol-relative URI. + # Use a protocol-relative URI. core/jquery.ui: css: component: assets/vendor/jquery.ui/themes/base/core.css: //my-server/my_theme/css/jquery_ui.css - # Use regular URI. + # Use an absolute URI. core/jquery.farbtastic: css: component: diff --git a/core/tests/Drupal/Tests/Core/Asset/LibraryDependencyResolverTest.php b/core/tests/Drupal/Tests/Core/Asset/LibraryDependencyResolverTest.php index 5ab278a..e8af816 100644 --- a/core/tests/Drupal/Tests/Core/Asset/LibraryDependencyResolverTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/LibraryDependencyResolverTest.php @@ -60,10 +60,10 @@ class LibraryDependencyResolverTest extends UnitTestCase { protected function setUp() { $this->libraryDiscovery = $this->getMockBuilder('Drupal\Core\Asset\LibraryDiscovery') ->disableOriginalConstructor() - ->setMethods(['getLibraryDefinitions']) + ->setMethods(['getLibrariesByExtension']) ->getMock(); $this->libraryDiscovery->expects($this->any()) - ->method('getLibraryDefinitions') + ->method('getLibrariesByExtension') ->with('test') ->will($this->returnValue($this->libraryData)); $this->libraryDependencyResolver= new LibraryDependencyResolver($this->libraryDiscovery);