1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- declare(strict_types = 1);
- namespace Fgm\Drupal\Composer;
- use Drupal\Component\Utility\NestedArray;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Yaml\Yaml;
- class MergeParamsCommand extends BaseBuilderCommand
- {
- const DEFAULT_SITE = '_default';
-
- protected $eventName;
-
- public function configure()
- {
- parent::configure();
- $this->eventName = $this->getName();
- $this
- ->setName('build:merge_params')
- ->setDescription('Merges the params.local.yml with dist.params.local.yml.')
- ->setDefinition([])
- ->setHelp(
- <<<EOT
- The build:merge_params command combines shared and per-environment parameters and
- generates merged.params.local.yml containing the merged result.
- EOT
- );
- }
- protected function merge(array $dist, array $local): array
- {
- if (empty($local)) {
- return $dist;
- }
-
- $merged = NestedArray::mergeDeep($dist, $local);
-
- $default = $merged['sites'][static::DEFAULT_SITE] ?? [];
- $sites = array_diff_key($merged['sites'], [static::DEFAULT_SITE => null]);
- $merged['sites'] = [];
- foreach ($sites as $name => $params) {
- $merged['sites'][$name] = NestedArray::mergeDeep($default, $params);
- }
- return $merged;
- }
-
- public function execute(InputInterface $input, OutputInterface $output)
- {
- $settingsPath = $this->getSettingsPath();
- $yaml = new Yaml();
-
- $defaultsPath = "${settingsPath}/dist.params.local.yml";
- $realDefaultsPath = realpath($defaultsPath);
- if (empty($realDefaultsPath)) {
- $output->writeln("Failed to open $defaultsPath");
- return 1;
- }
- $defaults = $yaml->parseFile($realDefaultsPath);
-
- $localPath = "${settingsPath}/params.local.yml";
- $realLocalPath = realpath($localPath);
- if (empty($realLocalPath)) {
- if ($output->isVerbose()) {
- $output->writeln("File $localPath not found, using only defaults");
- }
- $local = [];
- } else {
- $local = $yaml->parseFile($realLocalPath);
- }
-
- $merged = $this->merge($defaults, $local);
-
- $ok = file_put_contents("${settingsPath}/merged.params.local.yml", $yaml->dump($merged, 10, 2));
- if (!$ok) {
- $output->writeln("Failed to write merged params.");
- return 2;
- }
- }
- }
|