BuildServicesCommand.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Fgm\Drupal\Composer;
  3. use Symfony\Component\Console\Input\InputArgument;
  4. use Symfony\Component\Console\Input\InputDefinition;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Yaml\Yaml;
  8. /**
  9. * Build the services.yml file for all sites in the multisite.
  10. *
  11. * @package Fgm\Drupal\Composer
  12. */
  13. class BuildServicesCommand extends BaseBuilderCommand {
  14. /**
  15. * Machine name of current building process.
  16. *
  17. * Used as file name if none provided.
  18. */
  19. const BUILD_NAME = 'services';
  20. /**
  21. * The event triggering this command.
  22. *
  23. * @var string
  24. */
  25. protected $eventName;
  26. protected function UseTemplate() {
  27. return FALSE;
  28. }
  29. /**
  30. * Configures the current command.
  31. */
  32. public function configure() {
  33. parent::configure();
  34. $this->eventName = $this->getName();
  35. $this
  36. ->setName('build:services')
  37. ->setDescription('Step 5: build the services.yml file.')
  38. ->setDefinition(
  39. new InputDefinition([
  40. new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL, '', static::BUILD_NAME),
  41. ])
  42. )
  43. ->setHelp(
  44. <<<EOT
  45. The build:services command combines shared and per-environment parameters and passes
  46. them to the services.local.yml.twig template to build the settings/services.yml file.
  47. EOT
  48. );
  49. }
  50. /**
  51. * Executes the current command.
  52. *
  53. * {@inheritDoc}
  54. */
  55. public function execute(InputInterface $input, OutputInterface $output): int {
  56. [
  57. $params,
  58. $msg,
  59. $err,
  60. ] = $this->getParams();
  61. if ($err != 0) {
  62. $output->writeln($msg);
  63. return $err;
  64. }
  65. foreach ($params['sites'] as $name => $siteParams) {
  66. $services = [];
  67. if (!empty($siteParams['parameters'])) {
  68. $services['parameters'] = $siteParams['parameters'];
  69. }
  70. if (!empty($siteParams['services'])) {
  71. $services['services'] = $siteParams['services'];
  72. }
  73. if (!$services) {
  74. return 0;
  75. }
  76. $fileName = $input->getArgument(static::ARG_FILE);
  77. $destination = "web/sites/$name/$fileName.yml";
  78. if (file_exists($destination)) {
  79. $ok = unlink($destination);
  80. if (!$ok) {
  81. return [sprintf("Could not remove old %s file", $destination), 1];
  82. }
  83. }
  84. // Convert PHP array to Yaml.
  85. $yamlServices = Yaml::dump($services, 4, 2);
  86. $ok = file_put_contents($destination, $yamlServices, LOCK_EX);
  87. if (!$ok) {
  88. return [sprintf('Could not write new %s file', $destination), 2];
  89. }
  90. }
  91. return 0;
  92. }
  93. }