CustomVariables: use array notation for keys...

...with special characters
This commit is contained in:
Thomas Gelf 2016-07-22 18:07:56 +02:00
parent 247ff40459
commit 4e083d7d40
2 changed files with 53 additions and 4 deletions

View File

@ -234,10 +234,18 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
ksort($this->vars);
foreach ($this->vars as $key => $var) {
// 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;

View File

@ -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();
}
}