commit c6d34dae7129e43409d936e0c491acef7efd864b
parent 9107a9928b991fdd11429813d9550a86bc52c6b0
Author: dankert <openrat@jandankert.de>
Date: Fri, 15 Apr 2022 21:36:28 +0200
New: Configuration values may be overridden by environment variables.
Diffstat:
3 files changed, 31 insertions(+), 3 deletions(-)
diff --git a/dev-helper/docker/openrat-dev/config-dev.yml b/dev-helper/docker/openrat-dev/config-dev.yml
@@ -1,6 +1,6 @@
# OpenRat CMS configuration for development in docker
login:
- motd: "${env:CMS_MOTD}"
+ motd: ""
database-default:
default-id: db
database:
@@ -16,7 +16,7 @@ database:
suffix : _or
log:
file : ""
- level: "${env:CMS_LOG_LEVEL}"
+ level: "debug"
stdout: true
production: ${env:CMS_PRODUCTION}
diff --git a/dev-helper/docker/openrat-dev/docker-compose.yml b/dev-helper/docker/openrat-dev/docker-compose.yml
@@ -31,7 +31,7 @@ services:
DB_USER: cms
DB_PASS: dsfg77er35fsd084351
DB_NAME: cms
- CMS_MOTD:
+ CMS_LOGIN_MOTD: "Welcome, developers."
CMS_NAME: OpenRat Development
CMS_OPERATOR:
CMS_PRODUCTION: "false"
diff --git a/modules/configuration/ConfigurationLoader.class.php b/modules/configuration/ConfigurationLoader.class.php
@@ -75,6 +75,9 @@ class ConfigurationLoader
// resolve variables
$customConfig = self::resolveVariables($customConfig);
+ // enrich with environment variables
+ $customConfig = self::enrichEnvironmentVariables($customConfig, getenv('CMS_CONFIG_PREFIX')?:'CMS');
+
// Does we have includes?
if (isset($customConfig['include'])) {
@@ -135,5 +138,30 @@ class ConfigurationLoader
return $resolver->resolveVariablesInArray($config);
}
+ /**
+ * Test for environment variables.
+ * @param $prefix string|array prefix
+ * @return array
+ */
+ private function enrichEnvironmentVariables($config, $prefix)
+ {
+ foreach ( $config as $key=>$value ) {
+ $newKey = array_merge( (array)$prefix,[$key] );
+ if ( is_array($value) ) {
+ $value = $this->enrichEnvironmentVariables($value,$newKey );
+ } else {
+ $envKey = strtoupper( implode('_',$newKey ) );
+ //error_log( "get env ".$envKey );
+ $value = getenv( $envKey ) ?: $value;
+ if ( in_array(strtolower($value),['true ','on' ]) )
+ $value = true;
+ if ( in_array(strtolower($value),['false','off']) )
+ $value = false;
+ }
+ $config[$key] = $value;
+ }
+ return $config;
+ }
+
}