diff --git a/library/Director/CustomVariable/CustomVariable.php b/library/Director/CustomVariable/CustomVariable.php index d78fcb75..2d0761b8 100644 --- a/library/Director/CustomVariable/CustomVariable.php +++ b/library/Director/CustomVariable/CustomVariable.php @@ -67,6 +67,15 @@ abstract class CustomVariable implements IcingaConfigRenderer return $this->getValue(); } + public function toJson() + { + if ($this->getDbFormat() === 'string') { + return json_encode($this->getDbValue()); + } else { + return $this->getDbValue(); + } + } + // TODO: abstract public function getDbFormat() { @@ -91,6 +100,34 @@ abstract class CustomVariable implements IcingaConfigRenderer ); } + public function flatten(array & $flat, $prefix) + { + $flat[$prefix] = $this->getDbValue(); + } + + public function render($renderExpressions = false) + { + return c::renderKeyValue( + $this->renderKeyName($this->getKey()), + $this->toConfigStringPrefetchable($renderExpressions) + ); + } + + 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 checksum() + { + // TODO: remember checksum, invalidate on change + return sha1($this->getKey() . '=' . $this->toJson(), true); + } + public function isNew() { return ! $this->loadedFromDb; diff --git a/library/Director/CustomVariable/CustomVariableArray.php b/library/Director/CustomVariable/CustomVariableArray.php index da9a624c..c5428ba1 100644 --- a/library/Director/CustomVariable/CustomVariableArray.php +++ b/library/Director/CustomVariable/CustomVariableArray.php @@ -69,6 +69,13 @@ class CustomVariableArray extends CustomVariable return $this; } + public function flatten(array & $flat, $prefix) + { + foreach ($this->value as $k => $v) { + $v->flatten($flat, sprintf('%s[%d]', $prefix, $k)); + } + } + public function toConfigString($renderExpressions = false) { $parts = array(); diff --git a/library/Director/CustomVariable/CustomVariableDictionary.php b/library/Director/CustomVariable/CustomVariableDictionary.php index cd101290..c5ce7bda 100644 --- a/library/Director/CustomVariable/CustomVariableDictionary.php +++ b/library/Director/CustomVariable/CustomVariableDictionary.php @@ -75,6 +75,13 @@ class CustomVariableDictionary extends CustomVariable implements Countable return $ret; } + public function flatten(array & $flat, $prefix) + { + foreach ($this->value as $k => $v) { + $v->flatten($flat, sprintf('%s["%s"]', $prefix, $k)); + } + } + public function listKeys() { $keys = array_keys($this->value); diff --git a/library/Director/CustomVariable/CustomVariableString.php b/library/Director/CustomVariable/CustomVariableString.php index cbc5bc6d..530ca4ce 100644 --- a/library/Director/CustomVariable/CustomVariableString.php +++ b/library/Director/CustomVariable/CustomVariableString.php @@ -37,6 +37,12 @@ class CustomVariableString extends CustomVariable return $this; } + public function flatten(array & $flat, $prefix) + { + // TODO: we should get rid of type=string and always use JSON + $flat[$prefix] = json_encode($this->getValue()); + } + public function toConfigString($renderExpressions = false) { if ($renderExpressions) { diff --git a/library/Director/CustomVariable/CustomVariables.php b/library/Director/CustomVariable/CustomVariables.php index 05e10933..4275c50b 100644 --- a/library/Director/CustomVariable/CustomVariables.php +++ b/library/Director/CustomVariable/CustomVariables.php @@ -273,6 +273,26 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer return $this->storedVars; } + public function flatten() + { + $flat = array(); + foreach ($this->vars as $key => $var) { + $var->flatten($flat, $key); + } + + return $flat; + } + + public function checksum() + { + $sums = array(); + foreach ($this->vars as $key => $var) { + $sums[] = $key . '=' . $var->checksum(); + } + + return sha1(implode('|', $sums), true); + } + public function toConfigString($renderExpressions = false) { $out = '';