|
@@ -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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|