From 35031130947a60be83ca60277a9b34cae2f65b89 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Thu, 1 Nov 2018 10:36:32 +0100
Subject: [PATCH] Refactor namespace to extend LakeDrops Composer Components

---
 composer.json   |  2 +-
 src/Handler.php | 53 +++++++++----------------------------------------
 src/Plugin.php  | 22 ++++++++------------
 3 files changed, 18 insertions(+), 59 deletions(-)

diff --git a/composer.json b/composer.json
index d922c63..78e3ddb 100644
--- a/composer.json
+++ b/composer.json
@@ -39,7 +39,7 @@
         "drush-ops/behat-drush-endpoint": "^9.2",
         "jcalderonzumba/gastonjs": "^1.2",
         "jcalderonzumba/mink-phantomjs-driver": "^0.3",
-        "lakedrops/composer-scripts": "^1.0.0",
+        "lakedrops/composer-scripts": "^1.1.0",
         "lakedrops/docker4drupal": "^1.0.0",
         "lakedrops/dotenv": "^1.0.0",
         "php": ">=5.6",
diff --git a/src/Handler.php b/src/Handler.php
index 56da1fb..4dfc0f5 100644
--- a/src/Handler.php
+++ b/src/Handler.php
@@ -2,9 +2,8 @@
 
 namespace LakeDrops\Behat4Drupal;
 
-use Composer\Composer;
-use Composer\IO\IOInterface;
 use Composer\Script\Event;
+use LakeDrops\Component\Composer\BaseHandler;
 use LakeDrops\Component\Dotenv\Dotenv;
 use Symfony\Component\Filesystem\Filesystem;
 use Symfony\Component\Yaml\Yaml;
@@ -14,47 +13,7 @@ use Symfony\Component\Yaml\Yaml;
  *
  * @package LakeDrops\Behat4Drupal
  */
-class Handler {
-
-  /**
-   * The composer object running this session.
-   *
-   * @var \Composer\Composer
-   */
-  protected $composer;
-
-  /**
-   * The input-output object for the composer session.
-   *
-   * @var \Composer\IO\IOInterface
-   */
-  protected $io;
-
-  /**
-   * Handler constructor.
-   *
-   * @param \Composer\Composer $composer
-   *   The composer object.
-   * @param \Composer\IO\IOInterface $io
-   *   The input-output object.
-   */
-  public function __construct(Composer $composer, IOInterface $io) {
-    $this->composer = $composer;
-    $this->io = $io;
-  }
-
-  /**
-   * Retrieve a package from the current composer process.
-   *
-   * @param string $name
-   *   Name of the package to get from the current composer installation.
-   *
-   * @return \Composer\Package\PackageInterface
-   *   The package.
-   */
-  protected function getPackage($name) {
-    return $this->composer->getRepositoryManager()->getLocalRepository()->findPackage($name, '*');
-  }
+class Handler extends BaseHandler {
 
   /**
    * Post project create event to execute the scaffolding.
@@ -63,6 +22,10 @@ class Handler {
    *   The event that triggered the plugin.
    * @param bool $overwrite
    *   Whether to overwrite existing config files.
+   *
+   * @throws \Twig_Error_Loader
+   * @throws \Twig_Error_Runtime
+   * @throws \Twig_Error_Syntax
    */
   public function configureProject(Event $event, $overwrite = FALSE) {
 
@@ -95,6 +58,7 @@ class Handler {
         $rendered = $twig->render($filename, $options);
         if (!empty($def['add2yaml']) && isset($options[$filename])) {
           $yaml = Yaml::parse($rendered);
+          /** @noinspection SlowArrayOperationsInLoopInspection */
           $yaml = array_merge_recursive($yaml, $options[$filename]);
           $rendered = Yaml::dump($yaml, 9, 2);
         }
@@ -150,9 +114,10 @@ class Handler {
     $env = new Dotenv('behat4drupal', $this->io);
     $env->put('COMPOSE_PROJECT_NAME', $projectname);
     $extra = $this->composer->getPackage()->getExtra() + ['behat4drupal' => []];
-    return $extra['behat4drupal'] + [
+    $options = $extra['behat4drupal'] + [
       'projectname' => $projectname,
     ];
+    return $env->replaceEnvironmentVariables($options);
   }
 
 }
diff --git a/src/Plugin.php b/src/Plugin.php
index d630560..0e6d9f7 100644
--- a/src/Plugin.php
+++ b/src/Plugin.php
@@ -2,30 +2,20 @@
 
 namespace LakeDrops\Behat4Drupal;
 
-use Composer\Composer;
-use Composer\EventDispatcher\EventSubscriberInterface;
-use Composer\IO\IOInterface;
-use Composer\Plugin\PluginInterface;
 use Composer\Script\Event;
 use Composer\Script\ScriptEvents;
+use LakeDrops\Component\Composer\BasePlugin;
 
 /**
  * Composer plugin for handling drupal scaffold.
  */
-class Plugin implements PluginInterface, EventSubscriberInterface {
-
-  /**
-   * The handler object for events.
-   *
-   * @var \LakeDrops\Behat4Drupal\Handler
-   */
-  protected $handler;
+class Plugin extends BasePlugin {
 
   /**
    * {@inheritdoc}
    */
-  public function activate(Composer $composer, IOInterface $io) {
-    $this->handler = new Handler($composer, $io);
+  public function getHandlerClass() {
+    return Handler::class;
   }
 
   /**
@@ -52,6 +42,10 @@ class Plugin implements PluginInterface, EventSubscriberInterface {
    *
    * @param \Composer\Script\Event $event
    *   The event that triggered the call of this function.
+   *
+   * @throws \Twig_Error_Loader
+   * @throws \Twig_Error_Runtime
+   * @throws \Twig_Error_Syntax
    */
   public static function config(Event $event) {
     $handler = new Handler($event->getComposer(), $event->getIO());
-- 
GitLab