Builder.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. use Symfony\Component\Console\Input\ArgvInput;
  14. use Symfony\Component\Console\Output\ConsoleOutput;
  15. class Builder implements Capable, Capability, EventSubscriberInterface, PluginInterface
  16. {
  17. const NAME = "composer_builder";
  18. /**
  19. * @var \Composer\Composer
  20. */
  21. protected $composer;
  22. /**
  23. * @var \Composer\IO\IOInterface
  24. */
  25. protected $io;
  26. /**
  27. * {@inheritdoc}
  28. *
  29. * Available events:
  30. *
  31. * - Composer\Installer\InstallerEvents::* ->
  32. * Composer\Installer\InstallerEvent
  33. * - Composer\Installer\PackageEvents::* -> Composer\Installer\PackageEvent
  34. * - Composer\Installer\PluginEvents::INIT -> Composer\EventDispatcher\Event
  35. * - Composer\Installer\PluginEvents::COMMAND -> Composer\Plugin\CommandEvent
  36. * - Composer\Installer\PluginEvents::PRE_FILE_DOWNLOAD
  37. * -> Composer\Plugin\PreFileDownloadEvent
  38. * - Composer\Script\ScriptEvents::* -> Composer\Script\Event
  39. */
  40. public static function getSubscribedEvents()
  41. {
  42. return [
  43. ScriptEvents::POST_INSTALL_CMD => 'onScriptEvent',
  44. ScriptEvents::POST_UPDATE_CMD => 'onScriptEvent',
  45. ];
  46. }
  47. /**
  48. * Apply plugin modifications to Composer
  49. *
  50. * @param Composer $composer
  51. * @param IOInterface $io
  52. */
  53. public function activate(Composer $composer, IOInterface $io)
  54. {
  55. $this->composer = $composer;
  56. $this->io = $io;
  57. }
  58. /**
  59. * {@inheritDoc}
  60. */
  61. public function getCapabilities()
  62. {
  63. return [
  64. CommandProvider::class => BuilderCommandProvider::class,
  65. ];
  66. }
  67. /**
  68. * Event callback: run build:settings on post-install|update only.
  69. *
  70. * @param \Composer\Script\Event $event
  71. *
  72. * @throws \Exception
  73. */
  74. public function onScriptEvent(ScriptEvent $event)
  75. {
  76. if (in_array(
  77. $event->getName(),
  78. [
  79. ScriptEvents::POST_INSTALL_CMD,
  80. ScriptEvents::POST_UPDATE_CMD,
  81. ]
  82. )
  83. ) {
  84. // FIXME add behavior where, without an argument, the command will build all templates.
  85. // $buildCommand = new BuildSettingsCommand($event->getName());
  86. // $buildCommand->run(new ArgvInput([]), new ConsoleOutput());
  87. }
  88. }
  89. }