Skip to content

Commit c04aa44

Browse files
author
Serhii Balko
committed
Merge remote-tracking branch 'origin/MC-42158' into 2.4-develop-pr61
2 parents bd04039 + 344d7ed commit c04aa44

File tree

1 file changed

+33
-14
lines changed
  • lib/internal/Magento/Framework/Module/ModuleList

1 file changed

+33
-14
lines changed

lib/internal/Magento/Framework/Module/ModuleList/Loader.php

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ private function sortBySequence(array $origList): array
133133
{
134134
ksort($origList);
135135
$modules = $this->prearrangeModules($origList);
136-
136+
$sequenceCache = [];
137137
$expanded = [];
138138
foreach (array_keys($modules) as $moduleName) {
139-
$sequence = $this->expandSequence($origList, $moduleName);
139+
$sequence = $this->expandSequence($origList, $moduleName, $sequenceCache);
140140
asort($sequence);
141141

142142
$expanded[] = [
@@ -189,27 +189,46 @@ private function prearrangeModules(array $modules): array
189189
/**
190190
* Accumulate information about all transitive "sequence" references
191191
*
192+
* Added a sequence cache to avoid re-computing the sequences of dependencies over and over again.
193+
*
192194
* @param array $list
193195
* @param string $name
196+
* @param array $sequenceCache
194197
* @param array $accumulated
198+
* @param string $parentName
195199
* @return array
196200
* @throws \Exception
197201
*/
198-
private function expandSequence($list, $name, $accumulated = [])
199-
{
202+
private function expandSequence(
203+
array $list,
204+
string $name,
205+
array& $sequenceCache,
206+
array $accumulated = [],
207+
string $parentName = ''
208+
) {
209+
// Making sure we haven't already called the method for this module higher in the stack
210+
if (isset($accumulated[$name])) {
211+
throw new \LogicException("Circular sequence reference from '{$parentName}' to '{$name}'.");
212+
}
200213
$accumulated[$name] = true;
201-
$result = $list[$name]['sequence'];
202-
$allResults = [];
203-
foreach ($result as $relatedName) {
204-
if (isset($accumulated[$relatedName])) {
205-
throw new \LogicException("Circular sequence reference from '{$name}' to '{$relatedName}'.");
214+
215+
// Checking if we already computed the full sequence for this module
216+
if (!isset($sequenceCache[$name])) {
217+
$sequence = $list[$name]['sequence'] ?? [];
218+
$allSequences = [];
219+
// Going over all immediate dependencies to gather theirs recursively
220+
foreach ($sequence as $relatedName) {
221+
$relatedSequence = $this->expandSequence($list, $relatedName, $sequenceCache, $accumulated, $name);
222+
$allSequences[] = $relatedSequence;
206223
}
207-
if (!isset($list[$relatedName])) {
208-
continue;
224+
$allSequences[] = $sequence;
225+
226+
// Caching the full sequence list
227+
if (!empty($allSequences)) {
228+
$sequenceCache[$name] = array_unique(array_merge(...$allSequences));
209229
}
210-
$allResults[] = $this->expandSequence($list, $relatedName, $accumulated);
211230
}
212-
$allResults[] = $result;
213-
return array_unique(array_merge([], ...$allResults));
231+
232+
return $sequenceCache[$name] ?? [];
214233
}
215234
}

0 commit comments

Comments
 (0)