diff --git a/core/core.services.yml b/core/core.services.yml index 2ee184054a7b49a41abb16c0da3da71e2a97287f..cc45b91323434bf49b817f5818ca88fbaa23cdae 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1680,14 +1680,15 @@ services: class: Drupal\Core\Asset\AssetDumper arguments: ['@file_system'] library.discovery: - class: Drupal\Core\Asset\LibraryDiscovery - arguments: ['@library.discovery.collector'] - Drupal\Core\Asset\LibraryDiscoveryInterface: '@library.discovery' - library.discovery.collector: class: Drupal\Core\Asset\LibraryDiscoveryCollector arguments: ['@cache.discovery', '@lock', '@library.discovery.parser', '@theme.manager'] tags: - { name: needs_destruction } + Drupal\Core\Asset\LibraryDiscoveryInterface: '@library.discovery' + library.discovery.collector: + arguments: ['@cache.discovery', '@lock', '@library.discovery.parser', '@theme.manager'] + class: Drupal\Core\Asset\LibraryDiscoveryCollector + deprecated: The "%service_id%" service is deprecated in drupal:11.1.0 and is removed from drupal:12.0.0. Use LibraryDiscovery instead. See https://www.drupal.org/node/3462970 library.discovery.parser: class: Drupal\Core\Asset\LibraryDiscoveryParser arguments: ['%app.root%', '@module_handler', '@theme.manager', '@stream_wrapper_manager', '@library.libraries_directory_file_finder', '@extension.path.resolver', '@plugin.manager.sdc'] diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscovery.php b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php index feb343ec301bb76275b9bce7a5a4ccdc3c2df879..347fa942eab523dde04ccc782fa8addd9a13b17d 100644 --- a/core/lib/Drupal/Core/Asset/LibraryDiscovery.php +++ b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php @@ -6,6 +6,10 @@ /** * Discovers available asset libraries in Drupal. + * + * @deprecated in drupal:11.1.0 and is removed from drupal:12.0.0. Use + * \Drupal\Core\Asset\LibraryDiscoveryCollector instead. + * @see https://www.drupal.org/node/3462970 */ class LibraryDiscovery implements LibraryDiscoveryInterface { @@ -16,16 +20,6 @@ class LibraryDiscovery implements LibraryDiscoveryInterface { */ protected $collector; - /** - * The final library definitions, statically cached. - * - * Hooks hook_library_info_alter() and hook_js_settings_alter() allow modules - * and themes to dynamically alter a library definition (once per request). - * - * @var array - */ - protected $libraryDefinitions = []; - /** * Constructs a new LibraryDiscovery instance. * @@ -33,6 +27,7 @@ class LibraryDiscovery implements LibraryDiscoveryInterface { * The library discovery cache collector. */ public function __construct(CacheCollectorInterface $library_discovery_collector) { + trigger_error(__CLASS__ . 'is deprecated in drupal:11.1.0 and is removed from drupal:12.0.0. Use LibraryDiscoveryCollector instead. See https://www.drupal.org/node/3462970', E_USER_DEPRECATED); $this->collector = $library_discovery_collector; } @@ -40,22 +35,14 @@ public function __construct(CacheCollectorInterface $library_discovery_collector * {@inheritdoc} */ public function getLibrariesByExtension($extension) { - if (!isset($this->libraryDefinitions[$extension])) { - $libraries = $this->collector->get($extension); - $this->libraryDefinitions[$extension] = []; - foreach ($libraries as $name => $definition) { - $this->libraryDefinitions[$extension][$name] = $definition; - } - } - - return $this->libraryDefinitions[$extension]; + return $this->collector->get($extension); } /** * {@inheritdoc} */ public function getLibraryByName($extension, $name) { - $libraries = $this->getLibrariesByExtension($extension); + $libraries = $this->collector->get($extension); if (!isset($libraries[$name])) { return FALSE; } @@ -70,7 +57,6 @@ public function getLibraryByName($extension, $name) { * {@inheritdoc} */ public function clearCachedDefinitions() { - $this->libraryDefinitions = []; $this->collector->clear(); } diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscoveryCollector.php b/core/lib/Drupal/Core/Asset/LibraryDiscoveryCollector.php index 5379946b0c86e0a0d4b272cee55a05af3beb1a89..f7c5afb17be9a87394ec233e7bf8b777fda887c2 100644 --- a/core/lib/Drupal/Core/Asset/LibraryDiscoveryCollector.php +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryCollector.php @@ -13,7 +13,7 @@ /** * A CacheCollector implementation for building library extension info. */ -class LibraryDiscoveryCollector extends CacheCollector { +class LibraryDiscoveryCollector extends CacheCollector implements LibraryDiscoveryInterface { /** * The library discovery parser. @@ -159,6 +159,28 @@ protected function applyLibrariesExtend($extension, $library_name, $library_defi return $library_definition; } + /** + * {@inheritdoc} + */ + public function getLibrariesByExtension($extension) { + return $this->get($extension); + } + + /** + * {@inheritdoc} + */ + public function getLibraryByName($extension, $name) { + $libraries = $this->getLibrariesByExtension($extension); + if (!isset($libraries[$name])) { + return FALSE; + } + if (isset($libraries[$name]['deprecated'])) { + // phpcs:ignore Drupal.Semantics.FunctionTriggerError + @trigger_error(str_replace('%library_id%', "$extension/$name", $libraries[$name]['deprecated']), E_USER_DEPRECATED); + } + return $libraries[$name]; + } + /** * {@inheritdoc} */ @@ -167,4 +189,16 @@ public function reset() { $this->cid = NULL; } + /** + * Clears static and persistent cache. + * + * @deprecated in drupal:11.1.0 and is removed from drupal:12.0.0. Use + * LibraryDiscoveryCollector::clear() instead. + * @see https://www.drupal.org/node/3462970 + */ + public function clearCachedDefinitions() { + @trigger_error(__METHOD__ . 'is deprecated in drupal:11.0.0 and is removed from drupal:12.0.0. Use ::clear() instead. See https://www.drupal.org/node/3462970', E_USER_DEPRECATED); + $this->clear(); + } + } diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscoveryInterface.php b/core/lib/Drupal/Core/Asset/LibraryDiscoveryInterface.php index 0f4e70474b906df9bf71059c7159e6322dde8c77..3003015f662457bcf93ddbf2d5f243894facfbd4 100644 --- a/core/lib/Drupal/Core/Asset/LibraryDiscoveryInterface.php +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryInterface.php @@ -47,6 +47,10 @@ public function getLibraryByName($extension, $name); /** * Clears static and persistent library definition caches. + * + * @deprecated in drupal:11.1.0 and is removed from drupal:12.0.0. Use + * LibraryDiscoveryCollector::clear() instead. + * @see https://www.drupal.org/node/3462970 */ public function clearCachedDefinitions(); diff --git a/core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php b/core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php index 0f2235a85a71508cedec1267064fbac7e5fbfb87..518ef1638ed0aa16b9e62f8593779cca2ca61cba 100644 --- a/core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php +++ b/core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php @@ -422,7 +422,7 @@ public function testDynamicLibrary(): void { // Retrieve a dynamic library definition. // @see common_test_library_info_build() \Drupal::state()->set('common_test.library_info_build_test', TRUE); - $library_discovery->clearCachedDefinitions(); + $library_discovery->clear(); $dynamic_library = $library_discovery->getLibraryByName('common_test', 'dynamic_library'); $this->assertIsArray($dynamic_library); $this->assertArrayHasKey('version', $dynamic_library); diff --git a/core/tests/Drupal/KernelTests/Core/Asset/LibraryDiscoveryIntegrationTest.php b/core/tests/Drupal/KernelTests/Core/Asset/LibraryDiscoveryIntegrationTest.php index 6c3096ff5df4fb07519818e54ebed01a1056f2c4..9967b1f552135741a8bec24dc54467c35bc018fa 100644 --- a/core/tests/Drupal/KernelTests/Core/Asset/LibraryDiscoveryIntegrationTest.php +++ b/core/tests/Drupal/KernelTests/Core/Asset/LibraryDiscoveryIntegrationTest.php @@ -285,7 +285,7 @@ protected function activateTheme($theme_name) { $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName($theme_name)); - $this->libraryDiscovery->clearCachedDefinitions(); + $this->libraryDiscovery->clear(); $this->assertSame($theme_name, $theme_manager->getActiveTheme()->getName()); } diff --git a/core/tests/Drupal/KernelTests/Core/Asset/ResolvedLibraryDefinitionsFilesMatchTest.php b/core/tests/Drupal/KernelTests/Core/Asset/ResolvedLibraryDefinitionsFilesMatchTest.php index 3a7e1328ec1e7a82a47eac2f7ded5c9b85ae6db6..74efc95ab4072a61cd753404335acd09ee4cd48d 100644 --- a/core/tests/Drupal/KernelTests/Core/Asset/ResolvedLibraryDefinitionsFilesMatchTest.php +++ b/core/tests/Drupal/KernelTests/Core/Asset/ResolvedLibraryDefinitionsFilesMatchTest.php @@ -163,7 +163,7 @@ public function testCoreLibraryCompleteness(): void { // and these changes are only applied for the active theme. foreach ($this->allThemes as $theme) { $this->themeManager->setActiveTheme($this->themeInitialization->getActiveThemeByName($theme)); - $this->libraryDiscovery->clearCachedDefinitions(); + $this->libraryDiscovery->clear(); $this->verifyLibraryFilesExist($this->getAllLibraries()); } diff --git a/core/tests/Drupal/KernelTests/Core/Theme/Stable9LibraryOverrideTest.php b/core/tests/Drupal/KernelTests/Core/Theme/Stable9LibraryOverrideTest.php index be6b1fc5342736589d1b01d2cddc255eecd61275..9d900ebcb2a4e70244471ce227ae27169ea23622 100644 --- a/core/tests/Drupal/KernelTests/Core/Theme/Stable9LibraryOverrideTest.php +++ b/core/tests/Drupal/KernelTests/Core/Theme/Stable9LibraryOverrideTest.php @@ -64,7 +64,7 @@ public function testStable9LibraryOverrides(): void { $libraries_before = $this->removeVendorAssets($libraries_before); $this->themeManager->setActiveTheme($this->themeInitialization->getActiveThemeByName('stable9')); - $this->libraryDiscovery->clearCachedDefinitions(); + $this->libraryDiscovery->clear(); // Now get the library definitions with Stable 9 as the active theme. $libraries_after = $this->getAllLibraries(); diff --git a/core/tests/Drupal/Tests/Core/Asset/LibraryDependencyResolverTest.php b/core/tests/Drupal/Tests/Core/Asset/LibraryDependencyResolverTest.php index fa65a72ff51459f8ea3f3d96995cb11d50286955..cfc48663b901ded407bc21a31ed0486ddcab0bce 100644 --- a/core/tests/Drupal/Tests/Core/Asset/LibraryDependencyResolverTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/LibraryDependencyResolverTest.php @@ -5,6 +5,7 @@ namespace Drupal\Tests\Core\Asset; use Drupal\Core\Asset\LibraryDependencyResolver; +use Drupal\Core\Asset\LibraryDiscoveryCollector; use Drupal\Tests\UnitTestCase; /** @@ -13,6 +14,14 @@ */ class LibraryDependencyResolverTest extends UnitTestCase { + + /** + * The mock library discovery parser. + * + * @var \Drupal\Core\Asset\LibraryDiscoveryParser|\PHPUnit\Framework\MockObject\MockObject + */ + protected $libraryDiscoveryParser; + /** * The tested library dependency resolver. * @@ -27,13 +36,6 @@ class LibraryDependencyResolverTest extends UnitTestCase { */ protected $libraryDiscovery; - /** - * The mocked module handler. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject - */ - protected $moduleHandler; - /** * Test library data. * @@ -59,7 +61,11 @@ class LibraryDependencyResolverTest extends UnitTestCase { protected function setUp(): void { parent::setUp(); - $this->libraryDiscovery = $this->getMockBuilder('Drupal\Core\Asset\LibraryDiscovery') + $this->libraryDiscoveryParser = $this->getMockBuilder('Drupal\Core\Asset\LibraryDiscoveryParser') + ->disableOriginalConstructor() + ->getMock(); + + $this->libraryDiscovery = $this->getMockBuilder(LibraryDiscoveryCollector::class) ->disableOriginalConstructor() ->onlyMethods(['getLibrariesByExtension']) ->getMock(); diff --git a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php index a10dac0d043a59dab3487bf7e86aca1990420c1b..6b8df5044f271c133baecec12d0e2745731b1a8a 100644 --- a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryTest.php @@ -4,7 +4,6 @@ namespace Drupal\Tests\Core\Asset; -use Drupal\Core\Asset\LibraryDiscovery; use Drupal\Tests\UnitTestCase; /** @@ -16,17 +15,10 @@ class LibraryDiscoveryTest extends UnitTestCase { /** * The tested library discovery service. * - * @var \Drupal\Core\Asset\LibraryDiscovery + * @var \Drupal\Core\Asset\LibraryDiscoveryCollector */ protected $libraryDiscovery; - /** - * The mocked library discovery cache collector. - * - * @var \Drupal\Core\Cache\CacheCollectorInterface|\PHPUnit\Framework\MockObject\MockObject - */ - protected $libraryDiscoveryCollector; - /** * The cache tags invalidator. * @@ -68,28 +60,18 @@ protected function setUp(): void { parent::setUp(); $this->cacheTagsInvalidator = $this->createMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); - $this->libraryDiscoveryCollector = $this->getMockBuilder('Drupal\Core\Asset\LibraryDiscoveryCollector') + $this->libraryDiscovery = $this->getMockBuilder('Drupal\Core\Asset\LibraryDiscoveryCollector') + ->onlyMethods(['resolveCacheMiss', 'getLibrariesByExtension']) ->disableOriginalConstructor() ->getMock(); - $this->libraryDiscovery = new LibraryDiscovery($this->libraryDiscoveryCollector); - $this->libraryDiscoveryCollector->expects($this->once()) - ->method('get') + $this->libraryDiscovery->expects($this->any()) + ->method('resolveCacheMiss') + ->with('test') + ->willReturn($this->libraryData); + $this->libraryDiscovery->expects($this->any()) + ->method('getLibrariesByExtension') ->with('test') ->willReturn($this->libraryData); - } - - /** - * @covers ::getLibrariesByExtension - */ - public function testGetLibrariesByExtension(): void { - $this->libraryDiscovery->getLibrariesByExtension('test'); - // Verify that subsequent calls don't trigger hook_library_info_alter() - // and hook_js_settings_alter() invocations, nor do they talk to the - // collector again. This ensures that the alterations made by - // hook_library_info_alter() and hook_js_settings_alter() implementations - // are statically cached, as desired. - $this->libraryDiscovery->getLibraryByName('test', 'test_1'); - $this->libraryDiscovery->getLibrariesByExtension('test'); } /**