Make explicit calls to renderStringWithVariables

This commit is contained in:
Thomas Gelf 2016-10-22 05:48:09 +00:00
parent 37c91050c7
commit b7eaab715b
5 changed files with 40 additions and 25 deletions

View File

@ -38,6 +38,10 @@ class CustomVariableString extends CustomVariable
public function toConfigString($renderExpressions = false) public function toConfigString($renderExpressions = false)
{ {
return c::renderString($this->getValue(), $renderExpressions); if ($renderExpressions) {
return c::renderStringWithVariables($this->getValue());
} else {
return c::renderString($this->getValue());
}
} }
} }

View File

@ -74,7 +74,7 @@ class IcingaConfigHelper
// TODO: Find out how to allow multiline {{{...}}} strings. // TODO: Find out how to allow multiline {{{...}}} strings.
// Parameter? Dedicated method? Always if \n is found? // Parameter? Dedicated method? Always if \n is found?
public static function renderString($string, $renderExpressions = false) public static function renderString($string)
{ {
$special = array( $special = array(
'/\\\/', '/\\\/',
@ -100,11 +100,7 @@ class IcingaConfigHelper
$string = preg_replace($special, $replace, $string); $string = preg_replace($special, $replace, $string);
if ($renderExpressions) { return '"' . $string . '"';
return self::renderStringWithVariables($string);
} else {
return '"' . $string . '"';
}
} }
public static function renderDictionaryKey($key) public static function renderDictionaryKey($key)
@ -262,15 +258,21 @@ class IcingaConfigHelper
return $seconds . 's'; return $seconds . 's';
} }
private static function renderStringWithVariables($string) { public static function renderStringWithVariables($string)
$string = preg_replace('/(?<!\$)\$([\w\.]+)\$(?!\$)/', '" + ${1} + "', $string); {
$string = '"' . $string . '"'; $string = preg_replace(
'/(?<!\$)\$([\w\.]+)\$(?!\$)/',
'" + ${1} + "',
static::renderString($string)
);
if (substr($string, 0, 5) === '"" + ') { if (substr($string, 0, 5) === '"" + ') {
$string = substr($string, 5); $string = substr($string, 5);
} }
if (substr($string, -5) === ' + ""') { if (substr($string, -5) === ' + ""') {
$string = substr($string, 0, -5); $string = substr($string, 0, -5);
} }
return $string; return $string;
} }
} }

View File

@ -1703,7 +1703,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return $this->renderRelationProperty($relKey, $value); return $this->renderRelationProperty($relKey, $value);
} }
return c::renderKeyValue($key, c::renderString($value, $this->isApplyRule())); return c::renderKeyValue(
$key,
$this->isApplyRule() ?
c::renderStringWithVariables($value) :
c::renderString($value)
);
} }
protected function renderLegacyObjectProperty($key, $value) protected function renderLegacyObjectProperty($key, $value)

View File

@ -83,21 +83,24 @@ class IcingaConfigHelperTest extends BaseTestCase
public function testRenderStringWithVariables() public function testRenderStringWithVariables()
{ {
$this->assertEquals(c::renderString('Before $var$', true), '"Before " + var'); $this->assertEquals(c::renderStringWithVariables('Before $var$'), '"Before " + var');
$this->assertEquals(c::renderString('$var$ After', true), 'var + " After"'); $this->assertEquals(c::renderStringWithVariables('$var$ After'), 'var + " After"');
$this->assertEquals(c::renderString('$var$', true), 'var'); $this->assertEquals(c::renderStringWithVariables('$var$'), 'var');
$this->assertEquals(c::renderString('$$var$$', true), '"$$var$$"'); $this->assertEquals(c::renderStringWithVariables('$$var$$'), '"$$var$$"');
$this->assertEquals(c::renderString('Before $$var$$ After', true), '"Before $$var$$ After"'); $this->assertEquals(c::renderStringWithVariables('Before $$var$$ After'), '"Before $$var$$ After"');
$this->assertEquals( $this->assertEquals(
c::renderString('Before $name$ $name$ After', true), c::renderStringWithVariables('Before $name$ $name$ After'),
'"Before " + name + " " + name + " After"'); '"Before " + name + " " + name + " After"'
);
$this->assertEquals( $this->assertEquals(
c::renderString('Before $var1$ $var2$ After', true), c::renderStringWithVariables('Before $var1$ $var2$ After'),
'"Before " + var1 + " " + var2 + " After"'); '"Before " + var1 + " " + var2 + " After"'
$this->assertEquals(c::renderString('$host.vars.custom$', true), 'host.vars.custom'); );
$this->assertEquals(c::renderString('$var"$', true), '"$var\"$"'); $this->assertEquals(c::renderStringWithVariables('$host.vars.custom$'), 'host.vars.custom');
$this->assertEquals(c::renderStringWithVariables('$var"$'), '"$var\"$"');
$this->assertEquals( $this->assertEquals(
c::renderString('\tI am\rrendering\nproperly\fand I $support$ "multiple" $variables$\$', true), c::renderStringWithVariables('\tI am\rrendering\nproperly\fand I $support$ "multiple" $variables$\$'),
'"\\\\tI am\\\\rrendering\\\\nproperly\\\\fand I " + support + " \"multiple\" " + variables + "\\\\$"'); '"\\\\tI am\\\\rrendering\\\\nproperly\\\\fand I " + support + " \"multiple\" " + variables + "\\\\$"'
);
} }
} }

View File

@ -12,7 +12,8 @@ class IcingaServiceTest extends BaseTestCase
protected $testHostName = '___TEST___host'; protected $testHostName = '___TEST___host';
protected $testServiceName = '___TEST___service'; protected $testServiceName = '___TEST___service';
protected $createdServices = [];
protected $createdServices = array();
public function testUnstoredHostCanBeLazySet() public function testUnstoredHostCanBeLazySet()
{ {