CustomVariables: improve readability

This commit is contained in:
Thomas Gelf 2016-09-08 13:55:13 +00:00
parent 8a3956b6a2
commit defb8c50c2
1 changed files with 18 additions and 11 deletions

View File

@ -235,22 +235,29 @@ 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()
);
}
$out .= $this->renderSingleVar($key, $var);
}
return $out;
}
protected function renderSingleVar($key, $var)
{
return c::renderKeyValue(
$this->renderKeyName($key),
$var->toConfigString()
);
}
protected function renderKeyName($key)
{
if (preg_match('/^[a-z0-9_]+\d*$/i', $key)) {
return 'vars.' . c::escapeIfReserved($key);
} else {
return 'vars[' . c::renderString($key) . ']';
}
}
public function __get($key)
{
return $this->get($key);