[icinga-checkins] icinga.org: icingaweb2-module-director/master: Make explicit calls to renderStringWithVariables

git at icinga.org git at icinga.org
Tue Oct 25 04:39:55 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: b7eaab715b74405f789d88c0b6462be32a732b95
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=b7eaab715b74405f789d88c0b6462be32a732b95

Author: Thomas Gelf <thomas at gelf.net>
Date:   Sat Oct 22 05:48:09 2016 +0000

Make explicit calls to renderStringWithVariables

---

 .../CustomVariable/CustomVariableString.php        |    6 +++-
 .../Director/IcingaConfig/IcingaConfigHelper.php   |   20 ++++++++------
 library/Director/Objects/IcingaObject.php          |    7 ++++-
 .../IcingaConfig/IcingaConfigHelperTest.php        |   29 +++++++++++---------
 .../library/Director/Objects/IcingaServiceTest.php |    3 +-
 5 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/library/Director/CustomVariable/CustomVariableString.php b/library/Director/CustomVariable/CustomVariableString.php
index 6f0677b..1194276 100644
--- a/library/Director/CustomVariable/CustomVariableString.php
+++ b/library/Director/CustomVariable/CustomVariableString.php
@@ -38,6 +38,10 @@ class CustomVariableString extends CustomVariable
 
     public function toConfigString($renderExpressions = false)
     {
-        return c::renderString($this->getValue(), $renderExpressions);
+        if ($renderExpressions) {
+            return c::renderStringWithVariables($this->getValue());
+        } else {
+            return c::renderString($this->getValue());
+        }
     }
 }
diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php
index edc113b..f76ad16 100644
--- a/library/Director/IcingaConfig/IcingaConfigHelper.php
+++ b/library/Director/IcingaConfig/IcingaConfigHelper.php
@@ -74,7 +74,7 @@ class IcingaConfigHelper
 
     // TODO: Find out how to allow multiline {{{...}}} strings.
     //       Parameter? Dedicated method? Always if \n is found?
-    public static function renderString($string, $renderExpressions = false)
+    public static function renderString($string)
     {
         $special = array(
             '/\\\/',
@@ -100,11 +100,7 @@ class IcingaConfigHelper
 
         $string = preg_replace($special, $replace, $string);
 
-        if ($renderExpressions) {
-            return self::renderStringWithVariables($string);
-        } else {
-            return '"' . $string . '"';
-        }
+        return '"' . $string . '"';
     }
 
     public static function renderDictionaryKey($key)
@@ -262,15 +258,21 @@ class IcingaConfigHelper
         return $seconds . 's';
     }
 
-    private static function renderStringWithVariables($string) {
-        $string = preg_replace('/(?<!\$)\$([\w\.]+)\$(?!\$)/', '" + ${1} + "', $string);
-        $string = '"' . $string . '"';
+    public static function renderStringWithVariables($string)
+    {
+        $string = preg_replace(
+            '/(?<!\$)\$([\w\.]+)\$(?!\$)/',
+            '" + ${1} + "',
+            static::renderString($string)
+        );
+
         if (substr($string, 0, 5) === '"" + ') {
             $string = substr($string, 5);
         }
         if (substr($string, -5) === ' + ""') {
             $string = substr($string, 0, -5);
         }
+
         return $string;
     }
 }
diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php
index c4d92c9..6d1a7cb 100644
--- a/library/Director/Objects/IcingaObject.php
+++ b/library/Director/Objects/IcingaObject.php
@@ -1703,7 +1703,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
             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)
diff --git a/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php b/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php
index ff2b861..bb5955a 100644
--- a/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php
+++ b/test/php/library/Director/IcingaConfig/IcingaConfigHelperTest.php
@@ -83,21 +83,24 @@ class IcingaConfigHelperTest extends BaseTestCase
 
     public function testRenderStringWithVariables()
     {
-        $this->assertEquals(c::renderString('Before $var$', true), '"Before " + var');
-        $this->assertEquals(c::renderString('$var$ After', true), 'var + " After"');
-        $this->assertEquals(c::renderString('$var$', true), 'var');
-        $this->assertEquals(c::renderString('$$var$$', true), '"$$var$$"');
-        $this->assertEquals(c::renderString('Before $$var$$ After', true), '"Before $$var$$ After"');
+        $this->assertEquals(c::renderStringWithVariables('Before $var$'), '"Before " + var');
+        $this->assertEquals(c::renderStringWithVariables('$var$ After'), 'var + " After"');
+        $this->assertEquals(c::renderStringWithVariables('$var$'), 'var');
+        $this->assertEquals(c::renderStringWithVariables('$$var$$'), '"$$var$$"');
+        $this->assertEquals(c::renderStringWithVariables('Before $$var$$ After'), '"Before $$var$$ After"');
         $this->assertEquals(
-            c::renderString('Before $name$ $name$ After', true),
-            '"Before " + name + " " + name + " After"');
+            c::renderStringWithVariables('Before $name$ $name$ After'),
+            '"Before " + name + " " + name + " After"'
+        );
         $this->assertEquals(
-            c::renderString('Before $var1$ $var2$ After', true),
-            '"Before " + var1 + " " + var2 + " After"');
-        $this->assertEquals(c::renderString('$host.vars.custom$', true), 'host.vars.custom');
-        $this->assertEquals(c::renderString('$var"$', true), '"$var\"$"');
+            c::renderStringWithVariables('Before $var1$ $var2$ After'),
+            '"Before " + var1 + " " + var2 + " After"'
+        );
+        $this->assertEquals(c::renderStringWithVariables('$host.vars.custom$'), 'host.vars.custom');
+        $this->assertEquals(c::renderStringWithVariables('$var"$'), '"$var\"$"');
         $this->assertEquals(
-            c::renderString('\tI am\rrendering\nproperly\fand I $support$ "multiple" $variables$\$', true),
-            '"\\\\tI am\\\\rrendering\\\\nproperly\\\\fand I " + support + " \"multiple\" " + variables + "\\\\$"');
+            c::renderStringWithVariables('\tI am\rrendering\nproperly\fand I $support$ "multiple" $variables$\$'),
+            '"\\\\tI am\\\\rrendering\\\\nproperly\\\\fand I " + support + " \"multiple\" " + variables + "\\\\$"'
+        );
     }
 }
diff --git a/test/php/library/Director/Objects/IcingaServiceTest.php b/test/php/library/Director/Objects/IcingaServiceTest.php
index efbc18c..07fea36 100644
--- a/test/php/library/Director/Objects/IcingaServiceTest.php
+++ b/test/php/library/Director/Objects/IcingaServiceTest.php
@@ -12,7 +12,8 @@ class IcingaServiceTest extends BaseTestCase
     protected $testHostName = '___TEST___host';
 
     protected $testServiceName = '___TEST___service';
-    protected $createdServices = [];
+
+    protected $createdServices = array();
 
     public function testUnstoredHostCanBeLazySet()
     {



More information about the icinga-checkins mailing list