diff --git a/Config.php b/Config.php index d2d35e3cd61df841789ccc8ef414a6e1d32e3b8f..954e430f68a8bd912e034b055e0e0a6645a07260 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(); }