123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?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 {
- /**
- * Machine name of current building process.
- *
- * Used as file name if none provided.
- */
- const BUILD_NAME = 'phpmemcacheadmin';
- /**
- * PHPMemCacheAdminn package name.
- */
- const PACKAGE = 'elijaa/phpmemcacheadmin';
- /**
- * PHPMemCacheAdmin configuration file path.
- *
- * Path of PHPMemCacheAdmin configuration file relative to
- * directory of elijaa/phpmemcacheadmin package.
- */
- const TARGET = 'Config/Memcache.php';
- /**
- * 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, '', static::BUILD_NAME),
- ])
- )
- ->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 {
- [
- $wrapper,
- $params,
- $msg,
- $err,
- ] = $this->prepare($input, $output, static::BUILD_NAME);
- 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::BUILD_NAME]['basic']['file_path'] = realpath($params[static::BUILD_NAME]['basic']['file_path']);
- $variables = array_merge(
- $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;
- }
- /**
- * 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;
- }
- }
|