MergeParamsCommand.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Fgm\Drupal\Composer;
  4. use Drupal\Component\Utility\NestedArray;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Yaml\Yaml;
  8. class MergeParamsCommand extends BaseBuilderCommand
  9. {
  10. const DEFAULT_SITE = '_default';
  11. /**
  12. * @var string
  13. */
  14. protected $eventName;
  15. /**
  16. * {@inheritDoc}
  17. */
  18. public function configure()
  19. {
  20. parent::configure();
  21. $this->eventName = $this->getName();
  22. $this
  23. ->setName('build:merge_params')
  24. ->setDescription('Step 1: merges the params.local.yml with dist.params.local.yml.')
  25. ->setDefinition([])
  26. ->setHelp(
  27. <<<EOT
  28. Step 1 of a build, the build:merge_params command combines shared and per-environment
  29. parameters and generates merged.params.local.yml containing the merged result.
  30. EOT
  31. );
  32. }
  33. protected function merge(array $dist, array $local): array
  34. {
  35. if (empty($local)) {
  36. return $dist;
  37. }
  38. // Merge local into defaults.
  39. $merged = NestedArray::mergeDeepArray([$dist, $local], true);
  40. // Generate per-site data from settings/_default.
  41. $default = $merged['sites'][static::DEFAULT_SITE] ?? [];
  42. $sites = array_diff_key($merged['sites'], [static::DEFAULT_SITE => null]);
  43. $merged['sites'] = [];
  44. foreach ($sites as $name => $params) {
  45. $merged['sites'][$name] = NestedArray::mergeDeep($default, $params);
  46. }
  47. return $merged;
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function execute(InputInterface $input, OutputInterface $output)
  53. {
  54. $settingsPath = $this->getSettingsPath();
  55. $yaml = new Yaml();
  56. // Load defaults.
  57. $defaultsPath = "${settingsPath}/dist.params.local.yml";
  58. $realDefaultsPath = realpath($defaultsPath);
  59. if (empty($realDefaultsPath)) {
  60. $output->writeln("Failed to open $defaultsPath");
  61. return 1;
  62. }
  63. $defaults = $yaml->parseFile($realDefaultsPath);
  64. // Load local.
  65. $localPath = "${settingsPath}/params.local.yml";
  66. $realLocalPath = realpath($localPath);
  67. if (empty($realLocalPath)) {
  68. if ($output->isVerbose()) {
  69. $output->writeln("File $localPath not found, using only defaults");
  70. }
  71. $local = [];
  72. } else {
  73. $local = $yaml->parseFile($realLocalPath);
  74. }
  75. // Merge.
  76. $merged = $this->merge($defaults, $local);
  77. // Write.
  78. $ok = file_put_contents("${settingsPath}/merged.params.local.yml", $yaml->dump($merged, 10, 2));
  79. if (!$ok) {
  80. $output->writeln("Failed to write merged params.");
  81. return 2;
  82. }
  83. }
  84. }