Skip to content

Commit 47fcf74

Browse files
authored
Fix #14: Add alternative configurations (environments), add throwing exceptions to Config class (#64)
1 parent ec62ef4 commit 47fcf74

File tree

14 files changed

+833
-285
lines changed

14 files changed

+833
-285
lines changed

src/ComposerConfigProcess.php

+31-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function glob;
1515
use function in_array;
1616
use function is_file;
17+
use function ksort;
1718
use function realpath;
1819
use function str_replace;
1920
use function substr;
@@ -33,7 +34,7 @@ final class ComposerConfigProcess
3334
private array $configFiles = [];
3435

3536
/**
36-
* @psalm-var array<string, array<string, string|list<string>>>
37+
* @psalm-var array<string, array<string, array<string, list<string>>>>
3738
*/
3839
private array $mergePlan = [];
3940

@@ -59,6 +60,7 @@ public function __construct(Composer $composer, array $packagesForCheck, ?bool $
5960

6061
$this->process($rootOptions, $packagesForCheck, $forceCheck);
6162
$this->appendRootPackageConfigToMergePlan($rootPackage, $rootOptions);
63+
$this->appendEnvironmentsToMergePlan($rootPackage);
6264
}
6365

6466
/**
@@ -74,7 +76,7 @@ public function configFiles(): array
7476
/**
7577
* Returns data for changing the merge plan.
7678
*
77-
* @return array Data for changing the merge plan.
79+
* @return array<string, array<string, array<string, list<string>>>> Data for changing the merge plan.
7880
*/
7981
public function mergePlan(): array
8082
{
@@ -119,7 +121,7 @@ private function process(Options $rootOptions, array $packagesForCheck, bool $fo
119121

120122
// Do not copy variables.
121123
if (Options::isVariable($file)) {
122-
$this->mergePlan[$group][$package->getPrettyName()][] = $file;
124+
$this->mergePlan[Options::DEFAULT_ENVIRONMENT][$group][$package->getPrettyName()][] = $file;
123125
continue;
124126
}
125127

@@ -140,7 +142,7 @@ private function process(Options $rootOptions, array $packagesForCheck, bool $fo
140142
}
141143
}
142144

143-
$this->mergePlan[$group][$package->getPrettyName()][] = $file;
145+
$this->mergePlan[Options::DEFAULT_ENVIRONMENT][$group][$package->getPrettyName()][] = $file;
144146
continue;
145147
}
146148

@@ -158,7 +160,7 @@ private function process(Options $rootOptions, array $packagesForCheck, bool $fo
158160
);
159161
}
160162

161-
$this->mergePlan[$group][$package->getPrettyName()][] = $file;
163+
$this->mergePlan[Options::DEFAULT_ENVIRONMENT][$group][$package->getPrettyName()][] = $file;
162164
}
163165
}
164166
}
@@ -186,8 +188,31 @@ private function appendRootPackageConfigToMergePlan(RootPackageInterface $packag
186188
return $result . $file;
187189
}, (array) $files);
188190

191+
$packageGroups = $this->mergePlan[Options::DEFAULT_ENVIRONMENT][$group] ?? [];
189192
/** @psalm-suppress PropertyTypeCoercion */
190-
$this->mergePlan[$group] = ['/' => $files] + ($this->mergePlan[$group] ?? []);
193+
$this->mergePlan[Options::DEFAULT_ENVIRONMENT][$group] = [Options::ROOT_PACKAGE_NAME => $files] + $packageGroups;
194+
ksort($this->mergePlan[Options::DEFAULT_ENVIRONMENT]);
195+
}
196+
}
197+
198+
private function appendEnvironmentsToMergePlan(RootPackageInterface $package): void
199+
{
200+
/** @psalm-var array<string, string|array<string, string|list<string>>> $environments */
201+
$environments = (array) ($package->getExtra()['config-plugin-environments'] ?? []);
202+
203+
foreach ($environments as $environment => $groups) {
204+
if ($environment === Options::DEFAULT_ENVIRONMENT) {
205+
continue;
206+
}
207+
208+
foreach ((array) $groups as $group => $files) {
209+
/** @psalm-suppress InvalidPropertyAssignmentValue */
210+
$this->mergePlan[$environment][$group][Options::ROOT_PACKAGE_NAME] = (array) $files;
211+
}
212+
213+
if (isset($this->mergePlan[$environment])) {
214+
ksort($this->mergePlan[$environment]);
215+
}
191216
}
192217
}
193218

0 commit comments

Comments
 (0)