BuildSettingsCommand.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * The event triggering this command.
  16. *
  17. * @var string
  18. */
  19. protected $eventName;
  20. /**
  21. * Configures the current command.
  22. */
  23. public function configure() {
  24. parent::configure();
  25. $this->eventName = $this->getName();
  26. $this
  27. ->setName('build:settings')
  28. ->setDescription('Step 4: build the *.settings.local.php files.')
  29. ->setDefinition(
  30. new InputDefinition([
  31. new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL),
  32. ])
  33. )
  34. ->setHelp(
  35. <<<EOT
  36. The build:settings command combines shared and per-environment parameters and passes
  37. them to the settings.local.php.twig template to build the settings/(build|run).settings.local.php files.
  38. EOT
  39. );
  40. }
  41. /**
  42. * Executes the current command.
  43. *
  44. * {@inheritDoc}
  45. */
  46. public function execute(InputInterface $input, OutputInterface $output): int {
  47. [$wrapper, $params, $msg, $err] = $this->prepare($input, $output);
  48. if ($err != 0) {
  49. $output->writeln($msg);
  50. return $err;
  51. }
  52. foreach ($params['sites'] as $name => $siteParams) {
  53. foreach (['build', 'run'] as $stage) {
  54. $stageSettings = NestedArray::mergeDeepArray([
  55. $siteParams['settings']['both'] ?? [],
  56. $siteParams['settings'][$stage] ?? [],
  57. ], TRUE);
  58. $stageParams = $siteParams;
  59. $stageParams['settings'] = $stageSettings;
  60. $context = [
  61. 'instance' => $params['instance'],
  62. 'name' => $name,
  63. 'stage' => $stage,
  64. 'site' => $stageParams,
  65. ];
  66. $destination = "web/sites/${context['name']}/${context['stage']}.settings.local.php";
  67. [$msg, $error] = $this->render($wrapper, $context, $destination);
  68. if ($error) {
  69. $output->writeln(sprintf("Failed rendering %s settings for site %s: %s", $stage, $name, $msg));
  70. return 5;
  71. }
  72. }
  73. }
  74. return 0;
  75. }
  76. }