123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- declare(strict_types = 1);
- namespace Fgm\Drupal\Composer;
- 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('Step 1: merges the params.local.yml with dist.params.local.yml.')
- ->setDefinition([])
- ->setHelp(
- <<<EOT
- Step 1 of a build, 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 = static::mergeDeepArray([$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] = static::mergeDeepArray([$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;
- }
- return 0;
- }
-
- public static function mergeDeepArray(array $arrays) {
- $result = [];
- foreach ($arrays as $array) {
- foreach ($array as $key => $value) {
-
-
- if ($value === NULL) {
- unset($result[$key]);
- continue;
- }
-
-
-
- if (is_int($key)) {
-
-
- if ($key === 0) {
- $result = [];
- }
- $result[] = $value;
- }
-
- elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
- $result[$key] = static::mergeDeepArray([$result[$key], $value]);
- }
-
- else {
- $result[$key] = $value;
- }
- }
- }
- return $result;
- }
- }
|