8000 [AssetMapper] Sometimes asset contents are built from non-asset files · symfony/symfony@c05fdb8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c05fdb8

Browse files
committed
[AssetMapper] Sometimes asset contents are built from non-asset files
1 parent 5bf5c4d commit c05fdb8

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

src/Symfony/Component/AssetMapper/Factory/CachedMappedAssetFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ private function getCacheFilePath(string $logicalPath, string $sourcePath): stri
5959
*/
6060
private function collectResourcesFromAsset(MappedAsset $mappedAsset): array
6161
{
62-
$resources = [new FileResource($mappedAsset->getSourcePath())];
62+
$resources = array_map(fn (string $path) => new FileResource($path), $mappedAsset->getFileDependencies());
63+
$resources[] = new FileResource($mappedAsset->getSourcePath());
6364

6465
foreach ($mappedAsset->getDependencies() as $dependency) {
6566
if (!$dependency->isContentDependency) {

src/Symfony/Component/AssetMapper/MappedAsset.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ final class MappedAsset
3131
private bool $isPredigested;
3232
/** @var AssetDependency[] */
3333
private array $dependencies = [];
34+
/** @var string[] */
35+
private array $fileDependencies = [];
3436

3537
public function __construct(private readonly string $logicalPath)
3638
{
@@ -79,6 +81,14 @@ public function getDependencies(): array
7981
return $this->dependencies;
8082
}
8183

84+
/**
85+
* @return string[]
86+
*/
87+
public function getFileDependencies(): array
88+
{
89+
return $this->fileDependencies;
90+
}
91+
8292
public function setPublicPath(string $publicPath): void
8393
{
8494
if (isset($this->publicPath)) {
@@ -130,6 +140,16 @@ public function addDependency(AssetDependency $assetDependency): void
130140
$this->dependencies[] = $assetDependency;
131141
}
132142

143+
/**
144+
* Any filesystem files whose contents are used to create this asset.
145+
*
146+
* This is used to invalidate the cache when any of these files change.
147+
*/
148+
public function addFileDependency(string $sourcePath): void
149+
{
150+
$this->fileDependencies[] = $sourcePath;
151+
}
152+
133153
public function getPublicPathWithoutDigest(): string
134154
{
135155
return $this->publicPathWithoutDigest;

src/Symfony/Component/AssetMapper/Tests/Factory/CachedMappedAssetFactoryTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public function testAssetConfigCacheResourceContainsDependencies()
106106
$notDependentOnContentAsset->setSourcePath(__DIR__.'/../fixtures/dir2/already-abcdefVWXYZ0123456789.digested.css');
107107
$mappedAsset->addDependency(new AssetDependency($notDependentOnContentAsset, isContentDependency: false));
108108

109+
// just adding any file as an example
110+
$mappedAsset->addFileDependency(__DIR__.'/../fixtures/importmap.php');
111+
109112
$factory = $this->createMock(MappedAssetFactoryInterface::class);
110113
$factory->expects($this->once())
111114
->method('createMappedAsset')
@@ -119,12 +122,13 @@ public function testAssetConfigCacheResourceContainsDependencies()
119122
$cachedFactory->createMappedAsset('file1.css', $sourcePath);
120123

121124
$configCacheMetadata = $this->loadConfigCacheMetadataFor($mappedAsset);
122-
$this->assertCount(3, $configCacheMetadata);
125+
$this->assertCount(4, $configCacheMetadata);
123126
$this->assertInstanceOf(FileResource::class, $configCacheMetadata[0]);
124127
$this->assertInstanceOf(FileResource::class, $configCacheMetadata[1]);
125-
$this->assertSame($mappedAsset->getSourcePath(), $configCacheMetadata[0]->getResource());
126-
$this->assertSame($dependentOnContentAsset->getSourcePath(), $configCacheMetadata[1]->getResource());
127-
$this->assertSame($deeplyNestedAsset->getSourcePath(), $configCacheMetadata[2]->getResource());
128+
$this->assertSame(realpath(__DIR__.'/../fixtures/importmap.php'), $configCacheMetadata[0]->getResource());
129+
$this->assertSame($mappedAsset->getSourcePath(), $configCacheMetadata[1]->getResource());
130+
$this->assertSame($dependentOnContentAsset->getSourcePath(), $configCacheMetadata[2]->getResource());
131+
$this->assertSame($deeplyNestedAsset->getSourcePath(), $configCacheMetadata[3]->getResource());
128132
}
129133

130134
private function loadConfigCacheMetadataFor(MappedAsset $mappedAsset): array

src/Symfony/Component/AssetMapper/Tests/MappedAssetTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\AssetMapper\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\AssetMapper\AssetDependency;
1516
use Symfony\Component\AssetMapper\MappedAsset;
1617

1718
class MappedAssetTest extends TestCase
@@ -78,4 +79,17 @@ public function testGetContent()
7879
$asset->setContent('body { color: red; }');
7980
$this->assertSame('body { color: red; }', $asset->getContent());
8081
}
82+
83+
public function testAddDependencies()
84+
{
85+
$mainAsset = new MappedAsset('file.js');
86+
87+
$assetFoo = new MappedAsset('foo.js');
88+
$dependency = new AssetDependency($assetFoo, false, false);
89+
$mainAsset->addDependency($dependency);
90+
$mainAsset->addFileDependency('/path/to/foo.js');
91+
92+
$this->assertSame([$dependency], $mainAsset->getDependencies());
93+
$this->assertSame(['/path/to/foo.js'], $mainAsset->getFileDependencies());
94+
}
8195
}

0 commit comments

Comments
 (0)
0