8000 [AssetMapper] Sometimes asset contents are built from non-asset files by weaverryan · Pull Request #50400 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[AssetMapper] Sometimes asset contents are built from non-asset files #50400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ private function getCacheFilePath(string $logicalPath, string $sourcePath): stri
*/
private function collectResourcesFromAsset(MappedAsset $mappedAsset): array
{
$resources = [new FileResource($mappedAsset->getSourcePath())];
$resources = array_map(fn (string $path) => new FileResource($path), $mappedAsset->getFileDependencies());
$resources[] = new FileResource($mappedAsset->getSourcePath());

foreach ($mappedAsset->getDependencies() as $dependency) {
if (!$dependency->isContentDependency) {
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/AssetMapper/MappedAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ final class MappedAsset
private bool $isPredigested;
/** @var AssetDependency[] */
private array $dependencies = [];
/** @var string[] */
private array $fileDependencies = [];

public function __construct(private readonly string $logicalPath)
{
Expand Down Expand Up @@ -79,6 +81,14 @@ public function getDependencies(): array
return $this->dependencies;
}

/**
* @return string[]
*/
public function getFileDependencies(): array
{
return $this->fileDependencies;
}

public function setPublicPath(string $publicPath): void
{
if (isset($this->publicPath)) {
Expand Down Expand Up @@ -130,6 +140,16 @@ public function addDependency(AssetDependency $assetDependency): void
$this->dependencies[] = $assetDependency;
}

/**
* Any filesystem files whose contents are used to create this asset.
*
* This is used to invalidate the cache when any of these files change.
*/
public function addFileDependency(string $sourcePath): void
{
$this->fileDependencies[] = $sourcePath;
}

public function getPublicPathWithoutDigest(): string
{
return $this->publicPathWithoutDigest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public function testAssetConfigCacheResourceContainsDependencies()
$notDependentOnContentAsset->setSourcePath(__DIR__.'/../fixtures/dir2/already-abcdefVWXYZ0123456789.digested.css');
$mappedAsset->addDependency(new AssetDependency($notDependentOnContentAsset, isContentDependency: false));

// just adding any file as an example
$mappedAsset->addFileDependency(__DIR__.'/../fixtures/importmap.php');

$factory = $this->createMock(MappedAssetFactoryInterface::class);
$factory->expects($this->once())
->method('createMappedAsset')
Expand All @@ -119,12 +122,13 @@ public function testAssetConfigCacheResourceContainsDependencies()
$cachedFactory->createMappedAsset('file1.css', $sourcePath);

$configCacheMetadata = $this->loadConfigCacheMetadataFor($mappedAsset);
$this->assertCount(3, $configCacheMetadata);
$this->assertCount(4, $configCacheMetadata);
$this->assertInstanceOf(FileResource::class, $configCacheMetadata[0]);
$this->assertInstanceOf(FileResource::class, $configCacheMetadata[1]);
$this->assertSame($mappedAsset->getSourcePath(), $configCacheMetadata[0]->getResource());
$this->assertSame($dependentOnContentAsset->getSourcePath(), $configCacheMetadata[1]->getResource());
$this->assertSame($deeplyNestedAsset->getSourcePath(), $configCacheMetadata[2]->getResource());
$this->assertSame(realpath(__DIR__.'/../fixtures/importmap.php'), $configCacheMetadata[0]->getResource());
$this->assertSame($mappedAsset->getSourcePath(), $configCacheMetadata[1]->getResource());
$this->assertSame($dependentOnContentAsset->getSourcePath(), $configCacheMetadata[2]->getResource());
$this->assertSame($deeplyNestedAsset->getSourcePath(), $configCacheMetadata[3]->getResource());
}

private function loadConfigCacheMetadataFor(MappedAsset $mappedAsset): array
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/AssetMapper/Tests/MappedAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\AssetMapper\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\AssetMapper\AssetDependency;
use Symfony\Component\AssetMapper\MappedAsset;

class MappedAssetTest extends TestCase
Expand Down Expand Up @@ -78,4 +79,17 @@ public function testGetContent()
$asset->setContent('body { color: red; }');
$this->assertSame('body { color: red; }', $asset->getContent());
}

public function testAddDependencies()
{
$mainAsset = new MappedAsset('file.js');

$assetFoo = new MappedAsset('foo.js');
$dependency = new AssetDependency($assetFoo, false, false);
$mainAsset->addDependency($dependency);
$mainAsset->addFileDependency('/path/to/foo.js');

$this->assertSame([$dependency], $mainAsset->getDependencies());
$this->assertSame(['/path/to/foo.js'], $mainAsset->getFileDependencies());
}
}
0