Skip to content
Snippets Groups Projects
Commit 85f072e2 authored by jurgenhaas's avatar jurgenhaas
Browse files

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

parent 1f9b934a
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,11 @@ namespace LakeDrops\Component\Composer; ...@@ -4,6 +4,11 @@ namespace LakeDrops\Component\Composer;
use LakeDrops\Component\Dotenv\Dotenv; use LakeDrops\Component\Dotenv\Dotenv;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig_Environment;
use Twig_Loader_Array;
/** /**
* Manages project config. * Manages project config.
...@@ -17,6 +22,16 @@ final class Config { ...@@ -17,6 +22,16 @@ final class Config {
*/ */
private $component; private $component;
/**
* @var \Twig_Loader_Array
*/
private $twig_loader;
/**
* @var \Twig_Environment
*/
private $twig;
/** /**
* Config constructor. * Config constructor.
* *
...@@ -26,6 +41,9 @@ final class Config { ...@@ -26,6 +41,9 @@ final class Config {
*/ */
public function __construct(string $component, array $default_values, Dotenv $env) { public function __construct(string $component, array $default_values, Dotenv $env) {
$this->component = $component; $this->component = $component;
$this->twig_loader = new Twig_Loader_Array([]);
$this->twig = new Twig_Environment($this->twig_loader);
$this->init(); $this->init();
$componentValues = self::$values[$component] ?? []; $componentValues = self::$values[$component] ?? [];
self::$values[$component] = $env->replaceEnvironmentVariables(NestedArray::mergeDeep($default_values, $componentValues)); self::$values[$component] = $env->replaceEnvironmentVariables(NestedArray::mergeDeep($default_values, $componentValues));
...@@ -50,7 +68,7 @@ final class Config { ...@@ -50,7 +68,7 @@ final class Config {
* *
* @return mixed|null * @return mixed|null
*/ */
private function readValueFromArray($values, $keys) { private function readValueFromArray(array $values, array $keys) {
$key = array_shift($keys); $key = array_shift($keys);
if (!isset($values[$key])) { if (!isset($values[$key])) {
return NULL; return NULL;
...@@ -72,4 +90,24 @@ final class Config { ...@@ -72,4 +90,24 @@ final class Config {
return $this->readValueFromArray(self::$values[$this->component], $parts); return $this->readValueFromArray(self::$values[$this->component], $parts);
} }
/**
* @param string $filename
* @param string $content
*
* @return string
*/
public function render(string $filename, string $content): string {
$this->twig_loader->setTemplate($filename, $content);
try {
return $this->twig->render($filename, self::$values[$this->component]);
}
catch (LoaderError $e) {
}
catch (RuntimeError $e) {
}
catch (SyntaxError $e) {
}
return '';
}
} }
...@@ -27,7 +27,8 @@ ...@@ -27,7 +27,8 @@
"php": ">=7.2", "php": ">=7.2",
"ext-json": "*", "ext-json": "*",
"lakedrops/dotenv": "^1.5||dev-master", "lakedrops/dotenv": "^1.5||dev-master",
"symfony/yaml": "^3.4||^4.4||^5.0" "symfony/yaml": "^3.4||^4.4||^5.0",
"twig/twig": "^1.44"
}, },
"require-dev": { "require-dev": {
"composer/composer": "^1||^2", "composer/composer": "^1||^2",
......
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