Skip to content

[AssetMapper] Normalizing logicalPath to a getter like all other properties #50294

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
8 changes: 4 additions & 4 deletions src/Symfony/Component/AssetMapper/AssetMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function getPublicPath(string $logicalPath): ?string
private function getDigest(MappedAsset $asset): array
{
// check for a pre-digested file
if (1 === preg_match(self::PREDIGESTED_REGEX, $asset->logicalPath, $matches)) {
if (1 === preg_match(self::PREDIGESTED_REGEX, $asset->getLogicalPath(), $matches)) {
return [$matches[1], true];
}

Expand All @@ -143,14 +143,14 @@ private function getDigest(MappedAsset $asset): array

private function calculateContent(MappedAsset $asset): string
{
if (isset($this->fileContentsCache[$asset->logicalPath])) {
return $this->fileContentsCache[$asset->logicalPath];
if (isset($this->fileContentsCache[$asset->getLogicalPath()])) {
return $this->fileContentsCache[$asset->getLogicalPath()];
}

$content = file_get_contents($asset->getSourcePath());
$content = $this->compiler->compile($content, $asset, $this);

$this->fileContentsCache[$asset->logicalPath] = $content;
$this->fileContentsCache[$asset->getLogicalPath()] = $content;

return $content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private function createManifestAndWriteFiles(SymfonyStyle $io, string $publicDir
}

$this->filesystem->dumpFile($targetPath, $asset->getContent());
$manifest[$asset->logicalPath] = $asset->getPublicPath();
$manifest[$asset->getLogicalPath()] = $asset->getPublicPath();
}
ksort($manifest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$rows = [];
foreach ($allAssets as $asset) {
$logicalPath = $asset->logicalPath;
$logicalPath = $asset->getLogicalPath();
$sourcePath = $this->relativizePath($asset->getSourcePath());

if (!$input->getOption('full')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(private readonly bool $strictMode = true)
public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string
{
return preg_replace_callback(self::ASSET_URL_PATTERN, function ($matches) use ($asset, $assetMapper) {
$resolvedPath = $this->resolvePath(\dirname($asset->logicalPath), $matches[1]);
$resolvedPath = $this->resolvePath(\dirname($asset->getLogicalPath()), $matches[1]);
$dependentAsset = $assetMapper->getAsset($resolvedPath);

if (null === $dependentAsset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(private readonly bool $strictMode = true)
public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string
{
return preg_replace_callback(self::IMPORT_PATTERN, function ($matches) use ($asset, $assetMapper) {
$resolvedPath = $this->resolvePath(\dirname($asset->logicalPath), $matches[1]);
$resolvedPath = $this->resolvePath(\dirname($asset->getLogicalPath()), $matches[1]);

$dependentAsset = $assetMapper->getAsset($resolvedPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function supports(MappedAsset $asset): bool
public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string
{
return preg_replace_callback(self::SOURCE_MAPPING_PATTERN, function ($matches) use ($asset, $assetMapper) {
$resolvedPath = $this->resolvePath(\dirname($asset->logicalPath), $matches[2]);
$resolvedPath = $this->resolvePath(\dirname($asset->getLogicalPath()), $matches[2]);

$dependentAsset = $assetMapper->getAsset($resolvedPath);
if (!$dependentAsset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ private function requirePackages(array $packagesToRequire, array &$importMapEntr

throw new \LogicException(sprintf('The package was downloaded to "%s", but this path does not appear to be in any of your asset paths.', $vendorPath));
}
$path = $mappedAsset->logicalPath;
$path = $mappedAsset->getLogicalPath();
}

$newEntry = new ImportMapEntry($importName, $path, $url, $download, $preload);
Expand Down Expand Up @@ -395,7 +395,7 @@ private function convertEntriesToImports(array $entries): array
$dependencyImportMapEntries = array_map(function (AssetDependency $dependency) {
return new ImportMapEntry(
$dependency->asset->getPublicPathWithoutDigest(),
$dependency->asset->logicalPath,
$dependency->asset->getLogicalPath(),
preload: !$dependency->isLazy,
);
}, $dependencies);
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/AssetMapper/MappedAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ final class MappedAsset
/** @var AssetDependency[] */
private array $dependencies = [];

public function __construct(public readonly string $logicalPath)
public function __construct(private readonly string $logicalPath)
{
}

public function getLogicalPath(): string
{
return $this->logicalPath;
}

public function getPublicPath(): string
{
return $this->publicPath;
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/AssetMapper/Tests/AssetMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testGetAsset()
$this->assertNull($assetMapper->getAsset('non-existent.js'));

$asset = $assetMapper->getAsset('file2.js');
$this->assertSame('file2.js', $asset->logicalPath);
$this->assertSame('file2.js', $asset->getLogicalPath());
$this->assertMatchesRegularExpression('/^\/final-assets\/file2-[a-zA-Z0-9]{7,128}\.js$/', $asset->getPublicPath());
$this->assertSame('/final-assets/file2.js', $asset->getPublicPathWithoutDigest());
}
Expand All @@ -39,7 +39,7 @@ public function testGetAssetRespectsPreDigestedPaths()
{
$assetMapper = $this->createAssetMapper();
$asset = $assetMapper->getAsset('already-abcdefVWXYZ0123456789.digested.css');
$this->assertSame('already-abcdefVWXYZ0123456789.digested.css', $asset->logicalPath);
$this->assertSame('already-abcdefVWXYZ0123456789.digested.css', $asset->getLogicalPath());
$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());
Expand Down Expand Up @@ -73,7 +73,7 @@ public function testGetAssetFromFilesystemPath()
{
$assetMapper = $this->createAssetMapper();
$asset = $assetMapper->getAssetFromSourcePath(__DIR__.'/fixtures/dir1/file1.css');
$this->assertSame('file1.css', $asset->logicalPath);
$this->assertSame('file1.css', $asset->getLogicalPath());
}

public function testGetAssetWithContentBasic()
Expand Down Expand Up @@ -124,7 +124,7 @@ public function supports(MappedAsset $asset): bool

public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string
{
if ('subdir/file6.js' === $asset->logicalPath) {
if ('subdir/file6.js' === $asset->getLogicalPath()) {
return $content.'/* compiled */';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testCompile(string $sourceLogicalName, string $input, string $ex
$asset = new MappedAsset($sourceLogicalName);
$asset->setPublicPathWithoutDigest('/assets/'.$sourceLogicalName);
$this->assertSame($expectedOutput, $compiler->compile($input, $asset, $this->createAssetMapper()));
$assetDependencyLogicalPaths = array_map(fn (AssetDependency $dependency) => $dependency->asset->logicalPath, $asset->getDependencies());
$assetDependencyLogicalPaths = array_map(fn (AssetDependency $dependency) => $dependency->asset->getLogicalPath(), $asset->getDependencies());
$this->assertSame($expectedDependencies, $assetDependencyLogicalPaths);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testCompile(string $sourceLogicalName, string $input, array $exp
$this->assertSame($input, $compiler->compile($input, $asset, $this->createAssetMapper()));
$actualDependencies = [];
foreach ($asset->getDependencies() as $dependency) {
$actualDependencies[$dependency->asset->logicalPath] = $dependency->isLazy;
$actualDependencies[$dependency->asset->getLogicalPath()] = $dependency->isLazy;
}
$this->assertEquals($expectedDependencies, $actualDependencies);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testCompile(string $sourceLogicalName, string $input, string $ex
$asset = new MappedAsset($sourceLogicalName);
$asset->setPublicPathWithoutDigest('/assets/'.$sourceLogicalName);
$this->assertSame($expectedOutput, $compiler->compile($input, $asset, $assetMapper));
$assetDependencyLogicalPaths = array_map(fn (AssetDependency $dependency) => $dependency->asset->logicalPath, $asset->getDependencies());
$assetDependencyLogicalPaths = array_map(fn (AssetDependency $dependency) => $dependency->asset->getLogicalPath(), $asset->getDependencies());
$this->assertSame($expectedDependencies, $assetDependencyLogicalPaths);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testGetLogicalPath()
{
$asset = new MappedAsset('foo.css');

$this->assertSame('foo.css', $asset->logicalPath);
$this->assertSame('foo.css', $asset->getLogicalPath());
}

public function testGetPublicPath()
Expand Down