Skip to content

Commit

Permalink
Add AssetManager::getUrl() (#150)
Browse files Browse the repository at this point in the history
* Add `AssetManager::getUrl()`

* Update CHANGELOG.md

---------

Co-authored-by: Alexander Makarov <[email protected]>
  • Loading branch information
vjik and samdark authored Dec 11, 2024
1 parent f0604e8 commit aa82eff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Enh #119, #129: Add debug collector for `yiisoft/yii-debug` (@xepozz, @vjik)
- Enh #148: Raise the minimum PHP version to 8.1 and minor refactoring (@vjik)
- Enh #149: Improve `AssetBundle` properties' Psalm types (@vjik)
- New #150: Add `AssetManager::getUrl()` method instead of `getAssetUrl()` method that is marked as deprecated (@vjik)

## 4.0.0 February 13, 2023

Expand Down
17 changes: 17 additions & 0 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,25 @@ public function getBundle(string $name): AssetBundle
* @throws InvalidConfigException If asset files are not found.
*
* @return string The actual URL for the specified asset.
*
* @deprecated Use {@see getUrl()} instead.
*/
public function getAssetUrl(string $name, string $path): string
{
return $this->getUrl($name, $path);
}

/**
* Returns the actual URL for the specified asset.
*
* @param string $name The asset bundle name.
* @param string $path The asset path.
*
* @throws InvalidConfigException If asset files are not found.
*
* @return string The actual URL for the specified asset.
*/
public function getUrl(string $name, string $path): string
{
return $this->loader->getAssetUrl($this->getBundle($name), $path);
}
Expand Down
16 changes: 11 additions & 5 deletions tests/AssetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public function testExportWithoutRegisterWithoutAllowedBundles(): void
$this->manager->export(new JsonAssetExporter($this->aliases->get('@asset/test.json')));
}

public function testGetAssetUrl(): void
public function testGetUrl(): void
{
$bundle = new ExportAsset();
$sourcePath = $this->aliases->get($bundle->sourcePath);
Expand All @@ -523,20 +523,26 @@ public function testGetAssetUrl(): void

$this->assertSame(
$this->aliases->get("@assetUrl/{$hash}/css/stub.css"),
$this->manager->getAssetUrl(ExportAsset::class, 'css/stub.css'),
$this->manager->getUrl(ExportAsset::class, 'css/stub.css'),
);
$this->assertSame(
$this->aliases->get("@assetUrl/{$hash}/js/stub.js"),
$this->manager->getAssetUrl(ExportAsset::class, 'js/stub.js'),
$this->manager->getUrl(ExportAsset::class, 'js/stub.js'),
);
$this->assertSame(
$this->aliases->get("@assetUrl/{$hash}/export/stub.css"),
$this->manager->getAssetUrl(ExportAsset::class, 'export/stub.css'),
$this->manager->getUrl(ExportAsset::class, 'export/stub.css'),
);
$this->assertSame(
$this->aliases->get("@assetUrl/{$hash}/export/stub.js"),
$this->manager->getAssetUrl(ExportAsset::class, 'export/stub.js'),
$this->manager->getUrl(ExportAsset::class, 'export/stub.js'),
);
$this->assertSame(
$this->aliases->get("@assetUrl/{$hash}/export/yii-logo.png"),
$this->manager->getUrl(ExportAsset::class, 'export/yii-logo.png'),
);

// Test deprecated `getAssetUrl()` method
$this->assertSame(
$this->aliases->get("@assetUrl/{$hash}/export/yii-logo.png"),
$this->manager->getAssetUrl(ExportAsset::class, 'export/yii-logo.png'),
Expand Down

0 comments on commit aa82eff

Please sign in to comment.