BuildSettingsCommand.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Fgm\Drupal\Composer;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputDefinition;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. /**
  9. * Build the build- and run-time settings for all sites in the multisite.
  10. *
  11. * @package Fgm\Drupal\Composer
  12. */
  13. class BuildSettingsCommand extends BaseBuilderCommand {
  14. /**
  15. * Machine name of current building process.
  16. */
  17. const BUILD_NAME = 'settings';
  18. /**
  19. * The event triggering this command.
  20. *
  21. * @var string
  22. */
  23. protected $eventName;
  24. /**
  25. * Configures the current command.
  26. */
  27. public function configure() {
  28. parent::configure();
  29. $this->eventName = $this->getName();
  30. $this
  31. ->setName('build:settings')
  32. ->setDescription('Step 4: build the *.settings.local.php files.')
  33. ->setDefinition(
  34. new InputDefinition([
  35. new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL, '', static::BUILD_NAME),
  36. ])
  37. )
  38. ->setHelp(
  39. <<<EOT
  40. The build:settings command combines shared and per-environment parameters and passes
  41. them to the settings.local.php.twig template to build the settings/(build|run).settings.local.php files.
  42. EOT
  43. );
  44. }
  45. /**
  46. * Executes the current command.
  47. *
  48. * {@inheritDoc}
  49. */
  50. public function execute(InputInterface $input, OutputInterface $output): int {
  51. [$wrapper, $params, $msg, $err] = $this->prepare($input, $output, static::BUILD_NAME);
  52. if ($err != 0) {
  53. $output->writeln($msg);
  54. return $err;
  55. }
  56. foreach ($params['sites'] as $name => $siteParams) {
  57. foreach (['build', 'run'] as $stage) {
  58. $stageSettings = MergeParamsCommand::mergeDeepArray([
  59. $siteParams['settings']['both'] ?? [],
  60. $siteParams['settings'][$stage] ?? [],
  61. ]);
  62. $stageParams = $siteParams;
  63. $stageParams['settings'] = $stageSettings;
  64. $context = [
  65. 'extra' => array_diff_key($params, ['instance' => 0, 'sites' => 0]),
  66. 'instance' => $params['instance'],
  67. 'name' => $name,
  68. 'stage' => $stage,
  69. 'site' => $stageParams,
  70. ];
  71. $destination = "web/sites/${context['name']}/${context['stage']}.settings.local.php";
  72. echo "Building $destination\n";
  73. [$msg, $error] = $this->render($wrapper, $context, $destination);
  74. if ($error) {
  75. $output->writeln(sprintf("Failed rendering %s settings for site %s: %s", $stage, $name, $msg));
  76. return 5;
  77. }
  78. }
  79. }
  80. return 0;
  81. }
  82. }