Skip to content

Commit

Permalink
Expand variables
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Dec 7, 2023
1 parent 2b55f90 commit 6dc1132
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
62 changes: 61 additions & 1 deletion src/Composer/MergePlanCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Yiisoft\Config\MergePlan;
use Yiisoft\Config\Options;

use function in_array;

/**
* @internal
*
Expand Down Expand Up @@ -96,6 +98,64 @@ public function getGroup(string $group, string $environment = Options::DEFAULT_E
*/
public function generate(): array
{
return $this->mergePlan;
$result = [];
foreach ($this->mergePlan as $environment => $groups) {
if ($environment === Options::DEFAULT_ENVIRONMENT) {
$result[$environment] = [];
foreach ($groups as $group => $packages) {
$result[$environment][$group] = $this->expandVariablesInPackages($packages, $groups);
}
} else {
$result[$environment] = $groups;
}
}
return $result;
}

/**
* @psalm-param array<string, string[]> $packages
* @psalm-param array<string, array<string, string[]>> $groups
* @psalm-return array<string, string[]>
*/
private function expandVariablesInPackages(array $packages, array $groups, ?string $group = null): array
{
if ($group !== null) {
$groupPackages = $this->expandVariablesInPackages($groups[$group], $groups);
foreach ($groupPackages as $groupPackage => $groupItems) {
$variable = '$' . $group;
$packageItems = $packages[$groupPackage] ?? [];
$packages[$groupPackage] = in_array($variable, $packageItems, true)
? $this->replaceVariableToFiles($packageItems, $variable, $groupItems)
: array_merge($packageItems, $groupItems);
}
}

foreach ($packages as $items) {
foreach ($items as $item) {
if (Options::isVariable($item)) {
return $this->expandVariablesInPackages($packages, $groups, substr($item, 1));
}
}
}
return $packages;
}

/**
* @param string[] $items
* @param string $variable
* @param string[] $files
* @return string[]
*/
private function replaceVariableToFiles(array $items, string $variable, array $files): array
{
$result = [];
foreach ($items as $item) {
if ($item === $variable) {
$result = array_merge($result, $files);

Check warning on line 154 in src/Composer/MergePlanCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $result = []; foreach ($items as $item) { if ($item === $variable) { - $result = array_merge($result, $files); + $result = $files; } else { $result[] = $item; }

Check warning on line 154 in src/Composer/MergePlanCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ $result = []; foreach ($items as $item) { if ($item === $variable) { - $result = array_merge($result, $files); + $result = $files; } else { $result[] = $item; }
} else {
$result[] = $item;
}
}
return $result;
}
}
6 changes: 4 additions & 2 deletions tests/Composer/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ protected function assertMergePlan(array $environments = []): void
],
'test/custom-source' => [
'custom-dir/web.php',
'custom-dir/common/a.php',
'custom-dir/common/b.php',
],
'test/d-dev-c' => [
'config/web.php',
Expand All @@ -113,7 +115,7 @@ protected function assertMergePlan(array $environments = []): void
'test/over/web.php',
],
Options::ROOT_PACKAGE_NAME => [
'$common',
'common/*.php',
'web.php',
],
],
Expand All @@ -133,7 +135,7 @@ protected function assertMergePlan(array $environments = []): void
],
'events-web' => [
'test/custom-source' => [
'$events',
'custom-dir/events.php',
'custom-dir/events-web.php',
],
],
Expand Down

0 comments on commit 6dc1132

Please sign in to comment.