Builder.php 2.5 KB

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