Builder.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /**
  73. * Event callback: run build:settings on post-install|update only.
  74. *
  75. * @param \Composer\Script\Event $event
  76. * The subscribed event triggering this callback.
  77. *
  78. * @throws \Exception
  79. */
  80. public function onScriptEvent(ScriptEvent $event) {
  81. if (in_array($event->getName(), [
  82. ScriptEvents::POST_INSTALL_CMD,
  83. ScriptEvents::POST_UPDATE_CMD,
  84. ])) {
  85. // FIXME without an argument, the command should build all templates.
  86. // $buildCommand = new BuildSettingsCommand($event->getName());
  87. // $buildCommand->run(new ArgvInput([]), new ConsoleOutput());
  88. }
  89. }
  90. }