| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | <?phpdeclare(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';    /**   * @var string   */    protected $eventName;    /**   * {@inheritDoc}   */    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(                <<<EOTThe build:merge_params command combines shared and per-environment parameters andgenerates merged.params.local.yml containing the merged result.EOT            );    }    protected function merge(array $dist, array $local): array    {        if (empty($local)) {            return $dist;        }        // Merge local into defaults.        $merged = NestedArray::mergeDeep($dist, $local);        // Generate per-site data from settings/_default.        $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;    }    /**   * {@inheritDoc}   */    public function execute(InputInterface $input, OutputInterface $output)    {        $settingsPath = $this->getSettingsPath();        $yaml = new Yaml();        // Load defaults.        $defaultsPath = "${settingsPath}/dist.params.local.yml";        $realDefaultsPath = realpath($defaultsPath);        if (empty($realDefaultsPath)) {            $output->writeln("Failed to open $defaultsPath");            return 1;        }        $defaults = $yaml->parseFile($realDefaultsPath);        // Load local.        $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);        }        // Merge.        $merged = $this->merge($defaults, $local);        // Write.        $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;        }    }}
 |