|
@@ -0,0 +1,97 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+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(
|
|
|
+ <<<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;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 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 = getcwd() . "/settings/";
|
|
|
+ $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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|