123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace Fgm\Drupal\Composer;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputDefinition;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Yaml\Yaml;
- /**
- * Build the services.yml file for all sites in the multisite.
- *
- * @package Fgm\Drupal\Composer
- */
- class BuildServicesCommand extends BaseBuilderCommand {
- /**
- * Machine name of current building process.
- *
- * Used as file name if none provided.
- */
- const BUILD_NAME = 'services';
- /**
- * The event triggering this command.
- *
- * @var string
- */
- protected $eventName;
- protected function UseTemplate() {
- return FALSE;
- }
- /**
- * Configures the current command.
- */
- public function configure() {
- parent::configure();
- $this->eventName = $this->getName();
- $this
- ->setName('build:services')
- ->setDescription('Step 5: build the services.yml file.')
- ->setDefinition(
- new InputDefinition([
- new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL, '', static::BUILD_NAME),
- ])
- )
- ->setHelp(
- <<<EOT
- The build:services command combines shared and per-environment parameters and passes
- them to the services.local.yml.twig template to build the settings/services.yml file.
- EOT
- );
- }
- /**
- * Executes the current command.
- *
- * {@inheritDoc}
- */
- public function execute(InputInterface $input, OutputInterface $output): int {
- [
- $params,
- $msg,
- $err,
- ] = $this->getParams();
- if ($err != 0) {
- $output->writeln($msg);
- return $err;
- }
- foreach ($params['sites'] as $name => $siteParams) {
- $services = [];
- if (!empty($siteParams['parameters'])) {
- $services['parameters'] = $siteParams['parameters'];
- }
- if (!empty($siteParams['services'])) {
- $services['services'] = $siteParams['services'];
- }
- if (!$services) {
- return 0;
- }
- $fileName = $input->getArgument(static::ARG_FILE);
- $destination = "web/sites/$name/$fileName.yml";
- if (file_exists($destination)) {
- $ok = unlink($destination);
- if (!$ok) {
- return [sprintf("Could not remove old %s file", $destination), 1];
- }
- }
- // Convert PHP array to Yaml.
- $yamlServices = Yaml::dump($services, 4, 2);
- $ok = file_put_contents($destination, $yamlServices, LOCK_EX);
- if (!$ok) {
- return [sprintf('Could not write new %s file', $destination), 2];
- }
- }
- return 0;
- }
- }
|