From 957fb15aba24ef7e849c6c6d927cf020fef75ee1 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Mon, 10 Dec 2018 18:56:16 +0100
Subject: [PATCH] Implement support for Composer commands

---
 BaseCommand.php          | 32 ++++++++++++++++++++++++++++++
 BaseCommandInterface.php | 17 ++++++++++++++++
 BaseHandler.php          | 43 ++++++++++++++++++++++++++++++++++++++++
 BaseHandlerInterface.php | 18 +++++++++++++++++
 4 files changed, 110 insertions(+)
 create mode 100644 BaseCommand.php
 create mode 100644 BaseCommandInterface.php

diff --git a/BaseCommand.php b/BaseCommand.php
new file mode 100644
index 0000000..dfd7f7b
--- /dev/null
+++ b/BaseCommand.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace LakeDrops\Component\Composer;
+
+use Composer\Command\BaseCommand as ComposerBaseCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+/**
+ * Class BaseCommand.
+ *
+ * @package LakeDrops\Component\Composer
+ */
+abstract class BaseCommand extends ComposerBaseCommand implements BaseCommandInterface {
+
+  /**
+   * The handler object to do the real work then.
+   *
+   * @var \LakeDrops\Component\Composer\BaseHandlerInterface
+   */
+  protected $handler;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function execute(InputInterface $input, OutputInterface $output) {
+    $class = $this->getHandlerClass();
+    $this->handler = new $class($this->getComposer(), $this->getIO());
+    $this->handler->setInput($input);
+  }
+
+}
diff --git a/BaseCommandInterface.php b/BaseCommandInterface.php
new file mode 100644
index 0000000..62ffdde
--- /dev/null
+++ b/BaseCommandInterface.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace LakeDrops\Component\Composer;
+
+/**
+ * Interface for BaseCommand.
+ *
+ * @package LakeDrops\Component\Composer
+ */
+interface BaseCommandInterface {
+
+  /**
+   * @return \LakeDrops\Component\Composer\BaseHandlerInterface
+   */
+  public function getHandlerClass();
+
+}
diff --git a/BaseHandler.php b/BaseHandler.php
index 3cd0de2..d74e59b 100644
--- a/BaseHandler.php
+++ b/BaseHandler.php
@@ -4,6 +4,8 @@ namespace LakeDrops\Component\Composer;
 
 use Composer\Composer;
 use Composer\IO\IOInterface;
+use Composer\Script\Event;
+use Symfony\Component\Console\Input\InputInterface;
 
 /**
  * Class BaseHandler.
@@ -26,6 +28,18 @@ abstract class BaseHandler implements BaseHandlerInterface {
    */
   protected $io;
 
+  /**
+   * @var \Symfony\Component\Console\Input\InputInterface
+   */
+  protected $input;
+
+  /**
+   * The event that triggered the action.
+   *
+   * @var \Composer\Script\Event
+   */
+  protected $event;
+
   /**
    * The Drupal core package.
    *
@@ -46,6 +60,22 @@ abstract class BaseHandler implements BaseHandlerInterface {
     $this->io = $io;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function setEvent(Event $event) {
+    $this->event = $event;
+    return $this;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setInput(InputInterface $input) {
+    $this->input = $input;
+    return $this;
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -63,6 +93,19 @@ abstract class BaseHandler implements BaseHandlerInterface {
     return $this->composer->getRepositoryManager()->getLocalRepository()->findPackage($name, '*');
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function isDevMode() {
+    if ($this->event !== NULL) {
+      return $this->event->isDevMode();
+    }
+    if ($this->input !== NULL) {
+      return !$this->input->hasOption('no-dev');
+    }
+    return FALSE;
+  }
+
   /**
    * {@inheritdoc}
    */
diff --git a/BaseHandlerInterface.php b/BaseHandlerInterface.php
index 8ea968a..372847b 100644
--- a/BaseHandlerInterface.php
+++ b/BaseHandlerInterface.php
@@ -2,6 +2,9 @@
 
 namespace LakeDrops\Component\Composer;
 
+use Composer\Script\Event;
+use Symfony\Component\Console\Input\InputInterface;
+
 /**
  * Interface for BaseHandler.
  *
@@ -9,6 +12,16 @@ namespace LakeDrops\Component\Composer;
  */
 interface BaseHandlerInterface {
 
+  /**
+   * @param \Composer\Script\Event $event
+   */
+  public function setEvent(Event $event);
+
+  /**
+   * @param \Symfony\Component\Console\Input\InputInterface $input
+   */
+  public function setInput(InputInterface $input);
+
   /**
    * Look up the Drupal core package object.
    *
@@ -28,6 +41,11 @@ interface BaseHandlerInterface {
    */
   public function getPackage($name);
 
+  /**
+   * @return bool
+   */
+  public function isDevMode();
+
   /**
    * Determine if the current process runs in a CI/CD context.
    *
-- 
GitLab