Skip to content

Commit

Permalink
make config process deep ENV substitution (#214)
Browse files Browse the repository at this point in the history
* add deployer script

* refactor subdomain processing and add test to it (#205)

* make config process deep ENV substitution

* fix/var subs (#215)

* refactor subdomain processing and add test to it (#204)

* refactor subdomain processing and add test to it

* add other envs to deployment

* make config process deep ENV substitution

* fix namespace
  • Loading branch information
microstudi committed Sep 7, 2021
1 parent 5a6fded commit 02e9804
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
34 changes: 16 additions & 18 deletions src/Goteo/Application/Config.php
Expand Up @@ -303,6 +303,7 @@ static private function setDirConfiguration() {

/**********************************/
// LEGACY VIEWS
// One day, this will be removed, and happiness will spread along the galaxy
\Goteo\Core\View::addViewPath(GOTEO_PATH . 'Resources/templates/legacy');
//NormalForm views
\Goteo\Core\View::addViewPath(GOTEO_PATH . 'src/Goteo/Library/NormalForm/view');
Expand Down Expand Up @@ -415,10 +416,7 @@ static public function setConstants() {
* @return array|false|mixed|string|null
* @throws ConfigException
*/
static public function get(
string $name,
bool $strict = false
) {
static public function get(string $name, bool $strict = false) {
$part = strtok($name, '.');
if (self::$config && array_key_exists($part, self::$config)) {
$paramValue = self::$config[$part];
Expand All @@ -432,27 +430,27 @@ static public function get(
}
}

if(is_array($paramValue)) {
// TODO: iterative, to cover any level
foreach($paramValue as $key => $val) {
if (!is_array($val) && preg_match(self::ENV_PARAMETER_REG_EX, $val, $matches)) {
$paramValue[$key] = getenv($matches[1]);
}
}
} elseif (preg_match(self::ENV_PARAMETER_REG_EX, $paramValue, $matches)) {
if (sizeof($matches) >= 1) {
$paramValue = getenv($matches[1]);
}
}

return $paramValue;
return self::replaceEnvVars($paramValue);
} elseif ($strict) {
throw new ConfigException("Config var [$name] not found!");
}

return null;
}

static protected function replaceEnvVars($paramValue) {
if(is_array($paramValue)) {
foreach($paramValue as $key => $val) {
$paramValue[$key] = self::replaceEnvVars($val);
}
} elseif (preg_match(self::ENV_PARAMETER_REG_EX, $paramValue, $matches)) {
if (sizeof($matches) >= 1) {
$paramValue = getenv($matches[1]);
}
}
return $paramValue;
}

static public function set($name, $value) {
$config = self::_set(self::$config, $name, $value);
}
Expand Down
12 changes: 11 additions & 1 deletion src/Goteo/Core/Traits/LoggerTrait.php
Expand Up @@ -11,8 +11,10 @@
namespace Goteo\Core\Traits;

use Goteo\Application\App;
use Goteo\Application\Config;
use Goteo\Util\Monolog\Processor\WebProcessor;
use Psr\Log\LoggerInterface;
use RuntimeException;

/**
* Trait to add log common methods
Expand All @@ -35,7 +37,15 @@ public function getLog() {
public function log($message, array $context = [], $func = 'info') {
$logger = $this->getLog();
if (null !== $logger && method_exists($logger, $func)) {
return $logger->$func($message, WebProcessor::processObject($context));
if(Config::get('debug')) {
return $logger->$func($message, WebProcessor::processObject($context));
} else {
try {
return $logger->$func($message, WebProcessor::processObject($context));
} catch(RuntimeException $e) {
// nothing here, if not in debug mode, failure to process logs is ignored
}
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/Goteo/Core/Traits/StaticLoggerTrait.php
Expand Up @@ -12,6 +12,8 @@

use Goteo\Util\Monolog\Processor\WebProcessor;
use Psr\Log\LoggerInterface;
use Goteo\Application\Config;
use RuntimeException;

/**
* Trait to use log on legacy classes
Expand All @@ -30,7 +32,15 @@ static public function getLogger(LoggerInterface $logger) {

static public function log($message, array $context = [], $func = 'info') {
if(static::$logger) {
static::$logger->$func($message, WebProcessor::processObject($context));
if(Config::get('debug')) {
return static::$logger->$func($message, WebProcessor::processObject($context));
} else {
try {
return static::$logger->$func($message, WebProcessor::processObject($context));
} catch(RuntimeException $e) {
// nothing here, if not in debug mode, failure to process logs is ignored
}
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Goteo/Application/ConfigTest.php
Expand Up @@ -35,6 +35,32 @@ public function testValidateYamlLangFiles() {
}
}

public function testShallowEnvSubstitution() {
$expected = "a nice value";
Config::set("test.shallow_sub_env_var", "%env(SOME_NICE_ENV_VAR)%");
$this->assertEmpty($readParameterValue);
putenv("SOME_NICE_ENV_VAR=$expected");

$readParameterValue = Config::get("test.shallow_sub_env_var");
$this->assertEquals($expected, $readParameterValue);
}

public function testDeepEnvSubstitution() {
$expected = "a nice value";
Config::set("test.deep.sub.env.var", "%env(ANOTHER_NICE_ENV_VAR)%");
$this->assertEmpty($readParameterValue);
putenv("ANOTHER_NICE_ENV_VAR=$expected");

$readParameterValue = Config::get("test.deep.sub.env.var");
$this->assertEquals($expected, $readParameterValue);

$readParameterParent = Config::get("test.deep.sub.env");
$this->assertEquals($expected, $readParameterParent["var"]);

$readParameterAntecessor = Config::get("test");
$this->assertEquals($expected, $readParameterAntecessor["deep"]["sub"]["env"]["var"]);
}

public function testYamlLangFilesWithEnvParameter() {
$expectedDatabasePortEnv = 33061;
putenv("DATABASE_PORT=$expectedDatabasePortEnv");
Expand Down

0 comments on commit 02e9804

Please sign in to comment.