[icinga-checkins] icinga.org: icingaweb2-module-director/master: CustomVariables: use array notation for keys...

git at icinga.org git at icinga.org
Sat Jul 23 10:13:39 CEST 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Fri Jul 22 18:07:56 2016 +0200

CustomVariables: use array notation for keys...

...with special characters

---

 .../Director/CustomVariable/CustomVariables.php    |   16 ++++++--
 .../CustomVariable/CustomVariablesTest.php         |   41 ++++++++++++++++++++
 2 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/library/Director/CustomVariable/CustomVariables.php b/library/Director/CustomVariable/CustomVariables.php
index 351819c..a7b4c0c 100644
--- a/library/Director/CustomVariable/CustomVariables.php
+++ b/library/Director/CustomVariable/CustomVariables.php
@@ -234,10 +234,18 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
 
         ksort($this->vars);
         foreach ($this->vars as $key => $var) {
-            $out .= c::renderKeyValue(
-                'vars.' . c::escapeIfReserved($key),
-                $var->toConfigString()
-            );
+            // TODO: ctype_alnum + underscore?
+            if (preg_match('/^[a-z0-9_]+\d*$/i', $key)) {
+                $out .= c::renderKeyValue(
+                    'vars.' . c::escapeIfReserved($key),
+                    $var->toConfigString()
+                );
+            } else {
+                $out .= c::renderKeyValue(
+                    'vars[' . c::renderString($key) . ']',
+                    $var->toConfigString()
+                );
+            }
         }
 
         return $out;
diff --git a/test/php/library/Director/CustomVariable/CustomVariablesTest.php b/test/php/library/Director/CustomVariable/CustomVariablesTest.php
new file mode 100644
index 0000000..3c00686
--- /dev/null
+++ b/test/php/library/Director/CustomVariable/CustomVariablesTest.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Tests\Icinga\Module\Director\CustomVariable;
+
+use Icinga\Module\Director\CustomVariable\CustomVariables;
+use Icinga\Module\Director\Test\BaseTestCase;
+
+class CustomVariablesTest extends BaseTestCase
+{
+    protected $indent = '    ';
+
+    public function testWhetherSpecialKeyNames()
+    {
+        $vars = $this->newVars();
+        $vars->bla = 'da';
+        $vars->{'aBc'} = 'normal';
+        $vars->{'a-0'} = 'special';
+        $expected = $this->indentVarsList(array(
+            'vars["a-0"] = "special"',
+            'vars.aBc = "normal"',
+            'vars.bla = "da"'
+        ));
+        $this->assertEquals(
+            $vars->toConfigString(),
+            $expected
+        );
+    }
+
+    protected function indentVarsList($vars)
+    {
+        return $this->indent . implode(
+            "\n" . $this->indent,
+            $vars
+        ) . "\n";
+    }
+
+    protected function newVars()
+    {
+        return new CustomVariables();
+    }
+}



More information about the icinga-checkins mailing list