Builder.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Fgm\Drupal\Composer;
  4. use Composer\Composer;
  5. use Composer\EventDispatcher\EventSubscriberInterface;
  6. use Composer\IO\IOInterface;
  7. use Composer\Plugin\Capability\Capability;
  8. use Composer\Plugin\Capability\CommandProvider;
  9. use Composer\Plugin\Capable;
  10. use Composer\Plugin\PluginInterface;
  11. use Composer\Script\Event as ScriptEvent;
  12. use Composer\Script\ScriptEvents;
  13. /**
  14. * Class Builder is the Composer plugin entry point.
  15. *
  16. * @package Fgm\Drupal\Composer
  17. */
  18. class Builder implements Capable, Capability, EventSubscriberInterface, PluginInterface {
  19. const NAME = "composer_builder";
  20. /**
  21. * The currently running Composer instance.
  22. *
  23. * @var \Composer\Composer
  24. */
  25. protected $composer;
  26. /**
  27. * The Composer I/O.
  28. *
  29. * @var \Composer\IO\IOInterface
  30. */
  31. protected $io;
  32. /**
  33. * {@inheritdoc}
  34. *
  35. * Available events:
  36. *
  37. * - Composer\Installer\InstallerEvents::*
  38. * -> Composer\Installer\InstallerEvent
  39. * - Composer\Installer\PackageEvents::* -> Composer\Installer\PackageEvent
  40. * - Composer\Installer\PluginEvents::INIT -> Composer\EventDispatcher\Event
  41. * - Composer\Installer\PluginEvents::COMMAND -> Composer\Plugin\CommandEvent
  42. * - Composer\Installer\PluginEvents::PRE_FILE_DOWNLOAD
  43. * -> Composer\Plugin\PreFileDownloadEvent
  44. * - Composer\Script\ScriptEvents::* -> Composer\Script\Event
  45. */
  46. public static function getSubscribedEvents() {
  47. return [
  48. ScriptEvents::POST_INSTALL_CMD => 'onScriptEvent',
  49. ScriptEvents::POST_UPDATE_CMD => 'onScriptEvent',
  50. ];
  51. }
  52. /**
  53. * Apply plugin modifications to Composer.
  54. *
  55. * @param \Composer\Composer $composer
  56. * The currently running Composer instance.
  57. * @param \Composer\IO\IOInterface $io
  58. * The Composer I/O.
  59. */
  60. public function activate(Composer $composer, IOInterface $io) {
  61. $this->composer = $composer;
  62. $this->io = $io;
  63. }
  64. /**
  65. * Composer plugin API: describe the plugin capabilities.
  66. */
  67. public function getCapabilities() {
  68. return [
  69. CommandProvider::class => BuilderCommandProvider::class,
  70. ];
  71. }
  72. protected function showCommands() {
  73. $capabilities = $this->getCapabilities();
  74. if (empty($capabilities[CommandProvider::class])) {
  75. return;
  76. }
  77. $providerClass = $capabilities[CommandProvider::class];
  78. /** @var \Composer\Plugin\Capability\CommandProvider $provider */
  79. $provider = new $providerClass([
  80. 'composer' => $this->composer,
  81. 'io' => $io = $this->io,
  82. 'plugin' => NULL,
  83. ]);
  84. $commands = $provider->getCommands();
  85. $io->write("Available build commands:");
  86. foreach ($commands as $command) {
  87. $io->write("- " . $command->getName());
  88. }
  89. }
  90. /**
  91. * Event callback: run build:settings on post-install|update only.
  92. *
  93. * @param \Composer\Script\Event $event
  94. * The subscribed event triggering this callback.
  95. *
  96. * @throws \Exception
  97. */
  98. public function onScriptEvent(ScriptEvent $event) {
  99. if (in_array($event->getName(), [
  100. ScriptEvents::POST_INSTALL_CMD,
  101. ScriptEvents::POST_UPDATE_CMD,
  102. ])) {
  103. // FIXME without an argument, the command should build all templates.
  104. // $buildCommand = new BuildSettingsCommand($event->getName());
  105. // $buildCommand->run(new ArgvInput([]), new ConsoleOutput());
  106. // Verbosity seems limited to 32, while the test expects 64 or 128.
  107. if ($event->getIO()->isVerbose()) {
  108. $this->showCommands();
  109. }
  110. }
  111. }
  112. }