8000 [AssetMapper] Better public without digest · symfony/symfony@54ebd6d · GitHub
[go: up one dir, main page]

Skip to content

Commit 54ebd6d

Browse files
weaverryannicolas-grekas
authored andcommitted
[AssetMapper] Better public without digest
1 parent 70b40fc commit 54ebd6d

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

src/Symfony/Component/AssetMapper/AssetMapper.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public function getAsset(string $logicalPath): ?MappedAsset
138138
$asset->setSourcePath($filePath);
139139

140140
$asset->setMimeType($this->getMimeType($logicalPath));
141+
$asset->setPublicPathWithoutDigest($this->getPublicPathWithoutDigest($logicalPath));
141142
$publicPath = $this->getPublicPath($logicalPath);
142143
$asset->setPublicPath($publicPath);
143144
[$digest, $isPredigested] = $this->getDigest($asset);
@@ -202,6 +203,11 @@ public function getPublicPath(string $logicalPath): ?string
202203
}, $logicalPath);
203204
}
204205

206+
private function getPublicPathWithoutDigest(string $logicalPath): string
207+
{
208+
return $this->publicPrefix.$logicalPath;
209+
}
210+
205211
public static function isPathPredigested(string $path): bool
206212
{
207213
return 1 === preg_match(self::PREDIGESTED_REGEX, $path);
@@ -239,11 +245,7 @@ private function getMimeType(string $logicalPath): ?string
239245

240246
$extension = pathinfo($logicalPath, \PATHINFO_EXTENSION);
241247

242-
if (!isset($this->extensionsMap[$extension])) {
243-
throw new \LogicException(sprintf('The file extension "%s" from "%s" does not correspond to any known types in the asset mapper. To support this extension, configure framework.asset_mapper.extensions.', $extension, $logicalPath));
244-
}
245-
246-
return $this->extensionsMap[$extension];
248+
return $this->extensionsMap[$extension] ?? null;
247249
}
248250

249251
private function calculateContent(MappedAsset $asset): string

src/Symfony/Component/AssetMapper/MappedAsset.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
*/
2121
final class MappedAsset
2222
{
23-
public string $publicPath;
23+
private string $publicPath;
24+
private string $publicPathWithoutDigest;
2425
/**
2526
* @var string the filesystem path to the source file
2627
*/
@@ -88,6 +89,15 @@ public function setPublicPath(string $publicPath): void
8889
$this->publicPath = $publicPath;
8990
}
9091

92+
public function setPublicPathWithoutDigest(string $publicPathWithoutDigest): void
93+
{
94+
if (isset($this->publicPathWithoutDigest)) {
95+
throw new \LogicException('Cannot set public path without digest: it was already set on the asset.');
96+
}
97+
98+
$this->publicPathWithoutDigest = $publicPathWithoutDigest;
99+
}
100+
91101
public function setSourcePath(string $sourcePath): void
92102
{
93103
if (isset($this->sourcePath)) {
@@ -132,16 +142,6 @@ public function addDependency(self $asset, bool $isLazy = false): void
132142

133143
public function getPublicPathWithoutDigest(): string
134144
{
135-
if ($this->isPredigested()) {
136-
return $this->getPublicPath();
137-
}
138-
139-
// remove last part of publicPath and replace with last part of logicalPath
140-
$publicPathParts = explode('/', $this->getPublicPath());
141-
$logicalPathParts = explode('/', $this->logicalPath);
142-
array_pop($publicPathParts);
143-
$publicPathParts[] = array_pop($logicalPathParts);
144-
145-
return implode('/', $publicPathParts);
145+
return $this->publicPathWithoutDigest;
146146
}
147147
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function testGetAsset()
6565
$asset = $assetMapper->getAsset('file2.js');
6666
$this->assertSame('file2.js', $asset->logicalPath);
6767
$this->assertMatchesRegularExpression('/^\/final-assets\/file2-[a-zA-Z0-9]{7,128}\.js$/', $asset->getPublicPath());
68+
$this->assertSame('/final-assets/file2.js', $asset->getPublicPathWithoutDigest());
6869
}
6970

7071
public function testGetAssetRespectsPreDigestedPaths()
@@ -73,6 +74,8 @@ public function testGetAssetRespectsPreDigestedPaths()
7374
$asset = $assetMapper->getAsset('already-abcdefVWXYZ0123456789.digested.css');
7475
$this->assertSame('already-abcdefVWXYZ0123456789.digested.css', $asset->logicalPath);
7576
$this->assertSame('/final-assets/already-abcdefVWXYZ0123456789.digested.css', $asset->getPublicPath());
77+
// for pre-digested files, the digest *is* part of the public path
78+
$this->assertSame('/final-assets/already-abcdefVWXYZ0123456789.digested.css', $asset->getPublicPathWithoutDigest());
7679
}
7780

7881
public function testGetAssetUsesManifestIfAvailable()

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function testGetPublicPath()
3131
$this->assertSame('/assets/foo.1234567.css', $asset->getPublicPath());
3232
}
3333

34+
public function testGetPublicPathWithoutDigest()
35+
{
36+
$asset = new MappedAsset('anything');
37+
$asset->setPublicPathWithoutDigest('/assets/foo.css');
38+
39+
$this->assertSame('/assets/foo.css', $asset->getPublicPathWithoutDigest());
40+
}
41+
3442
/**
3543
* @dataProvider getExtensionTests
3644
*/
@@ -48,29 +56,29 @@ public static function getExtensionTests(): iterable
4856
yield 'with_directory' => ['foo/bar.css', 'css'];
4957
}
5058

51-
public function testGetSourcePath(): void
59+
public function testGetSourcePath()
5260
{
5361
$asset = new MappedAsset('foo.css');
5462
$asset->setSourcePath('/path/to/source.css');
5563
$this->assertSame('/path/to/source.css', $asset->getSourcePath());
5664
}
5765

58-
public function testGetMimeType(): void
66+
public function testGetMimeType()
5967
{
6068
$asset = new MappedAsset('foo.css');
6169
$asset->setMimeType('text/css');
6270
$this->assertSame('text/css', $asset->getMimeType());
6371
}
6472

65-
public function testGetDigest(): void
73+
public function testGetDigest()
6674
{
6775
$asset = new MappedAsset('foo.css');
6876
$asset->setDigest('1234567', false);
6977
$this->assertSame('1234567', $asset->getDigest());
7078
$this->assertFalse($asset->isPredigested());
7179
}
7280

73-
public function testGetContent(): void
81+
public function testGetContent()
7482
{
7583
$asset = new MappedAsset('foo.css');
7684
$asset->setContent('body { color: red; }');

0 commit comments

Comments
 (0)
0