diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php index 71e4a5bc..c163bfb0 100644 --- a/library/Director/IcingaConfig/IcingaConfigHelper.php +++ b/library/Director/IcingaConfig/IcingaConfigHelper.php @@ -100,7 +100,7 @@ class IcingaConfigHelper $string = preg_replace($special, $replace, $string); - return '"' . $string . '"'; + return '"' . self::renderVariablesAsExpression($string) . '"'; } public static function renderDictionaryKey($key) @@ -257,4 +257,8 @@ class IcingaConfigHelper return $seconds . 's'; } + + private static function renderVariablesAsExpression($string) { + return preg_replace('/\$\$([\w\.]+)\$\$/', '" + ${1} + "', $string); + } } diff --git a/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php b/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php index c73b5bb8..80075370 100644 --- a/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php +++ b/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php @@ -69,4 +69,30 @@ class IcingaConfigHelperTest extends BaseTestCase { return file_get_contents(__DIR__ . '/rendered/' . $name . '.out'); } + + public function testRenderStringIsCorrectlyRendered() + { + $this->assertEquals(c::renderString('val1\\\val2'), '"val1\\\\\\\\val2"'); + $this->assertEquals(c::renderString('"val1"'), '"\"val1\""'); + $this->assertEquals(c::renderString('\$val\$'), '"\\\\$val\\\\$"'); + $this->assertEquals(c::renderString('\t'), '"\\\\t"'); + $this->assertEquals(c::renderString('\r'), '"\\\\r"'); + $this->assertEquals(c::renderString('\n'), '"\\\\n"'); + $this->assertEquals(c::renderString('\f'), '"\\\\f"'); + } + + public function testRenderStringWithVariables() + { + $this->assertEquals( + c::renderString('Before $$name$$ $$name$$ After'), + '"Before " + name + " " + name + " After"'); + $this->assertEquals( + c::renderString('Before $$var1$$ $$var2$$ After'), + '"Before " + var1 + " " + var2 + " After"'); + $this->assertEquals(c::renderString('$$host.vars.custom$$'), '"" + host.vars.custom + ""'); + $this->assertEquals(c::renderString('$$var"$$'), '"$$var\"$$"'); + $this->assertEquals( + c::renderString('\tI am\rrendering\nproperly\fand I $$support$$ "multiple" $$variables$$\$'), + '"\\\\tI am\\\\rrendering\\\\nproperly\\\\fand I " + support + " \"multiple\" " + variables + "\\\\$"'); + } }