[icinga-checkins] icinga.org: icingaweb2-module-director/master: CustomVariable: render expressions in Arrays...

git at icinga.org git at icinga.org
Mon Nov 14 11:31:32 CET 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Sat Nov 12 15:03:11 2016 +0100

CustomVariable: render expressions in Arrays...

...and introduce a new abstract method

---

 library/Director/CustomVariable/CustomVariable.php          |    3 +++
 library/Director/CustomVariable/CustomVariableArray.php     |    9 +++++++--
 library/Director/CustomVariable/CustomVariableBoolean.php   |    2 +-
 .../Director/CustomVariable/CustomVariableDictionary.php    |    3 ++-
 library/Director/CustomVariable/CustomVariableNull.php      |    2 +-
 library/Director/CustomVariable/CustomVariableNumber.php    |    2 +-
 library/Director/IcingaConfig/IcingaConfigHelper.php        |   11 ++++++++---
 7 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/library/Director/CustomVariable/CustomVariable.php b/library/Director/CustomVariable/CustomVariable.php
index 73f2699..242ae3d 100644
--- a/library/Director/CustomVariable/CustomVariable.php
+++ b/library/Director/CustomVariable/CustomVariable.php
@@ -4,6 +4,7 @@ namespace Icinga\Module\Director\CustomVariable;
 
 use Icinga\Exception\ProgrammingError;
 use Icinga\Module\Director\Db\Cache\PrefetchCache;
+use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
 use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
 use Exception;
 
@@ -81,6 +82,8 @@ abstract class CustomVariable implements IcingaConfigRenderer
 
     abstract public function getValue();
 
+    abstract public function toConfigString($renderExpressions = false);
+
     public function isNew()
     {
         return ! $this->loadedFromDb;
diff --git a/library/Director/CustomVariable/CustomVariableArray.php b/library/Director/CustomVariable/CustomVariableArray.php
index 4919e92..da9a624 100644
--- a/library/Director/CustomVariable/CustomVariableArray.php
+++ b/library/Director/CustomVariable/CustomVariableArray.php
@@ -69,9 +69,14 @@ class CustomVariableArray extends CustomVariable
         return $this;
     }
 
-    public function toConfigString()
+    public function toConfigString($renderExpressions = false)
     {
-        return c::renderArray($this->value);
+        $parts = array();
+        foreach ($this->value as $k => $v) {
+            $parts[] = $v->toConfigString($renderExpressions);
+        }
+
+        return c::renderEscapedArray($parts);
     }
 
     public function __clone()
diff --git a/library/Director/CustomVariable/CustomVariableBoolean.php b/library/Director/CustomVariable/CustomVariableBoolean.php
index 463751a..9953fae 100644
--- a/library/Director/CustomVariable/CustomVariableBoolean.php
+++ b/library/Director/CustomVariable/CustomVariableBoolean.php
@@ -41,7 +41,7 @@ class CustomVariableBoolean extends CustomVariable
         return $this;
     }
 
-    public function toConfigString()
+    public function toConfigString($renderExpressions = false)
     {
         return $this->value ? 'true' : 'false';
     }
diff --git a/library/Director/CustomVariable/CustomVariableDictionary.php b/library/Director/CustomVariable/CustomVariableDictionary.php
index b830048..cd10129 100644
--- a/library/Director/CustomVariable/CustomVariableDictionary.php
+++ b/library/Director/CustomVariable/CustomVariableDictionary.php
@@ -109,8 +109,9 @@ class CustomVariableDictionary extends CustomVariable implements Countable
         return $this->value[$key];
     }
 
-    public function toConfigString()
+    public function toConfigString($renderExpressions = false)
     {
+        // TODO
         return c::renderDictionary($this->value);
     }
 
diff --git a/library/Director/CustomVariable/CustomVariableNull.php b/library/Director/CustomVariable/CustomVariableNull.php
index c2abca3..f87ccfa 100644
--- a/library/Director/CustomVariable/CustomVariableNull.php
+++ b/library/Director/CustomVariable/CustomVariableNull.php
@@ -40,7 +40,7 @@ class CustomVariableNull extends CustomVariable
         return $this;
     }
 
-    public function toConfigString()
+    public function toConfigString($renderExpressions = false)
     {
         return 'null';
     }
diff --git a/library/Director/CustomVariable/CustomVariableNumber.php b/library/Director/CustomVariable/CustomVariableNumber.php
index 156a572..62838a9 100644
--- a/library/Director/CustomVariable/CustomVariableNumber.php
+++ b/library/Director/CustomVariable/CustomVariableNumber.php
@@ -57,7 +57,7 @@ class CustomVariableNumber extends CustomVariable
         return $this;
     }
 
-    public function toConfigString()
+    public function toConfigString($renderExpressions = false)
     {
         if (is_int($this->value)) {
             return (string) $this->value;
diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php
index e1a43f3..d05ba65 100644
--- a/library/Director/IcingaConfig/IcingaConfigHelper.php
+++ b/library/Director/IcingaConfig/IcingaConfigHelper.php
@@ -123,15 +123,20 @@ class IcingaConfigHelper
                 $data[] = self::renderString($entry);
             }
         }
-        $str = '[ ' . implode(', ', $data) . ' ]';
+
+        return static::renderEscapedArray($data);
+    }
+
+    public static function renderEscapedArray($array)
+    {
+        $str = '[ ' . implode(', ', $array) . ' ]';
 
         if (strlen($str) < 60) {
             return $str;
         }
 
         // Prefix for toConfigString?
-        return "[\n    " . implode(",\n    ", $data) . "\n]";
-
+        return "[\n    " . implode(",\n    ", $array) . "\n]";
     }
 
     public static function renderDictionary($dictionary)



More information about the icinga-checkins mailing list