public static function substVars($val, $props = null) { $sbuf = ''; $i = 0; while (true) { $j = strpos($val, self::DELIM_START, $i); if ($j === false) { // no more variables if ($i == 0) { // this is a simple string return $val; } else { // add the tail string which contails no variables and return the result. $sbuf .= substr($val, $i); return $sbuf; } } else { $sbuf .= substr($val, $i, $j - $i); $k = strpos($val, self::DELIM_STOP, $j); if ($k === false) { // LoggerOptionConverter::substVars() has no closing brace. Opening brace return ''; } else { $j += self::DELIM_START_LEN; $key = substr($val, $j, $k - $j); // first try in System properties $replacement = LoggerOptionConverter::getSystemProperty($key, null); // then try props parameter if ($replacement == null and $props !== null) { $replacement = @$props[$key]; } if (!empty($replacement)) { // Do variable substitution on the replacement string // such that we can solve "Hello ${x2}" as "Hello p1" // the where the properties are // x1=p1 // x2=${x1} $recursiveReplacement = LoggerOptionConverter::substVars($replacement, $props); $sbuf .= $recursiveReplacement; } $i = $k + self::DELIM_STOP_LEN; } } } }
/** * Perform variable substitution in string <var>$val</var> from the * values of keys found with the {@link getSystemProperty()} method. * * <p>The variable substitution delimeters are <b>${</b> and <b>}</b>. * * <p>For example, if the "MY_CONSTANT" contains "value", then * the call * <code> * $s = LoggerOptionConverter::substituteVars("Value of key is ${MY_CONSTANT}."); * </code> * will set the variable <i>$s</i> to "Value of key is value.".</p> * * <p>If no value could be found for the specified key, then the * <var>$props</var> parameter is searched, if the value could not * be found there, then substitution defaults to the empty string.</p> * * <p>For example, if {@link getSystemProperty()} cannot find any value for the key * "inexistentKey", then the call * <code> * $s = LoggerOptionConverter::substVars("Value of inexistentKey is [${inexistentKey}]"); * </code> * will set <var>$s</var> to "Value of inexistentKey is []".</p> * * <p>A warn is thrown if <var>$val</var> contains a start delimeter "${" * which is not balanced by a stop delimeter "}" and an empty string is returned.</p> * * @log4j-author Avy Sharell * * @param string $val The string on which variable substitution is performed. * @param array $props * @return string * * @static */ function substVars($val, $props = null) { LoggerLog::debug("LoggerOptionConverter::substVars():val=[{$val}]"); $sbuf = ''; $i = 0; while (true) { $j = strpos($val, LOG4PHP_OPTION_CONVERTER_DELIM_START, $i); if ($j === false) { LoggerLog::debug("LoggerOptionConverter::substVars() no more variables"); // no more variables if ($i == 0) { // this is a simple string LoggerLog::debug("LoggerOptionConverter::substVars() simple string"); return $val; } else { // add the tail string which contails no variables and return the result. $sbuf .= substr($val, $i); LoggerLog::debug("LoggerOptionConverter::substVars():sbuf=[{$sbuf}]. Returning sbuf"); return $sbuf; } } else { $sbuf .= substr($val, $i, $j - $i); LoggerLog::debug("LoggerOptionConverter::substVars():sbuf=[{$sbuf}]:i={$i}:j={$j}."); $k = strpos($val, LOG4PHP_OPTION_CONVERTER_DELIM_STOP, $j); if ($k === false) { LoggerLog::warn("LoggerOptionConverter::substVars() " . "'{$val}' has no closing brace. Opening brace at position {$j}."); return ''; } else { $j += LOG4PHP_OPTION_CONVERTER_DELIM_START_LEN; $key = substr($val, $j, $k - $j); // first try in System properties $replacement = LoggerOptionConverter::getSystemProperty($key, null); // then try props parameter if ($replacement == null and $props !== null) { $replacement = @$props[$key]; } if (!empty($replacement)) { // Do variable substitution on the replacement string // such that we can solve "Hello ${x2}" as "Hello p1" // the where the properties are // x1=p1 // x2=${x1} $recursiveReplacement = LoggerOptionConverter::substVars($replacement, $props); $sbuf .= $recursiveReplacement; } $i = $k + LOG4PHP_OPTION_CONVERTER_DELIM_STOP_LEN; } } } }