ソースを参照

Merge branch 'add_services'

Frederic G. MARAND 3 年 前
コミット
5bfdf25961

+ 46 - 22
src/BaseBuilderCommand.php

@@ -1,6 +1,6 @@
 <?php
 
-declare(strict_types = 1);
+declare(strict_types=1);
 
 namespace Fgm\Drupal\Composer;
 
@@ -49,14 +49,13 @@ abstract class BaseBuilderCommand extends BaseCommand {
    *   - string Template name
    *   - int Error
    */
-  public function validateTemplateName(InputInterface $input, OutputInterface $output): array {
-    $file = $input->getArgument(static::ARG_FILE);
+  public function validateTemplateName(InputInterface $input, OutputInterface $output, string $buildName): array {
     $conf = $this->getComposer()->getPackage()->getExtra()[Builder::NAME] ?? [];
-    $templateName = $conf['templates'][$file] ?? '';
+    $templateName = $conf['templates'][$buildName] ?? '';
     if (empty($templateName)) {
       $output->writeln(sprintf(
         'Could not build file %s: no such template in composer.json extra section',
-        $file
+        $buildName
       ));
       return ["", 1];
     }
@@ -105,6 +104,8 @@ abstract class BaseBuilderCommand extends BaseCommand {
    *   Command input.
    * @param \Symfony\Component\Console\Output\OutputInterface $output
    *   Command output.
+   * @param string $buildName
+   *   Machine name of build process.
    *
    * @return array
    *   - TemplateWrapper|NULL: Twig template wrapper
@@ -116,8 +117,11 @@ abstract class BaseBuilderCommand extends BaseCommand {
    * @throws \Twig\Error\RuntimeError
    * @throws \Twig\Error\SyntaxError
    */
-  protected function prepare(InputInterface $input, OutputInterface $output): array {
-    [$templateName, $err] = $this->validateTemplateName($input, $output);
+  protected function prepare(InputInterface $input, OutputInterface $output, string $buildName): array {
+    [
+      $templateName,
+      $err,
+    ] = $this->validateTemplateName($input, $output, $buildName);
     if ($err) {
       return [NULL, [], "Could not validate template name", 1];
     };
@@ -126,16 +130,46 @@ abstract class BaseBuilderCommand extends BaseCommand {
     $templatePath = "${settingsPath}/${templateName}";
     $realTemplatePath = realpath($templatePath);
     if (empty($realTemplatePath)) {
-      return [NULL, [],
+      return [
+        NULL,
+        [],
         sprintf("Could not load template %s: no such file", $templateName),
         2,
       ];
     }
 
+    [$params, $msg, $err] = $this->getParams();
+    if ($err) {
+      return [NULL, [], $msg, $err];
+    };
+
+    $loader = new FilesystemLoader($settingsPath, $settingsPath);
+    $twig = new Environment($loader, [
+      'auto_reload' => TRUE,
+      'cache' => FALSE,
+      'debug' => TRUE,
+      'strict_variables' => TRUE,
+    ]);
+    $twig->addExtension(new DebugExtension());
+    $wrapper = $twig->load($templateName);
+    return [$wrapper, $params, "", 0];
+  }
+
+  /**
+   * Get parameters as obtained from configuration.
+   *
+   * @return array
+   *   - array: parameters
+   *   - string: error message
+   *   - int: error, 0 if OK. If non-zero, only the error message is reliable.
+   */
+  public function getParams() {
+    $settingsPath = $this->getSettingsPath();
     $paramsPath = "${settingsPath}/merged.params.local.yml";
     $realParamsPath = realpath($paramsPath);
     if (empty($realParamsPath)) {
-      return [NULL, [],
+      return [
+        [],
         sprintf("Could not load parameters %s: no such file", $paramsPath),
         3,
       ];
@@ -143,25 +177,15 @@ abstract class BaseBuilderCommand extends BaseCommand {
 
     $yaml = new Yaml();
     try {
-      $params = $yaml->parseFile($realParamsPath);
+      return [$yaml->parseFile($realParamsPath), '', 0];
     }
     catch (ParseException $e) {
-      return [NULL, [],
+      return [
+        [],
         sprintf("Could not parse %s: %s", $realParamsPath, $e->getMessage()),
         4,
       ];
     }
-
-    $loader = new FilesystemLoader($settingsPath, $settingsPath);
-    $twig = new Environment($loader, [
-      'auto_reload' => TRUE,
-      'cache' => FALSE,
-      'debug' => TRUE,
-      'strict_variables' => TRUE,
-    ]);
-    $twig->addExtension(new DebugExtension());
-    $wrapper = $twig->load($templateName);
-    return [$wrapper, $params, "", 0];
   }
 
 }

+ 106 - 0
src/BuildServicesCommand.php

@@ -0,0 +1,106 @@
+<?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;
+  }
+
+}

+ 7 - 2
src/BuildSettingsCommand.php

@@ -16,6 +16,11 @@ use Symfony\Component\Console\Output\OutputInterface;
  */
 class BuildSettingsCommand extends BaseBuilderCommand {
 
+  /**
+   * Machine name of current building process.
+   */
+  const BUILD_NAME = 'settings';
+
   /**
    * The event triggering this command.
    *
@@ -34,7 +39,7 @@ class BuildSettingsCommand extends BaseBuilderCommand {
       ->setDescription('Step 4: build the *.settings.local.php files.')
       ->setDefinition(
         new InputDefinition([
-          new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL),
+          new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL, '', static::BUILD_NAME),
         ])
       )
       ->setHelp(
@@ -52,7 +57,7 @@ EOT
    * {@inheritDoc}
    */
   public function execute(InputInterface $input, OutputInterface $output): int {
-    [$wrapper, $params, $msg, $err] = $this->prepare($input, $output);
+    [$wrapper, $params, $msg, $err] = $this->prepare($input, $output, static::BUILD_NAME);
     if ($err != 0) {
       $output->writeln($msg);
       return $err;

+ 1 - 0
src/BuilderCommandProvider.php

@@ -51,6 +51,7 @@ class BuilderCommandProvider implements CommandProvider {
    */
   public function getCommands() {
     return [
+      new BuildServicesCommand(),
       new BuildSettingsCommand(),
       new HugoConfigCommand(),
       new MergeParamsCommand(),

+ 7 - 2
src/HugoConfigCommand.php

@@ -16,6 +16,11 @@ use Symfony\Component\Console\Output\OutputInterface;
  */
 class HugoConfigCommand extends BaseBuilderCommand {
 
+  /**
+   * Machine name of current building process.
+   */
+  const BUILD_NAME = 'config_doc';
+
   /**
    * The event triggering this command.
    *
@@ -34,7 +39,7 @@ class HugoConfigCommand extends BaseBuilderCommand {
       ->setDescription('Step 2 (Optional): configure the documentation site for the current environment.')
       ->setDefinition(
         new InputDefinition([
-          new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL),
+          new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL, '', static::BUILD_NAME),
         ])
       )
       ->setHelp(
@@ -52,7 +57,7 @@ EOT
    * {@inheritDoc}
    */
   public function execute(InputInterface $input, OutputInterface $output): int {
-    [$wrapper, $params, $msg, $err] = $this->prepare($input, $output);
+    [$wrapper, $params, $msg, $err] = $this->prepare($input, $output, static::BUILD_NAME);
     if ($err != 0) {
       $output->writeln($msg);
       return $err;

+ 32 - 12
src/PhpMemcacheAdminConfigCommand.php

@@ -1,6 +1,6 @@
 <?php
 
-declare(strict_types = 1);
+declare(strict_types=1);
 
 namespace Fgm\Drupal\Composer;
 
@@ -29,10 +29,26 @@ use Symfony\Component\Console\Output\OutputInterface;
  * @package Fgm\Drupal\Composer
  */
 class PhpMemcacheAdminConfigCommand extends BaseBuilderCommand {
+
+  /**
+   * Machine name of current building process.
+   *
+   * Used as file name if none provided.
+   */
+  const BUILD_NAME = 'phpmemcacheadmin';
+
+  /**
+   * PHPMemCacheAdminn package name.
+   */
   const PACKAGE = 'elijaa/phpmemcacheadmin';
-  const PARAM_KEY = 'phpmemcacheadmin';
+
+  /**
+   * PHPMemCacheAdmin configuration file path.
+   *
+   * Path of PHPMemCacheAdmin configuration file relative to
+   * directory of elijaa/phpmemcacheadmin package.
+   */
   const TARGET = 'Config/Memcache.php';
-  const TEMPLATE = 'Memcache.php.twig';
 
   /**
    * The event triggering this command.
@@ -52,7 +68,7 @@ class PhpMemcacheAdminConfigCommand extends BaseBuilderCommand {
       ->setDescription('Build the PhpMemcacheAdmin configuration.')
       ->setDefinition(
         new InputDefinition([
-          new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL),
+          new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL, '', static::BUILD_NAME),
         ])
       )
       ->setHelp(
@@ -69,8 +85,12 @@ EOT
    * {@inheritDoc}
    */
   public function execute(InputInterface $input, OutputInterface $output): int {
-    $input->setArgument(static::ARG_FILE, static::PARAM_KEY);
-    [$wrapper, $params, $msg, $err] = $this->prepare($input, $output);
+    [
+      $wrapper,
+      $params,
+      $msg,
+      $err,
+    ] = $this->prepare($input, $output, static::BUILD_NAME);
     if ($err != 0) {
       $output->writeln($msg);
       return $err;
@@ -101,13 +121,13 @@ EOT
    *   An array made of the relevant parameters.
    */
   protected function buildConfig(\Twig_TemplateWrapper $template, array $params) {
-    $params[static::PARAM_KEY]['basic']['file_path'] = realpath($params[static::PARAM_KEY]['basic']['file_path']);
+    $params[static::BUILD_NAME]['basic']['file_path'] = realpath($params[static::BUILD_NAME]['basic']['file_path']);
     $variables = array_merge(
-      $params[static::PARAM_KEY]['basic'],
-      $params[static::PARAM_KEY]['advanced'], [
-        'servers' => $params['memcache']['servers'],
-      ],
-      ['params' => realpath($this->getSettingsPath() . "/params.local.yml")]
+      $params[static::BUILD_NAME]['basic'],
+      $params[static::BUILD_NAME]['advanced'], [
+      'servers' => $params['memcache']['servers'],
+    ],
+      ['local_params_path' => realpath($this->getSettingsPath() . "/params.local.yml")]
     );
     return $variables;
   }