From 3224c867422f5b76e78d45d347cb6baab1aa9cc4 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen.haas@lakedrops.com>
Date: Sun, 27 Dec 2020 15:47:40 +0100
Subject: [PATCH] docker/l3d#58 Move project settings out of composer.json

---
 Config.php | 50 ++++++++++++++++++++++++++++++++++----------------
 1 file changed, 34 insertions(+), 16 deletions(-)

diff --git a/Config.php b/Config.php
index d2d35e3..954e430 100644
--- a/Config.php
+++ b/Config.php
@@ -19,6 +19,11 @@ final class Config {
    */
   private static $values;
 
+  /**
+   * @var array
+   */
+  private static $customValues;
+
   /**
    * @var string
    */
@@ -56,11 +61,10 @@ final class Config {
     $this->env = $env;
     $this->twig_loader = new Twig_Loader_Array([]);
     $this->twig = new Twig_Environment($this->twig_loader);
-    $this->configFile = getcwd() . '/.project.yaml';
+    $this->configFile = getcwd() . '/.lakedrops.yml';
 
     $this->init();
-    $componentValues = self::$values[$component] ?? [];
-    $this->merge($default_values, $componentValues);
+    $this->merge($default_values, []);
   }
 
   /**
@@ -68,29 +72,40 @@ final class Config {
    */
   private function init(): void {
     if (self::$values === NULL) {
+      self::$values = [];
       if (!file_exists($this->configFile)) {
-        self::$values = [];
+        self::$customValues = [];
         $this->save();
       }
       else {
-        self::$values = Yaml::parseFile($this->configFile);
+        self::$customValues = Yaml::parseFile($this->configFile);
       }
     }
   }
 
   /**
-   * @param array $values1
-   * @param array $values2
+   * @param array $defaultValues
+   * @param array $customValues
    */
-  private function merge(array $values1, array $values2): void {
-    self::$values[$this->component] = $this->env->replaceEnvironmentVariables(NestedArray::mergeDeep($values1, $values2));
+  private function merge(array $defaultValues, array $customValues): void {
+    $default = self::$values[$this->component] ?? [];
+    $custom = self::$customValues[$this->component] ?? [];
+    if (!empty($defaultValues)) {
+      $default = NestedArray::mergeDeep($default, $defaultValues);
+    }
+    if (!empty($customValues)) {
+      $default = NestedArray::mergeDeep($default, $customValues);
+      $custom = NestedArray::mergeDeep($custom, $customValues);
+    }
+    self::$values[$this->component] = $this->env->replaceEnvironmentVariables($default);
+    self::$customValues[$this->component] = $custom;
   }
 
   /**
    * Save the current settings.
    */
   private function save(): void {
-    file_put_contents($this->configFile, Yaml::dump(self::$values, 9, 2));
+    file_put_contents($this->configFile, Yaml::dump(self::$customValues, 9, 2));
   }
 
   /**
@@ -111,20 +126,23 @@ final class Config {
   }
 
   /**
-   * @param $path
+   * @param string|array $keys
    *
    * @return mixed|null
    */
-  public function readValue($path) {
-    $parts = explode('/', $path);
-    return $this->readValueFromArray(self::$values[$this->component], $parts);
+  public function readValue($keys) {
+    if (is_string($keys)) {
+      $keys = [$keys];
+    }
+    return $this->readValueFromArray(self::$values[$this->component], $keys);
   }
 
   /**
+   * @param string $key
    * @param array $values
    */
-  public function setValue(array $values): void {
-    $this->merge(self::$values[$this->component], [$this->component => $values]);
+  public function setValue(string $key, array $values): void {
+    $this->merge([], [$this->component => [$key => $values]]);
     $this->save();
   }
 
-- 
GitLab