From c3a0f5e0c154a565cb5665e8c7c9e3f84549d97c Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Wed, 10 May 2023 10:34:26 -0400 Subject: [PATCH] [AssetMapper] Normalizing logicalPath to a getter like all other properties --- src/Symfony/Component/AssetMapper/AssetMapper.php | 8 ++++---- .../AssetMapper/Command/AssetMapperCompileCommand.php | 2 +- .../AssetMapper/Command/DebugAssetMapperCommand.php | 2 +- .../AssetMapper/Compiler/CssAssetUrlCompiler.php | 2 +- .../AssetMapper/Compiler/JavaScriptImportPathCompiler.php | 2 +- .../AssetMapper/Compiler/SourceMappingUrlsCompiler.php | 2 +- .../Component/AssetMapper/ImportMap/ImportMapManager.php | 4 ++-- src/Symfony/Component/AssetMapper/MappedAsset.php | 7 ++++++- .../Component/AssetMapper/Tests/AssetMapperTest.php | 8 ++++---- .../Tests/Compiler/CssAssetUrlCompilerTest.php | 2 +- .../Tests/Compiler/JavaScriptImportPathCompilerTest.php | 2 +- .../Tests/Compiler/SourceMappingUrlsCompilerTest.php | 2 +- .../Component/AssetMapper/Tests/MappedAssetTest.php | 2 +- 13 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/Symfony/Component/AssetMapper/AssetMapper.php b/src/Symfony/Component/AssetMapper/AssetMapper.php index 5da130fb7ab52..0e735fb2c089f 100644 --- a/src/Symfony/Component/AssetMapper/AssetMapper.php +++ b/src/Symfony/Component/AssetMapper/AssetMapper.php @@ -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]; } @@ -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; } diff --git a/src/Symfony/Component/AssetMapper/Command/AssetMapperCompileCommand.php b/src/Symfony/Component/AssetMapper/Command/AssetMapperCompileCommand.php index da4f7ea37fefb..355f465deb1e0 100644 --- a/src/Symfony/Component/AssetMapper/Command/AssetMapperCompileCommand.php +++ b/src/Symfony/Component/AssetMapper/Command/AssetMapperCompileCommand.php @@ -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); diff --git a/src/Symfony/Component/AssetMapper/Command/DebugAssetMapperCommand.php b/src/Symfony/Component/AssetMapper/Command/DebugAssetMapperCommand.php index 54b2e7e98038d..ac5e9e2799b5c 100644 --- a/src/Symfony/Component/AssetMapper/Command/DebugAssetMapperCommand.php +++ b/src/Symfony/Component/AssetMapper/Command/DebugAssetMapperCommand.php @@ -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')) { diff --git a/src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php b/src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php index 6ba267adfdd65..6c342ca295074 100644 --- a/src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php +++ b/src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php @@ -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) { diff --git a/src/Symfony/Component/AssetMapper/Compiler/JavaScriptImportPathCompiler.php b/src/Symfony/Component/AssetMapper/Compiler/JavaScriptImportPathCompiler.php index b5287e8929480..e8c94c9e1dd18 100644 --- a/src/Symfony/Component/AssetMapper/Compiler/JavaScriptImportPathCompiler.php +++ b/src/Symfony/Component/AssetMapper/Compiler/JavaScriptImportPathCompiler.php @@ -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); diff --git a/src/Symfony/Component/AssetMapper/Compiler/SourceMappingUrlsCompiler.php b/src/Symfony/Component/AssetMapper/Compiler/SourceMappingUrlsCompiler.php index 663934cc5e165..5442c346d5adc 100644 --- a/src/Symfony/Component/AssetMapper/Compiler/SourceMappingUrlsCompiler.php +++ b/src/Symfony/Component/AssetMapper/Compiler/SourceMappingUrlsCompiler.php @@ -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) { diff --git a/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php b/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php index 23ded1b0a59bd..3a72d58b90707 100644 --- a/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php +++ b/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php @@ -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); @@ -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); diff --git a/src/Symfony/Component/AssetMapper/MappedAsset.php b/src/Symfony/Component/AssetMapper/MappedAsset.php index e92162c50f6c8..3d7ff98ad6d62 100644 --- a/src/Symfony/Component/AssetMapper/MappedAsset.php +++ b/src/Symfony/Component/AssetMapper/MappedAsset.php @@ -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; diff --git a/src/Symfony/Component/AssetMapper/Tests/AssetMapperTest.php b/src/Symfony/Component/AssetMapper/Tests/AssetMapperTest.php index 9ef24bdd64528..0f098ddb96297 100644 --- a/src/Symfony/Component/AssetMapper/Tests/AssetMapperTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/AssetMapperTest.php @@ -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()); } @@ -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()); @@ -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() @@ -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 */'; } diff --git a/src/Symfony/Component/AssetMapper/Tests/Compiler/CssAssetUrlCompilerTest.php b/src/Symfony/Component/AssetMapper/Tests/Compiler/CssAssetUrlCompilerTest.php index ee9485798a3d0..b284a34cd1b78 100644 --- a/src/Symfony/Component/AssetMapper/Tests/Compiler/CssAssetUrlCompilerTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/Compiler/CssAssetUrlCompilerTest.php @@ -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); } diff --git a/src/Symfony/Component/AssetMapper/Tests/Compiler/JavaScriptImportPathCompilerTest.php b/src/Symfony/Component/AssetMapper/Tests/Compiler/JavaScriptImportPathCompilerTest.php index 06a0c791dc13a..3dfa8fa99c319 100644 --- a/src/Symfony/Component/AssetMapper/Tests/Compiler/JavaScriptImportPathCompilerTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/Compiler/JavaScriptImportPathCompilerTest.php @@ -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); } diff --git a/src/Symfony/Component/AssetMapper/Tests/Compiler/SourceMappingUrlsCompilerTest.php b/src/Symfony/Component/AssetMapper/Tests/Compiler/SourceMappingUrlsCompilerTest.php index d2b8515ef2515..fa3cce9d8094c 100644 --- a/src/Symfony/Component/AssetMapper/Tests/Compiler/SourceMappingUrlsCompilerTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/Compiler/SourceMappingUrlsCompilerTest.php @@ -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); } diff --git a/src/Symfony/Component/AssetMapper/Tests/MappedAssetTest.php b/src/Symfony/Component/AssetMapper/Tests/MappedAssetTest.php index 2828c30c470b8..2cd0bc733ba1c 100644 --- a/src/Symfony/Component/AssetMapper/Tests/MappedAssetTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/MappedAssetTest.php @@ -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()