From e8a8ff885722be58952ec786fbe43e3b471951dc Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Tue, 24 Oct 2017 19:52:36 +0200
Subject: [PATCH] #7 Always refresh settings files, allow live alias parameters
 in composer.json

---
 README.md                          | 39 +++++++++++++++++++++++++++++-
 src/Handler.php                    | 16 ++++++++++--
 src/Plugin.php                     |  2 +-
 templates/aliases.drushrc.php.twig |  8 +++---
 templates/aliases.yml.twig         |  8 +++---
 5 files changed, 61 insertions(+), 12 deletions(-)

diff --git a/README.md b/README.md
index 0e267fe..63887c7 100644
--- a/README.md
+++ b/README.md
@@ -191,6 +191,12 @@ To overwrite the default settings for the Docker environment, add the relevant p
   "extra": {
     "docker4drupal": {
         "projectname": "[NAME OF CURRENT DIRECTORY]",
+        "live": {
+          "root": "",
+          "uri": "",
+          "host": "",
+          "user": ""
+        },
         "drupal": {
           "version": 8
         },
@@ -275,7 +281,38 @@ That allows you to simply call the following command to only update docker4drupa
 composer d4d
 ``` 
 
-Note, that docker4drupal configuration does not overwrite existing setting files. If you want to get them changed, you have to delete them first.
+Note, that docker4drupal configuration **does overwrite** existing setting files when you run this script.
+
+Therefore, you can add custom content to the settings files by adding another key to the `extra/docker4drupal` section with the file name as the key and additional settings underneath. Example: 
+
+```json
+{
+  "extra": {
+    "docker4drupal": {
+      "docker-compose.yml": {
+        "services": {
+          "php": {
+            "environment": {
+              "MY_VAR": "value"
+            }
+          },
+          "mariadbd6": {
+            "image": "wodby/mariadb:10.1",
+            "environment": {
+              "MYSQL_ROOT_PASSWORD": "password",
+              "MYSQL_DATABASE": "drupal",
+              "MYSQL_USER": "drupal",
+              "MYSQL_PASSWORD": "drupal"
+            }
+          }
+        }
+      }
+    }
+  }
+}
+```
+
+This will add another environment variable to the php container and define a second mariadb container named `mariadbd6` which may be used e.g. for a migration from a separate database.
 
 ## Tipps & Tricks
 
diff --git a/src/Handler.php b/src/Handler.php
index 239b935..1ffe9ed 100644
--- a/src/Handler.php
+++ b/src/Handler.php
@@ -67,8 +67,9 @@ class Handler {
    * Configure Drupal Project for Docker.
    *
    * @param ScriptEvent $event
+   * @param bool $overwrite
    */
-  public function configureProject($event) {
+  public function configureProject($event, $overwrite = FALSE) {
 
     // We only do the fancy stuff for developers
     if (!$event->isDevMode()) {
@@ -118,7 +119,10 @@ class Handler {
     $options['webRoot'] = $webRoot . '/';
     foreach ($this->getFiles($projectRoot, $webRoot, $settingsPath) as $template => $def) {
       $file = $def['dest'] . '/' . $template;
-      if (!$fs->exists($file)) {
+      if ($overwrite || !$fs->exists($file)) {
+        if ($fs->exists($file)) {
+          $fs->rename($file, $file . '.orig');
+        }
         $twig_loader->setTemplate($template, file_get_contents($pluginRoot . '/templates/' . $template . '.twig'));
         $rendered = $twig->render($template, $options);
         if (!empty($def['add2yaml']) && isset($options[$template])) {
@@ -180,9 +184,11 @@ class Handler {
       ],
       'aliases.yml' => [
         'dest' => $projectRoot . '/drush',
+        'add2yaml' => TRUE,
       ],
       'drush.yml' => [
         'dest' => $projectRoot . '/drush',
+        'add2yaml' => TRUE,
       ],
       '.env' => [
         'dest' => $projectRoot,
@@ -202,6 +208,12 @@ class Handler {
         'docker0' => [
           'ip' => $this->getLocalIpv4('docker0'),
         ],
+        'live' => [
+          'root' => '',
+          'uri' => '',
+          'host' => '',
+          'user' => '',
+        ],
         'drupal' => [
           'version' => '8',
         ],
diff --git a/src/Plugin.php b/src/Plugin.php
index bf13951..139d55a 100644
--- a/src/Plugin.php
+++ b/src/Plugin.php
@@ -52,7 +52,7 @@ class Plugin implements PluginInterface, EventSubscriberInterface {
    */
   public static function config($event) {
     $handler = new Handler($event->getComposer(), $event->getIO());
-    $handler->configureProject($event);
+    $handler->configureProject($event, TRUE);
   }
 
 }
diff --git a/templates/aliases.drushrc.php.twig b/templates/aliases.drushrc.php.twig
index b19e429..e4c4ce1 100644
--- a/templates/aliases.drushrc.php.twig
+++ b/templates/aliases.drushrc.php.twig
@@ -6,8 +6,8 @@ $aliases['dev'] = array(
 );
 
 $aliases['live'] = array(
-  'root' => '',
-  'uri' => '',
-  'remote-host' => '',
-  'remote-user' => '',
+  'root' => '{{ live.root }}',
+  'uri' => '{{ live.uri }}',
+  'remote-host' => '{{ live.host }}',
+  'remote-user' => '{{ live.user }}',
 );
diff --git a/templates/aliases.yml.twig b/templates/aliases.yml.twig
index 16b9e5c..e637107 100644
--- a/templates/aliases.yml.twig
+++ b/templates/aliases.yml.twig
@@ -4,7 +4,7 @@ sites:
       root: '/var/www/html/{{ webRoot }}'
       uri: '{{ projectname }}.docker.localhost:8000'
     live:
-      root: ''
-      uri: ''
-      remote-host: ''
-      remote-user: ''
+      root: '{{ live.root }}'
+      uri: '{{ live.uri }}'
+      remote-host: '{{ live.host }}'
+      remote-user: '{{ live.user }}'
-- 
GitLab