Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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] ?? [];
}
Expand All @@ -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);
}
Expand Down
7 changes: 4 additions & 3 deletions tests/phpunit/Unit/AssetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
});

Expand All @@ -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());
}

Expand Down