diff --git a/docs/getting-started.md b/docs/getting-started.md index 3a435d0..d7f83d2 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -37,8 +37,8 @@ Since the output of the `manifest.json` is fixed, we're limited with the Loader. **manifest.json** ```json { - "script.js": "/public/path/script.23dafsf2138d.js", - "style.css": "style.23dafsf2138d.css" + "script-handle": "/public/path/script.23dafsf2138d.js", + "style-handle": "style.23dafsf2138d.css" } ``` @@ -54,16 +54,16 @@ add_action( AssetManager::ACTION_SETUP, function(AssetManager $assetManager) { $assetManager->extendAsset( - Style::class, - 'style.css', + 'style-handle', + Style::class, [ 'inline' => ['before' => ':root { --black: #000; }'] 'enqueue' => false, ] ); $assetManager->extendAsset( + 'script-handle', Script::class, - 'script.js', [ 'enqueue' => static fn(): bool => is_user_logged_in(), 'inFooter' => true, diff --git a/src/AssetManager.php b/src/AssetManager.php index e01bd47..ebac872 100644 --- a/src/AssetManager.php +++ b/src/AssetManager.php @@ -139,13 +139,13 @@ public function asset(string $handle, ?string $type = null): ?Asset } /** - * @param string $type * @param string $handle + * @param string $type * @param AssetExtensionConfig $extensions * * @return $this */ - public function extendAsset(string $type, string $handle, array $extensions): AssetManager + public function extendAsset(string $handle, string $type, array $extensions): AssetManager { $existingExtension = $this->extensions[$type][$handle] ?? []; $extensions = array_merge_recursive($existingExtension, $extensions); @@ -162,12 +162,12 @@ public function extendAsset(string $type, string $handle, array $extensions): As } /** - * @param string $type * @param string $handle + * @param string $type * * @return AssetExtensionConfig */ - public function assetExtensions(string $type, string $handle): array + public function assetExtensions(string $handle, string $type): array { return $this->extensions[$type][$handle] ?? []; } @@ -181,7 +181,7 @@ protected function extendAndRegisterAsset(Asset $asset): AssetManager { $handle = $asset->handle(); $type = get_class($asset); - $extensions = $this->assetExtensions($type, $handle); + $extensions = $this->assetExtensions($handle, $type); if (count($extensions) > 0 && !$this->isAssetProcessed($asset)) { $asset = AssetFactory::configureAsset($asset, $extensions); } diff --git a/tests/phpunit/Unit/AssetManagerTest.php b/tests/phpunit/Unit/AssetManagerTest.php index 514c1a7..d9778b1 100644 --- a/tests/phpunit/Unit/AssetManagerTest.php +++ b/tests/phpunit/Unit/AssetManagerTest.php @@ -128,7 +128,7 @@ public function testWithAssetExtension(): void $handle = 'foo'; $assetManager = $this->factoryAssetManager(); - $assetManager->extendAsset(Script::class, $handle, ['enqueue' => false]); + $assetManager->extendAsset($handle, Script::class, ['enqueue' => false]); $script = new Script($handle, ''); $script->canEnqueue(true); @@ -142,6 +142,7 @@ public function testWithAssetExtension(): void $asset = $assetManager->asset($handle, Script::class); static::assertFalse($asset->enqueue()); + static::assertCount(1, $assetManager->assetExtensions($handle, Script::class)); } public function testWithAssetExtensionInSetupAction(): void @@ -157,7 +158,7 @@ public function testWithAssetExtensionInSetupAction(): void ->once() ->with($assetManager) ->whenHappen(static function (AssetManager $manager) use ($handle, $script) { - $manager->extendAsset(Script::class, $handle, ['enqueue' => false]); + $manager->extendAsset($handle, Script::class, ['enqueue' => false]); $manager->register($script); }); @@ -184,7 +185,7 @@ public function testWithAssetExtensionAfterSetup(): void $asset = $assetManager->asset($handle, Script::class); // Extend the Asset after it is being accessed but before being processed. - $assetManager->extendAsset(Script::class, $handle, ['enqueue' => false]); + $assetManager->extendAsset($handle, Script::class, ['enqueue' => false]); static::assertFalse($asset->enqueue()); }