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;
use LakeDrops\Component\Dotenv\Dotenv;
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.
......@@ -17,6 +22,16 @@ final class Config {
*/
private $component;
/**
* @var \Twig_Loader_Array
*/
private $twig_loader;
/**
* @var \Twig_Environment
*/
private $twig;
/**
* Config constructor.
*
......@@ -26,6 +41,9 @@ final class Config {
*/
public function __construct(string $component, array $default_values, Dotenv $env) {
$this->component = $component;
$this->twig_loader = new Twig_Loader_Array([]);
$this->twig = new Twig_Environment($this->twig_loader);
$this->init();
$componentValues = self::$values[$component] ?? [];
self::$values[$component] = $env->replaceEnvironmentVariables(NestedArray::mergeDeep($default_values, $componentValues));
......@@ -50,7 +68,7 @@ final class Config {
*
* @return mixed|null
*/
private function readValueFromArray($values, $keys) {
private function readValueFromArray(array $values, array $keys) {
$key = array_shift($keys);
if (!isset($values[$key])) {
return NULL;
......@@ -72,4 +90,24 @@ final class Config {
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 @@
"php": ">=7.2",
"ext-json": "*",
"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": {
"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