Browse Source

Working command scaffold, also triggered on post-install/update.

Frederic G. MARAND 6 years ago
parent
commit
aa31e09cd5
3 changed files with 73 additions and 74 deletions
  1. 1 6
      src/BuildSettingsCommand.php
  2. 24 68
      src/Builder.php
  3. 48 0
      src/BuilderCommandProvider.php

+ 1 - 6
src/BuildSettingsCommand.php

@@ -8,16 +8,11 @@ use Symfony\Component\Console\Output\OutputInterface;
 
 class BuildSettingsCommand extends BaseCommand {
 
-  public function __construct(string $name = NULL) {
-    parent::__construct($name);
-    echo "Constructing " . __CLASS__ . "\n";
-  }
-
   public function configure() {
     parent::configure();
     $this
       ->setName('build-settings')
-      ->setDescription('Build the settings.local.php file from the per-environment parameters and the settings template.')
+      ->setDescription('Builds the *.settings.local.php files.')
       ->setDefinition([])
       ->setHelp(<<<EOT
 The build-settings command combines shared and per-environment parameters and passes them to the settings.local.php.twig template to build the settings/(build|run).settings.local.php files.

+ 24 - 68
src/Builder.php

@@ -12,14 +12,23 @@ use Composer\IO\IOInterface;
 use Composer\Plugin\Capability\Capability;
 use Composer\Plugin\Capability\CommandProvider;
 use Composer\Plugin\Capable;
+use Composer\Plugin\CommandEvent;
+use Composer\Plugin\PluginEvents;
 use Composer\Plugin\PluginInterface;
 use Composer\Script\Event as ScriptEvent;
 use Composer\Script\ScriptEvents;
+use Symfony\Component\Console\Input\ArgvInput;
+use Symfony\Component\Console\Output\ConsoleOutput;
 
-class Builder implements Capable, Capability, CommandProvider, EventSubscriberInterface, PluginInterface {
+class Builder implements Capable, Capability,  EventSubscriberInterface, PluginInterface {
 
   /**
-   * @var IOInterface
+   * @var \Composer\Composer
+   */
+  protected $composer;
+
+  /**
+   * @var \Composer\IO\IOInterface
    */
   protected $io;
 
@@ -30,91 +39,38 @@ class Builder implements Capable, Capability, CommandProvider, EventSubscriberIn
    * @param IOInterface $io
    */
   public function activate(Composer $composer, IOInterface $io) {
+    $this->composer = $composer;
     $this->io = $io;
   }
 
   public function getCapabilities() {
     return [
-      CommandProvider::class => Builder::class,
-    ];
-  }
-
-  /**
-   * For CommandProvider::class.
-   */
-  public function getCommands() {
-    echo __METHOD__ . "\n";
-    return [
-      new BuildSettingsCommand(),
+      CommandProvider::class => BuilderCommandProvider::class,
     ];
   }
 
   /**
-   * Returns an array of event names this subscriber wants to listen to.
-   *
-   * The array keys are event names and the value can be:
+   * {@inheritdoc}
    *
-   * * The method name to call (priority defaults to 0)
-   * * An array composed of the method name to call and the priority
-   * * An array of arrays composed of the method names to call and respective
-   *   priorities, or 0 if unset
+   * Available events:
    *
-   * For instance:
-   *
-   * * array('eventName' => 'methodName')
-   * * array('eventName' => array('methodName', $priority))
-   * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
-   *
-   * @return array The event names to listen to
+   * - Composer\Installer\InstallerEvents::* -> Composer\Installer\InstallerEvent
+   * - Composer\Installer\PackageEvents::* -> Composer\Installer\PackageEvent
+   * - Composer\Installer\PluginEvents::INIT -> Composer\EventDispatcher\Event
+   * - Composer\Installer\PluginEvents::COMMAND -> Composer\Plugin\CommandEvent
+   * - Composer\Installer\PluginEvents::PRE_FILE_DOWNLOAD
+   *     -> Composer\Plugin\PreFileDownloadEvent
+   * - Composer\Script\ScriptEvents::* -> Composer\Script\Event
    */
   public static function getSubscribedEvents() {
     return [
-      InstallerEvents::POST_DEPENDENCIES_SOLVING => 'onEvent',
-      InstallerEvents::PRE_DEPENDENCIES_SOLVING => 'onEvent',
-
-      PackageEvents::POST_PACKAGE_INSTALL => 'onPackageEvent',
-      PackageEvents::POST_PACKAGE_UNINSTALL => 'onPackageEvent',
-      PackageEvents::POST_PACKAGE_UPDATE => 'onPackageEvent',
-      PackageEvents::PRE_PACKAGE_INSTALL => 'onPackageEvent',
-      PackageEvents::PRE_PACKAGE_UNINSTALL => 'onPackageEvent',
-      PackageEvents::PRE_PACKAGE_UPDATE => 'onPackageEvent',
-
-//      PluginEvents::COMMAND => 'onEvent',
-//      PluginEvents::INIT => 'onEvent',
-//      PluginEvents::PRE_FILE_DOWNLOAD => 'onEvent',
-
-//      ScriptEvents::POST_ARCHIVE_CMD => 'onScriptEvent',
-//      ScriptEvents::POST_AUTOLOAD_DUMP => 'onScriptEvent',
-//      ScriptEvents::POST_CREATE_PROJECT_CMD => 'onScriptEvent',
       ScriptEvents::POST_INSTALL_CMD => 'onScriptEvent',
-//      ScriptEvents::POST_ROOT_PACKAGE_INSTALL => 'onScriptEvent',
-//      ScriptEvents::POST_STATUS_CMD => 'onScriptEvent',
       ScriptEvents::POST_UPDATE_CMD => 'onScriptEvent',
-//      ScriptEvents::PRE_ARCHIVE_CMD => 'onScriptEvent',
-//      ScriptEvents::PRE_AUTOLOAD_DUMP => 'onScriptEvent',
-//      ScriptEvents::PRE_INSTALL_CMD => 'onScriptEvent',
-//      ScriptEvents::PRE_STATUS_CMD => 'onScriptEvent',
-//      ScriptEvents::PRE_UPDATE_CMD => 'onScriptEvent',
     ];
   }
 
-  public function onEvent(Event $event) {
-    $this->io->write($event->getName());
-  }
-
-  public function onPackageEvent(PackageEvent $event) {
-    $this->io->write([
-      $event->getName(),
-      $event->getOperation()->getJobType() . ' - ' . $event->getOperation()->getReason(),
-      json_encode($event->getArguments()),
-      json_encode($event->getInstalledRepo()->getRepositories()),
-    ]);
-  }
-
   public function onScriptEvent(ScriptEvent $event) {
-    $this->io->write([
-      $event->getName(),
-      json_encode($event->getArguments()),
-    ]);
+    $buildCommand = new BuildSettingsCommand($event->getName());
+    $buildCommand->run(new ArgvInput([]), new ConsoleOutput());
   }
 }

+ 48 - 0
src/BuilderCommandProvider.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace Fgm\Drupal\Composer;
+
+
+use Composer\Plugin\Capability\CommandProvider;
+
+class BuilderCommandProvider implements CommandProvider {
+
+  /**
+   * @var \Composer\Composer
+   */
+  protected $composer;
+
+  /**
+   * @var \Composer\IO\IOInterface
+   */
+  protected $io;
+
+  /**
+   * An instance of the plugin instantiated not-as-a-command.
+   *
+   * @var self
+   */
+  protected $plugin;
+
+  /**
+   * BuilderCommandProvider constructor.
+   *
+   * @param array|NULL $args
+   *   Guaranteed to contain composer/io/plugin as per CommandProvider.
+   */
+  public function __construct(array $args = null) {
+    $this->composer = $args['composer'];
+    $this->io = $args['io'];
+    $this->plugin = $args['plugin'];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getCommands() {
+    return [
+      new BuildSettingsCommand(),
+    ];
+  }
+
+}