PhpMemcacheAdminConfigCommand.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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(new InputDefinition([
  60. new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL, '', static::BUILD_NAME),
  61. ]))
  62. ->setHelp(
  63. <<<EOT
  64. The build:phpmemcacheadmin command uses the memcache section of the site parameters
  65. to build the vendor/elijaa/phpmemcacheamin/Config/Memcache.php file.
  66. EOT
  67. );
  68. }
  69. /**
  70. * Executes the current command.
  71. *
  72. * {@inheritDoc}
  73. */
  74. public function execute(InputInterface $input, OutputInterface $output): int {
  75. [
  76. $wrapper,
  77. $params,
  78. $msg,
  79. $err,
  80. ] = $this->prepare($input, $output, static::BUILD_NAME);
  81. if ($err != 0) {
  82. $output->writeln($msg);
  83. return $err;
  84. }
  85. $context = $this->buildConfig($wrapper, $params);
  86. $target = $this->getTarget();
  87. echo "Generating $target\n";
  88. [$msg, $error] = $this->render($wrapper, $context, $target);
  89. if ($error) {
  90. $output->writeln(sprintf("Failed rendering doc configuration: %s", $msg));
  91. return 5;
  92. }
  93. return 0;
  94. }
  95. /**
  96. * Render the config from params.local.yml and the template.
  97. *
  98. * @param \Twig_TemplateWrapper $template
  99. * The template used to format the parameters.
  100. * @param array $params
  101. * The parameters loaded from the local params file.
  102. *
  103. * @return array
  104. * An array made of the relevant parameters.
  105. */
  106. protected function buildConfig(\Twig_TemplateWrapper $template, array $params) {
  107. $params[static::BUILD_NAME]['basic']['file_path'] = realpath($params[static::BUILD_NAME]['basic']['file_path']);
  108. $variables = array_merge(
  109. $params[static::BUILD_NAME]['basic'],
  110. $params[static::BUILD_NAME]['advanced'], [
  111. 'servers' => $params['memcache']['servers'],
  112. ],
  113. ['local_params_path' => realpath($this->getSettingsPath() . "/params.local.yml")]
  114. );
  115. return $variables;
  116. }
  117. /**
  118. * Get the actual path where the configuration file needs to be generated.
  119. *
  120. * @return string
  121. * The path.
  122. */
  123. protected function getTarget(): string {
  124. $composer = $this->getComposer();
  125. $packages = $composer
  126. ->getRepositoryManager()
  127. ->getLocalRepository()
  128. ->getPackages();
  129. $package = current(
  130. array_filter(
  131. $packages,
  132. function (PackageInterface $package) {
  133. return $package->getName() === static::PACKAGE;
  134. }
  135. )
  136. );
  137. $targetBase = $composer
  138. ->getInstallationManager()
  139. ->getInstallPath($package);
  140. $target = "{$targetBase}/" . static::TARGET;
  141. return $target;
  142. }
  143. }