commit d1bae9818d3ddf4ad831496b798614c8c4366afa
parent c122f04ac950924aac55b8e101b2fa9a0889b254
Author: Jan Dankert <develop@jandankert.de>
Date: Mon, 11 Nov 2019 00:42:26 +0100
Refactoring: Resolver config variables with a separate variable resolver.
Diffstat:
2 files changed, 39 insertions(+), 24 deletions(-)
diff --git a/modules/configuration/ConfigurationLoader.class.php b/modules/configuration/ConfigurationLoader.class.php
@@ -116,31 +116,13 @@ class ConfigurationLoader
*/
private function resolveVariables($value)
{
- return preg_replace_callback(
- "|\\$\{([[:alnum:]]+)\:([[:alnum:]_]+)\}|",
+ $value = Text::resolveVariables( $value, 'env' , function($var) { return getenv(strtoupper($var)); } );
- function ($match)
- {
- $type = $match[1];
- $value = $match[2];
- $value = str_replace('-', '_', $value);
-
- switch( strtolower( $type ) )
- {
- case 'env':
- return getenv(strtoupper($value));
-
- case 'http': // http:... is a shortcut for server:http-...
- return @$_SERVER['HTTP_' . strtoupper($value)];
-
- case 'server':
- return @$_SERVER[strtoupper($value)];
- default:
- return "";
- }
- },
-
- $value);
+ // http:... is a shortcut for server:http-...
+ $value = Text::resolveVariables( $value,'http' , function($var) { return @$_SERVER['HTTP_' . strtoupper($var)]; } );
+ $value = Text::resolveVariables( $value,'server', function($var) { return @$_SERVER[strtoupper($var)]; } );
+
+ return $value;
}
}
diff --git a/modules/util/Text.class.php b/modules/util/Text.class.php
@@ -378,5 +378,38 @@ class Text
return $oids;
}
+
+
+ public static function resolveVariables($value,$key,$resolver)
+ {
+ $begin = '${';
+ $end = '}';
+ $split = ':';
+ $offset = 0;
+
+ while( true )
+ {
+ $pos = strpos($value, $begin . $key . $split, $offset);
+
+ if ($pos === FALSE)
+ return $value;
+
+ $offset = $pos + 1;
+
+ $posEnd = strpos($value, $end, $offset);
+
+ if ($posEnd === FALSE)
+ return $value;
+
+ $name = substr($value, $pos + strlen($begin . $key . $split), $posEnd - strlen($begin . $key . $split) - $pos );
+
+ $varValue = $resolver($name);
+
+ $value = substr($value, 0, $pos) . $varValue . substr($value,$posEnd + strlen($end));
+ }
+
+ return $value;
+ }
+
}