CustomVariables: use array notation for keys...
...with special characters
This commit is contained in:
parent
247ff40459
commit
4e083d7d40
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue