Skip to content

[AssetMapper] Better public without digest #50232

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
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
12 changes: 7 additions & 5 deletions src/Symfony/Component/AssetMapper/AssetMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function getAsset(string $logicalPath): ?MappedAsset
$asset->setSourcePath($filePath);

$asset->setMimeType($this->getMimeType($logicalPath));
$asset->setPublicPathWithoutDigest($this->getPublicPathWithoutDigest($logicalPath));
$publicPath = $this->getPublicPath($logicalPath);
$asset->setPublicPath($publicPath);
[$digest, $isPredigested] = $this->getDigest($asset);
Expand Down Expand Up @@ -202,6 +203,11 @@ public function getPublicPath(string $logicalPath): ?string
}, $logicalPath);
}

private function getPublicPathWithoutDigest(string $logicalPath): string
{
return $this->publicPrefix.$logicalPath;
}

public static function isPathPredigested(string $path): bool
{
return 1 === preg_match(self::PREDIGESTED_REGEX, $path);
Expand Down Expand Up @@ -239,11 +245,7 @@ private function getMimeType(string $logicalPath): ?string

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

if (!isset($this->extensionsMap[$extension])) {
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));
}

return $this->extensionsMap[$extension];
return $this->extensionsMap[$extension] ?? null;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated. I had a file that mentioned a sourcemap - so foo.js.map - and the system exploded because we didn't know the mime type of .map. The mime type is only important for (A) the dev server to return that asset with the correctContent-Type and (B) for asset compilers, if they rely on the mime type. So I think this is a reasonable thing to do.

}

private function calculateContent(MappedAsset $asset): string
Expand Down
24 changes: 12 additions & 12 deletions src/Symfony/Component/AssetMapper/MappedAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/
final class MappedAsset
{
public string $publicPath;
private string $publicPath;
private string $publicPathWithoutDigest;
/**
* @var string the filesystem path to the source file
*/
Expand Down Expand Up @@ -88,6 +89,15 @@ public function setPublicPath(string $publicPath): void
$this->publicPath = $publicPath;
}

public function setPublicPathWithoutDigest(string $publicPathWithoutDigest): void
{
if (isset($this->publicPathWithoutDigest)) {
throw new \LogicException('Cannot set public path without digest: it was already set on the asset.');
}

$this->publicPathWithoutDigest = $publicPathWithoutDigest;
}

public function setSourcePath(string $sourcePath): void
{
if (isset($this->sourcePath)) {
Expand Down Expand Up @@ -132,16 +142,6 @@ public function addDependency(self $asset, bool $isLazy = false): void

public function getPublicPathWithoutDigest(): string
{
if ($this->isPredigested()) {
return $this->getPublicPath();
}

// remove last part of publicPath and replace with last part of logicalPath
$publicPathParts = explode('/', $this->getPublicPath());
$logicalPathParts = explode('/', $this->logicalPath);
array_pop($publicPathParts);
$publicPathParts[] = array_pop($logicalPathParts);

return implode('/', $publicPathParts);
return $this->publicPathWithoutDigest;
}
}
3 changes: 3 additions & 0 deletions src/Symfony/Component/AssetMapper/Tests/AssetMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function testGetAsset()
$asset = $assetMapper->getAsset('file2.js');
$this->assertSame('file2.js', $asset->logicalPath);
$this->assertMatchesRegularExpression('/^\/final-assets\/file2-[a-zA-Z0-9]{7,128}\.js$/', $asset->getPublicPath());
$this->assertSame('/final-assets/file2.js', $asset->getPublicPathWithoutDigest());
}

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

public function testGetAssetUsesManifestIfAvailable()
Expand Down
16 changes: 12 additions & 4 deletions src/Symfony/Component/AssetMapper/Tests/MappedAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public function testGetPublicPath()
$this->assertSame('/assets/foo.1234567.css', $asset->getPublicPath());
}

public function testGetPublicPathWithoutDigest()
{
$asset = new MappedAsset('anything');
$asset->setPublicPathWithoutDigest('/assets/foo.css');

$this->assertSame('/assets/foo.css', $asset->getPublicPathWithoutDigest());
}

/**
* @dataProvider getExtensionTests
*/
Expand All @@ -48,29 +56,29 @@ public static function getExtensionTests(): iterable
yield 'with_directory' => ['foo/bar.css', 'css'];
}

public function testGetSourcePath(): void
public function testGetSourcePath()
{
$asset = new MappedAsset('foo.css');
$asset->setSourcePath('/path/to/source.css');
$this->assertSame('/path/to/source.css', $asset->getSourcePath());
}

public function testGetMimeType(): void
public function testGetMimeType()
{
$asset = new MappedAsset('foo.css');
$asset->setMimeType('text/css');
$this->assertSame('text/css', $asset->getMimeType());
}

public function testGetDigest(): void
public function testGetDigest()
{
$asset = new MappedAsset('foo.css');
$asset->setDigest('1234567', false);
$this->assertSame('1234567', $asset->getDigest());
$this->assertFalse($asset->isPredigested());
}

public function testGetContent(): void
public function testGetContent()
{
$asset = new MappedAsset('foo.css');
$asset->setContent('body { color: red; }');
Expand Down