Browse Source

Multi-stage settings improvements. Support arrays, preserving numeric keys.

Frederic G. MARAND 4 years ago
parent
commit
e85b2c72d1
2 changed files with 35 additions and 12 deletions
  1. 34 11
      src/BuildSettingsCommand.php
  2. 1 1
      src/MergeParamsCommand.php

+ 34 - 11
src/BuildSettingsCommand.php

@@ -3,6 +3,7 @@ declare(strict_types = 1);
 
 namespace Fgm\Drupal\Composer;
 
+use Drupal\Component\Utility\NestedArray;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputDefinition;
 use Symfony\Component\Console\Input\InputInterface;
@@ -56,16 +57,10 @@ EOT
      */
     public function execute(InputInterface $input, OutputInterface $output)
     {
-        $file = $input->getArgument(static::ARG_FILE);
-        $conf = $this->getBuilderConfig();
-        $templateName = $conf['templates'][$file] ?? '';
-        if (empty($templateName)) {
-            $output->writeln(sprintf(
-                'Could not build file %s: no such template in composer.json extra section',
-                $file
-            ));
-            return 1;
-        }
+        [$templateName, $err] = $this->validateTemplateName($input, $output);
+        if ($err) {
+          return 1;
+        };
 
         $settingsPath = $this->getSettingsPath();
         $templatePath = "${settingsPath}/${templateName}";
@@ -102,11 +97,17 @@ EOT
         $wrapper = $twig->load($templateName);
         foreach ($params['sites'] as $name => $siteParams) {
             foreach (['build', 'run'] as $stage) {
+                $stageSettings = NestedArray::mergeDeepArray([
+                  $siteParams['settings']['both'] ?? [],
+                  $siteParams['settings'][$stage] ?? []
+                ], true);
+                $stageParams = $siteParams;
+                $stageParams['settings'] = $stageSettings;
                 $context = [
                   'instance' => $params['instance'],
                   'name' => $name,
                   'stage' => $stage,
-                  'site' => $siteParams,
+                  'site' => $stageParams,
                 ];
                 $error = $this->render($wrapper, $context, $output);
                 if ($error) {
@@ -140,4 +141,26 @@ EOT
 
         return 0;
     }
+
+  /**
+   * @param \Symfony\Component\Console\Input\InputInterface $input
+   * @param \Symfony\Component\Console\Output\OutputInterface $output
+   *
+   * @return array
+   *   - string Template name
+   *   - int Error
+   */
+  protected function validateTemplateName(InputInterface $input, OutputInterface $output): array {
+    $file = $input->getArgument(static::ARG_FILE);
+    $conf = $this->getBuilderConfig();
+    $templateName = $conf['templates'][$file] ?? '';
+    if (empty($templateName)) {
+      $output->writeln(sprintf(
+        'Could not build file %s: no such template in composer.json extra section',
+        $file
+      ));
+      return ["", 1];
+    }
+    return [$templateName, 0];
+  }
 }

+ 1 - 1
src/MergeParamsCommand.php

@@ -44,7 +44,7 @@ EOT
         }
 
         // Merge local into defaults.
-        $merged = NestedArray::mergeDeep($dist, $local);
+        $merged = NestedArray::mergeDeepArray([$dist, $local], true);
 
         // Generate per-site data from settings/_default.
         $default = $merged['sites'][static::DEFAULT_SITE] ?? [];