Skip to content

Commit

Permalink
bug #6124 Add full support for AssetMapper entries (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.x branch.

Discussion
----------

Add full support for AssetMapper entries

Fixes #6123.

Commits
-------

7c52583 dd full support for AssetMapper entries
  • Loading branch information
javiereguiluz committed Jan 31, 2024
2 parents c2ceb14 + 7c52583 commit 66cdc15
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Dto/AssetsDto.php
Expand Up @@ -145,6 +145,11 @@ public function loadedOn(?string $pageName): self
$filteredAssets->addJsAsset($jsAsset);
}
}
foreach ($this->assetMapperAssets as $assetMapperAsset) {
if ($assetMapperAsset->getLoadedOn()->has($pageName)) {
$filteredAssets->addAssetMapperAsset($assetMapperAsset);
}
}
foreach ($this->webpackEncoreAssets as $webpackEncoreAsset) {
if ($webpackEncoreAsset->getLoadedOn()->has($pageName)) {
$filteredAssets->addWebpackEncoreAsset($webpackEncoreAsset);
Expand All @@ -162,6 +167,7 @@ public function loadedOn(?string $pageName): self

public function mergeWith(self $assetsDto): self
{
$this->assetMapperAssets = array_merge($this->assetMapperAssets, $assetsDto->getAssetMapperAssets());
$this->webpackEncoreAssets = array_merge($this->webpackEncoreAssets, $assetsDto->getWebpackEncoreAssets());
$this->cssAssets = array_merge($this->cssAssets, $assetsDto->getCssAssets());
$this->jsAssets = array_merge($this->jsAssets, $assetsDto->getJsAssets());
Expand Down
5 changes: 5 additions & 0 deletions src/Dto/FieldDto.php
Expand Up @@ -401,6 +401,11 @@ public function setAssets(AssetsDto $assets): void
$this->assets = $assets;
}

public function addAssetMapperEncoreAsset(AssetDto $assetDto): void
{
$this->assets->addAssetMapperAsset($assetDto);
}

public function addWebpackEncoreAsset(AssetDto $assetDto): void
{
$this->assets->addWebpackEncoreAsset($assetDto);
Expand Down
17 changes: 17 additions & 0 deletions src/Field/FieldTrait.php
Expand Up @@ -257,6 +257,23 @@ public function addWebpackEncoreEntries(Asset|string ...$entryNamesOrAssets): se
return $this;
}

public function addAssetMapperEntries(Asset|string ...$entryNamesOrAssets): self
{
if (!class_exists('Symfony\\Component\\AssetMapper\\AssetMapper')) {
throw new \RuntimeException('You are trying to add AssetMapper entries in a field but AssetMapper is not installed in your project. Try running "composer require symfony/asset-mapper"');
}

foreach ($entryNamesOrAssets as $entryNameOrAsset) {
if (\is_string($entryNameOrAsset)) {
$this->dto->addAssetMapperEncoreAsset(new AssetDto($entryNameOrAsset));
} else {
$this->dto->addAssetMapperEncoreAsset($entryNameOrAsset->getAsDto());
}
}

return $this;
}

public function addCssFiles(Asset|string ...$pathsOrAssets): self
{
foreach ($pathsOrAssets as $pathOrAsset) {
Expand Down

0 comments on commit 66cdc15

Please sign in to comment.