Builder.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Fgm\Drupal\Composer;
  3. use Composer\Composer;
  4. use Composer\EventDispatcher\Event;
  5. use Composer\EventDispatcher\EventSubscriberInterface;
  6. use Composer\Installer\InstallerEvents;
  7. use Composer\Installer\PackageEvent;
  8. use Composer\Installer\PackageEvents;
  9. use Composer\IO\IOInterface;
  10. use Composer\Plugin\Capability\Capability;
  11. use Composer\Plugin\Capability\CommandProvider;
  12. use Composer\Plugin\Capable;
  13. use Composer\Plugin\PluginInterface;
  14. use Composer\Script\Event as ScriptEvent;
  15. use Composer\Script\ScriptEvents;
  16. class Builder implements Capable, Capability, CommandProvider, EventSubscriberInterface, PluginInterface {
  17. /**
  18. * @var IOInterface
  19. */
  20. protected $io;
  21. /**
  22. * Apply plugin modifications to Composer
  23. *
  24. * @param Composer $composer
  25. * @param IOInterface $io
  26. */
  27. public function activate(Composer $composer, IOInterface $io) {
  28. $this->io = $io;
  29. }
  30. public function getCapabilities() {
  31. return [
  32. CommandProvider::class => Builder::class,
  33. ];
  34. }
  35. /**
  36. * For CommandProvider::class.
  37. */
  38. public function getCommands() {
  39. echo __METHOD__ . "\n";
  40. return [
  41. new BuildSettingsCommand(),
  42. ];
  43. }
  44. /**
  45. * Returns an array of event names this subscriber wants to listen to.
  46. *
  47. * The array keys are event names and the value can be:
  48. *
  49. * * The method name to call (priority defaults to 0)
  50. * * An array composed of the method name to call and the priority
  51. * * An array of arrays composed of the method names to call and respective
  52. * priorities, or 0 if unset
  53. *
  54. * For instance:
  55. *
  56. * * array('eventName' => 'methodName')
  57. * * array('eventName' => array('methodName', $priority))
  58. * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
  59. *
  60. * @return array The event names to listen to
  61. */
  62. public static function getSubscribedEvents() {
  63. return [
  64. InstallerEvents::POST_DEPENDENCIES_SOLVING => 'onEvent',
  65. InstallerEvents::PRE_DEPENDENCIES_SOLVING => 'onEvent',
  66. PackageEvents::POST_PACKAGE_INSTALL => 'onPackageEvent',
  67. PackageEvents::POST_PACKAGE_UNINSTALL => 'onPackageEvent',
  68. PackageEvents::POST_PACKAGE_UPDATE => 'onPackageEvent',
  69. PackageEvents::PRE_PACKAGE_INSTALL => 'onPackageEvent',
  70. PackageEvents::PRE_PACKAGE_UNINSTALL => 'onPackageEvent',
  71. PackageEvents::PRE_PACKAGE_UPDATE => 'onPackageEvent',
  72. // PluginEvents::COMMAND => 'onEvent',
  73. // PluginEvents::INIT => 'onEvent',
  74. // PluginEvents::PRE_FILE_DOWNLOAD => 'onEvent',
  75. // ScriptEvents::POST_ARCHIVE_CMD => 'onScriptEvent',
  76. // ScriptEvents::POST_AUTOLOAD_DUMP => 'onScriptEvent',
  77. // ScriptEvents::POST_CREATE_PROJECT_CMD => 'onScriptEvent',
  78. ScriptEvents::POST_INSTALL_CMD => 'onScriptEvent',
  79. // ScriptEvents::POST_ROOT_PACKAGE_INSTALL => 'onScriptEvent',
  80. // ScriptEvents::POST_STATUS_CMD => 'onScriptEvent',
  81. ScriptEvents::POST_UPDATE_CMD => 'onScriptEvent',
  82. // ScriptEvents::PRE_ARCHIVE_CMD => 'onScriptEvent',
  83. // ScriptEvents::PRE_AUTOLOAD_DUMP => 'onScriptEvent',
  84. // ScriptEvents::PRE_INSTALL_CMD => 'onScriptEvent',
  85. // ScriptEvents::PRE_STATUS_CMD => 'onScriptEvent',
  86. // ScriptEvents::PRE_UPDATE_CMD => 'onScriptEvent',
  87. ];
  88. }
  89. public function onEvent(Event $event) {
  90. $this->io->write($event->getName());
  91. }
  92. public function onPackageEvent(PackageEvent $event) {
  93. $this->io->write([
  94. $event->getName(),
  95. $event->getOperation()->getJobType() . ' - ' . $event->getOperation()->getReason(),
  96. json_encode($event->getArguments()),
  97. json_encode($event->getInstalledRepo()->getRepositories()),
  98. ]);
  99. }
  100. public function onScriptEvent(ScriptEvent $event) {
  101. $this->io->write([
  102. $event->getName(),
  103. json_encode($event->getArguments()),
  104. ]);
  105. }
  106. }