Skip to content

Commit bac4f07

Browse files
committed
Optimize traversal while avoiding duplicates
1 parent eb43128 commit bac4f07

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/Symfony/Component/AssetMapper/ImportMap/ImportMapGenerator.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,28 @@ private function findAsset(string $path): ?MappedAsset
225225
private function findEagerImports(MappedAsset $asset): array
226226
{
227227
$dependencies = [];
228-
foreach ($asset->getJavaScriptImports() as $javaScriptImport) {
229-
if ($javaScriptImport->isLazy) {
230-
continue;
231-
}
228+
$queue = [$asset];
232229

233-
$dependencies[] = $javaScriptImport->importName;
230+
while ($asset = array_shift($queue)) {
231+
foreach ($asset->getJavaScriptImports() as $javaScriptImport) {
232+
if ($javaScriptImport->isLazy) {
233+
continue;
234+
}
235+
if (isset($dependencies[$javaScriptImport->importName])) {
236+
continue;
237+
}
238+
$dependencies[$javaScriptImport->importName] = true;
234239

235-
// Follow its imports!
236-
if (!$nextAsset = $this->assetMapper->getAsset($javaScriptImport->assetLogicalPath)) {
237-
// should not happen at this point, unless something added a bogus JavaScriptImport to this asset
238-
throw new LogicException(sprintf('Cannot find imported JavaScript asset "%s" in asset mapper.', $javaScriptImport->assetLogicalPath));
240+
// Follow its imports!
241+
if (!$javaScriptAsset = $this->assetMapper->getAsset($javaScriptImport->assetLogicalPath)) {
242+
// should not happen at this point, unless something added a bogus JavaScriptImport to this asset
243+
throw new LogicException(sprintf('Cannot find JavaScript asset "%s" (imported in "%s") in asset mapper.', $javaScriptImport->assetLogicalPath, $asset->logicalPath));
244+
}
245+
$queue[] = $javaScriptAsset;
239246
}
240-
241-
$dependencies = array_merge($dependencies, $this->findEagerImports($nextAsset));
242247
}
243248

244-
return array_unique($dependencies);
249+
return array_keys($dependencies);
245250
}
246251

247252
private function createMissingImportMapAssetException(ImportMapEntry $entry): \InvalidArgumentException

src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ public function getEagerEntrypointImportsTests(): iterable
687687
new JavaScriptImport('/assets/imports_simple2.js', assetLogicalPath: $importsSimpleAsset2->logicalPath, assetSourcePath: $importsSimpleAsset2->sourcePath, isLazy: false),
688688
]
689689
),
690-
['/assets/imports_simple.js', '/assets/simple.js', '/assets/imports_simple2.js'],
690+
['/assets/imports_simple.js', '/assets/imports_simple2.js', '/assets/simple.js'],
691691
[$simpleAsset, $importsSimpleAsset, $importsSimpleAsset2],
692692
];
693693
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy