Browse Source

Merge branch 'pmca' of fgm/drupal_composer_builder into master

Eric Bellot 4 years ago
parent
commit
96777186d1
3 changed files with 140 additions and 0 deletions
  1. 2 0
      src/BuildSettingsCommand.php
  2. 1 0
      src/BuilderCommandProvider.php
  3. 137 0
      src/PhpMemcacheAdminConfigCommand.php

+ 2 - 0
src/BuildSettingsCommand.php

@@ -67,12 +67,14 @@ EOT
         $stageParams = $siteParams;
         $stageParams['settings'] = $stageSettings;
         $context = [
+          'extra' => array_diff_key($params, ['instance' => 0, 'sites' => 0]),
           'instance' => $params['instance'],
           'name' => $name,
           'stage' => $stage,
           'site' => $stageParams,
         ];
         $destination = "web/sites/${context['name']}/${context['stage']}.settings.local.php";
+        echo "Building $destination\n";
         [$msg, $error] = $this->render($wrapper, $context, $destination);
         if ($error) {
           $output->writeln(sprintf("Failed rendering %s settings for site %s: %s", $stage, $name, $msg));

+ 1 - 0
src/BuilderCommandProvider.php

@@ -54,6 +54,7 @@ class BuilderCommandProvider implements CommandProvider {
       new BuildSettingsCommand(),
       new HugoConfigCommand(),
       new MergeParamsCommand(),
+      new PhpMemcacheAdminConfigCommand(),
     ];
   }
 

+ 137 - 0
src/PhpMemcacheAdminConfigCommand.php

@@ -0,0 +1,137 @@
+<?php
+
+declare(strict_types = 1);
+
+namespace Fgm\Drupal\Composer;
+
+use Composer\Package\CompletePackageInterface;
+use Osinet\Deploy\BuilderBase;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputDefinition;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+/**
+ * Class Installer sets up PhpMemcacheAdmin in vendor/phpmemcacheadmin/Config.
+ *
+ * Unlike the default configuration:
+ * - it does not require making part of vendor/ writable.
+ * - it builds the Memcache.php configuration class from a non-repository
+ *   configuration file.
+ *
+ * Configuration:
+ * - add a "phpmemcacheadmin" subsection in settings/params.local.yml.
+ *   Available keys:
+ *   - basic: a hash with the most commonly changed settings
+ *   - advanced: a hash with the least commonly changed settings
+ * - the memcache.servers section is used to chose manageed instances.
+ *
+ * @package Fgm\Drupal\Composer
+ */
+class PhpMemcacheAdminConfigCommand extends BaseBuilderCommand {
+  const PACKAGE = 'elijaa/phpmemcacheadmin';
+  const PARAM_KEY = 'phpmemcacheadmin';
+  const TARGET = 'Config/Memcache.php';
+  const TEMPLATE = 'Memcache.php.twig';
+
+  /**
+   * The event triggering this command.
+   *
+   * @var string
+   */
+  protected $eventName;
+
+  /**
+   * Configures the current command.
+   */
+  public function configure() {
+    parent::configure();
+    $this->eventName = $this->getName();
+    $this
+      ->setName('build:phpmemcacheadmin')
+      ->setDescription('Build the PhpMemcacheAdmin configuration.')
+      ->setDefinition(
+        new InputDefinition([
+          new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL),
+        ])
+      )
+      ->setHelp(
+        <<<EOT
+The build:phpmemcacheadmin command uses the memcache section of the site parameters
+to build the vendor/elijaa/phpmemcacheamin/Config/Memcache.php file.
+EOT
+      );
+  }
+
+  /**
+   * Executes the current command.
+   *
+   * {@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);
+    if ($err != 0) {
+      $output->writeln($msg);
+      return $err;
+    }
+
+    $context = $this->buildConfig($wrapper, $params);
+    $target = $this->getTarget();
+    echo "Generating $target\n";
+
+    [$msg, $error] = $this->render($wrapper, $context, $target);
+    if ($error) {
+      $output->writeln(sprintf("Failed rendering doc configuration: %s", $msg));
+      return 5;
+    }
+
+    return 0;
+  }
+
+  /**
+   * Render the config from params.local.yml and the template.
+   *
+   * @param \Twig_TemplateWrapper $template
+   *   The template used to format the parameters.
+   * @param array $params
+   *   The parameters loaded from the local params file.
+   *
+   * @return array
+   *   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']);
+    $variables = array_merge(
+      $params[static::PARAM_KEY]['basic'],
+      $params[static::PARAM_KEY]['advanced'], [
+        'servers' => $params['memcache']['servers'],
+      ],
+      ['params' => realpath($this->getSettingsPath() . "/params.local.yml")]
+    );
+    return $variables;
+  }
+
+  /**
+   * Get the actual path where the configuration file needs to be generated.
+   *
+   * @return string
+   *   The path.
+   */
+  protected function getTarget(): string {
+    $composer = $this->getComposer();
+    $packages = $composer
+      ->getRepositoryManager()
+      ->getLocalRepository()
+      ->getPackages();
+    $package = current(array_filter($packages, function (CompletePackageInterface $package) {
+      return $package->getName() === static::PACKAGE;
+    }));
+    $targetBase = $composer
+      ->getInstallationManager()
+      ->getInstallPath($package);
+    $target = "{$targetBase}/" . static::TARGET;
+    return $target;
+  }
+
+}