From 4b748de522c5d2edfa1bb93f71271a06891cfc32 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen.haas@lakedrops.com>
Date: Thu, 27 Jun 2024 12:10:58 +0200
Subject: [PATCH] composer/plugin/drupal-environment#17 Implement default
 settings for composer.json

---
 src/DrupalInstaller.php | 32 ++++++++++++++++++++++++++++++++
 src/Installer.php       | 30 ++++++++++++++++++++++++++++++
 src/Plugin.php          | 29 +++++++++++++++++++++++++----
 3 files changed, 87 insertions(+), 4 deletions(-)
 create mode 100644 src/DrupalInstaller.php
 create mode 100644 src/Installer.php

diff --git a/src/DrupalInstaller.php b/src/DrupalInstaller.php
new file mode 100644
index 0000000..85744e0
--- /dev/null
+++ b/src/DrupalInstaller.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace LakeDrops\DrupalEnvironment;
+
+use Composer\Installers\BaseInstaller;
+
+/**
+ * Overrides the Drupal installer with LakeDrops locations.
+ */
+class DrupalInstaller extends BaseInstaller {
+
+  /**
+   * The locations.
+   *
+   * @var array<string, string>
+   */
+  protected $locations = [
+    'core' => 'web/core/',
+    'module' => 'web/modules/contrib/{$name}/',
+    'profile' => 'web/profiles/contrib/{$name}/',
+    'recipe' => 'web/recipes/contrib/{$name}/',
+    'theme' => 'web/themes/contrib/{$name}/',
+    'library' => 'web/libraries/{$name}/',
+    'drush' => 'drush/Commands/{$name}/',
+    'custom-module' => 'web/modules/custom/{$name}/',
+    'custom-profile' => 'web/profiles/custom/{$name}/',
+    'custom-recipe' => 'web/recipes/custom/{$name}/',
+    'custom-theme' => 'web/themes/custom/{$name}/',
+    'drupal-multisite' => 'web/sites/{$name}/',
+  ];
+
+}
diff --git a/src/Installer.php b/src/Installer.php
new file mode 100644
index 0000000..6858363
--- /dev/null
+++ b/src/Installer.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace LakeDrops\DrupalEnvironment;
+
+use Composer\Installers\Installer as ComposerInstaller;
+use Composer\Package\PackageInterface;
+
+/**
+ * Extends the composer installers for special handling of Drupal locations.
+ */
+class Installer extends ComposerInstaller {
+
+  /**
+   * {@inheritDoc}
+   */
+  public function getInstallPath(PackageInterface $package) {
+    $type = $package->getType();
+    $frameworkType = $this->findFrameworkType($type);
+    if ($frameworkType === 'drupal') {
+      $installer = new DrupalInstaller($package, $this->composer, $this->io);
+      $path = $installer->getInstallPath($package, $frameworkType);
+      if (!$this->filesystem->isAbsolutePath($path)) {
+        $path = getcwd() . '/' . $path;
+      }
+      return $path;
+    }
+    return parent::getInstallPath($package);
+  }
+
+}
diff --git a/src/Plugin.php b/src/Plugin.php
index 55ab7ce..2d6f806 100644
--- a/src/Plugin.php
+++ b/src/Plugin.php
@@ -3,6 +3,7 @@
 namespace LakeDrops\DrupalEnvironment;
 
 use Composer\Composer;
+use Composer\Installers\Installer;
 use Composer\IO\IOInterface;
 use Composer\Plugin\Capability\CommandProvider as ComposerCommandProvider;
 use Composer\Script\Event;
@@ -22,11 +23,20 @@ class Plugin extends BasePlugin {
    */
   protected DrupalScaffoldHandler $scaffoldHandler;
 
+  /**
+   * The installer.
+   *
+   * @var Installer
+   */
+  private Installer $installer;
+
   /**
    * {@inheritdoc}
    */
   public function activate(Composer $composer, IOInterface $io): void {
     parent::activate($composer, $io);
+    $this->installer = new Installer($io, $composer);
+    $composer->getInstallationManager()->addInstaller($this->installer);
     if (class_exists(DrupalScaffoldHandler::class)) {
       $this->scaffoldHandler = new DrupalScaffoldHandler($composer, $io);
     }
@@ -34,13 +44,17 @@ class Plugin extends BasePlugin {
     if (isset($extra['lakedrops-config-file']) || isset($extra['lakedrops-curated-file'])) {
       if (isset($extra['lakedrops-config-file'])) {
         foreach ($this->readJson('config', $extra['lakedrops-config-file'], $io) as $key => $value) {
-          $composer->getConfig()->getConfigSource()->addConfigSetting($key, $value);
+          $composer->getConfig()
+            ->getConfigSource()
+            ->addConfigSetting($key, $value);
         }
       }
       if (isset($extra['lakedrops-curated-file'])) {
         $audit = $composer->getConfig()->get('audit');
         $audit['ignore'] = $this->readJson('audit ignore', $extra['lakedrops-curated-file'], $io);
-        $composer->getConfig()->getConfigSource()->addConfigSetting('audit', $audit);
+        $composer->getConfig()
+          ->getConfigSource()
+          ->addConfigSetting('audit', $audit);
       }
     }
 
@@ -63,6 +77,14 @@ class Plugin extends BasePlugin {
     }
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function deactivate(Composer $composer, IOInterface $io): void {
+    parent::deactivate($composer, $io);
+    $composer->getInstallationManager()->removeInstaller($this->installer);
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -122,8 +144,7 @@ class Plugin extends BasePlugin {
     try {
       $data = json_decode(file_get_contents($filename), TRUE, 512, JSON_THROW_ON_ERROR);
       $error = json_last_error();
-    }
-    catch (\JsonException) {
+    } catch (\JsonException) {
       $error = JSON_ERROR_SYNTAX;
     }
     if ($error !== 0) {
-- 
GitLab