Skip to content
Snippets Groups Projects
Commit 3224c867 authored by jurgenhaas's avatar jurgenhaas
Browse files

docker/l3d#58 Move project settings out of composer.json

parent 24db58f8
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment