PhpMemcacheAdminConfigCommand.php 4.3 KB

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