PhpMemcacheAdminConfigCommand.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /**
  29. * Machine name of current building process.
  30. *
  31. * Used as file name if none provided.
  32. */
  33. const BUILD_NAME = 'phpmemcacheadmin';
  34. /**
  35. * PHPMemCacheAdminn package name.
  36. */
  37. const PACKAGE = 'elijaa/phpmemcacheadmin';
  38. /**
  39. * PHPMemCacheAdmin configuration file path.
  40. *
  41. * Path of PHPMemCacheAdmin configuration file relative to
  42. * directory of elijaa/phpmemcacheadmin package.
  43. */
  44. const TARGET = 'Config/Memcache.php';
  45. /**
  46. * The event triggering this command.
  47. *
  48. * @var string
  49. */
  50. protected $eventName;
  51. /**
  52. * Configures the current command.
  53. */
  54. public function configure() {
  55. parent::configure();
  56. $this->eventName = $this->getName();
  57. $this
  58. ->setName('build:phpmemcacheadmin')
  59. ->setDescription('Build the PhpMemcacheAdmin configuration.')
  60. ->setDefinition(
  61. new InputDefinition([
  62. new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL, '', static::BUILD_NAME),
  63. ])
  64. )
  65. ->setHelp(
  66. <<<EOT
  67. The build:phpmemcacheadmin command uses the memcache section of the site parameters
  68. to build the vendor/elijaa/phpmemcacheamin/Config/Memcache.php file.
  69. EOT
  70. );
  71. }
  72. /**
  73. * Executes the current command.
  74. *
  75. * {@inheritDoc}
  76. */
  77. public function execute(InputInterface $input, OutputInterface $output): int {
  78. [
  79. $wrapper,
  80. $params,
  81. $msg,
  82. $err,
  83. ] = $this->prepare($input, $output, static::BUILD_NAME);
  84. if ($err != 0) {
  85. $output->writeln($msg);
  86. return $err;
  87. }
  88. $context = $this->buildConfig($wrapper, $params);
  89. $target = $this->getTarget();
  90. echo "Generating $target\n";
  91. [$msg, $error] = $this->render($wrapper, $context, $target);
  92. if ($error) {
  93. $output->writeln(sprintf("Failed rendering doc configuration: %s", $msg));
  94. return 5;
  95. }
  96. return 0;
  97. }
  98. /**
  99. * Render the config from params.local.yml and the template.
  100. *
  101. * @param \Twig_TemplateWrapper $template
  102. * The template used to format the parameters.
  103. * @param array $params
  104. * The parameters loaded from the local params file.
  105. *
  106. * @return array
  107. * An array made of the relevant parameters.
  108. */
  109. protected function buildConfig(\Twig_TemplateWrapper $template, array $params) {
  110. $params[static::BUILD_NAME]['basic']['file_path'] = realpath($params[static::BUILD_NAME]['basic']['file_path']);
  111. $variables = array_merge(
  112. $params[static::BUILD_NAME]['basic'],
  113. $params[static::BUILD_NAME]['advanced'], [
  114. 'servers' => $params['memcache']['servers'],
  115. ],
  116. ['local_params_path' => realpath($this->getSettingsPath() . "/params.local.yml")]
  117. );
  118. return $variables;
  119. }
  120. /**
  121. * Get the actual path where the configuration file needs to be generated.
  122. *
  123. * @return string
  124. * The path.
  125. */
  126. protected function getTarget(): string {
  127. $composer = $this->getComposer();
  128. $packages = $composer
  129. ->getRepositoryManager()
  130. ->getLocalRepository()
  131. ->getPackages();
  132. $package = current(array_filter($packages, function (CompletePackageInterface $package) {
  133. return $package->getName() === static::PACKAGE;
  134. }));
  135. $targetBase = $composer
  136. ->getInstallationManager()
  137. ->getInstallPath($package);
  138. $target = "{$targetBase}/" . static::TARGET;
  139. return $target;
  140. }
  141. }