PhpMemcacheAdminConfigCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Fgm\Drupal\Composer;
  4. use Composer\Package\CompletePackageInterface;
  5. use Osinet\Deploy\BuilderBase;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Input\InputDefinition;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. /**
  11. * Class Installer sets up PhpMemcacheAdmin in vendor/phpmemcacheadmin/Config.
  12. *
  13. * Unlike the default configuration:
  14. * - it does not require making part of vendor/ writable.
  15. * - it builds the Memcache.php configuration class from a non-repository
  16. * configuration file.
  17. *
  18. * Configuration:
  19. * - add a "phpmemcacheadmin" subsection in settings/params.local.yml.
  20. * Available keys:
  21. * - basic: a hash with the most commonly changed settings
  22. * - advanced: a hash with the least commonly changed settings
  23. * - the memcache.servers section is used to chose manageed instances.
  24. *
  25. * @package Fgm\Drupal\Composer
  26. */
  27. class PhpMemcacheAdminConfigCommand extends BaseBuilderCommand {
  28. const PACKAGE = 'elijaa/phpmemcacheadmin';
  29. const PARAM_KEY = 'phpmemcacheadmin';
  30. const TARGET = 'Config/Memcache.php';
  31. const TEMPLATE = 'Memcache.php.twig';
  32. /**
  33. * The event triggering this command.
  34. *
  35. * @var string
  36. */
  37. protected $eventName;
  38. /**
  39. * Configures the current command.
  40. */
  41. public function configure() {
  42. parent::configure();
  43. $this->eventName = $this->getName();
  44. $this
  45. ->setName('build:phpmemcacheadmin')
  46. ->setDescription('Build the PhpMemcacheAdmin configuration.')
  47. ->setDefinition(
  48. new InputDefinition([
  49. new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL),
  50. ])
  51. )
  52. ->setHelp(
  53. <<<EOT
  54. The build:phpmemcacheadmin command uses the memcache section of the site parameters
  55. to build the vendor/elijaa/phpmemcacheamin/Config/Memcache.php file.
  56. EOT
  57. );
  58. }
  59. /**
  60. * Executes the current command.
  61. *
  62. * {@inheritDoc}
  63. */
  64. public function execute(InputInterface $input, OutputInterface $output): int {
  65. $input->setArgument(static::ARG_FILE, static::PARAM_KEY);
  66. [$wrapper, $params, $msg, $err] = $this->prepare($input, $output);
  67. if ($err != 0) {
  68. $output->writeln($msg);
  69. return $err;
  70. }
  71. $context = $this->buildConfig($wrapper, $params);
  72. $target = $this->getTarget();
  73. echo "Generating $target\n";
  74. [$msg, $error] = $this->render($wrapper, $context, $target);
  75. if ($error) {
  76. $output->writeln(sprintf("Failed rendering doc configuration: %s", $msg));
  77. return 5;
  78. }
  79. return 0;
  80. }
  81. /**
  82. * Render the config from params.local.yml and the template.
  83. *
  84. * @param \Twig_TemplateWrapper $template
  85. * The template used to format the parameters.
  86. * @param array $params
  87. * The parameters loaded from the local params file.
  88. *
  89. * @return array
  90. * An array made of the relevant parameters.
  91. */
  92. protected function buildConfig(\Twig_TemplateWrapper $template, array $params) {
  93. $params[static::PARAM_KEY]['basic']['file_path'] = realpath($params[static::PARAM_KEY]['basic']['file_path']);
  94. $variables = array_merge(
  95. $params[static::PARAM_KEY]['basic'],
  96. $params[static::PARAM_KEY]['advanced'], [
  97. 'servers' => $params['memcache']['servers'],
  98. ],
  99. ['params' => realpath($this->getSettingsPath() . "/params.local.yml")]
  100. );
  101. return $variables;
  102. }
  103. /**
  104. * Get the actual path where the configuration file needs to be generated.
  105. *
  106. * @return string
  107. * The path.
  108. */
  109. protected function getTarget(): string {
  110. $composer = $this->getComposer();
  111. $packages = $composer
  112. ->getRepositoryManager()
  113. ->getLocalRepository()
  114. ->getPackages();
  115. $package = current(array_filter($packages, function (CompletePackageInterface $package) {
  116. return $package->getName() === static::PACKAGE;
  117. }));
  118. $targetBase = $composer
  119. ->getInstallationManager()
  120. ->getInstallPath($package);
  121. $target = "{$targetBase}/" . static::TARGET;
  122. return $target;
  123. }
  124. }