From fbc56df500fe1cb8a3739ea372d978d41f5e4521 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 15 Jun 2015 14:51:06 +0200 Subject: [PATCH] CustomVariableDictionary: rudimentary rendering --- .../CustomVariableDictionary.php | 22 ++++++++++++++++++- .../IcingaConfig/IcingaConfigHelper.php | 12 ++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/library/Director/CustomVariable/CustomVariableDictionary.php b/library/Director/CustomVariable/CustomVariableDictionary.php index 79bd7edd..0ce89b7f 100644 --- a/library/Director/CustomVariable/CustomVariableDictionary.php +++ b/library/Director/CustomVariable/CustomVariableDictionary.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Director\CustomVariable; +use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c; use Countable; class CustomVariableDictionary extends CustomVariable implements Countable @@ -27,6 +28,25 @@ class CustomVariableDictionary extends CustomVariable implements Countable return true; } + public function setValue($value) + { + $new = array(); + + foreach ($value as $key => $val) { + $new[$key] = self::wantCustomVariable($key, $val); + } + + // WTF? + if ($this->value === $new) { + return $this; + } + + $this->value = $new; + $this->setModified(); + + return $this; + } + public function listKeys() { $keys = array_keys($this->value); @@ -53,6 +73,6 @@ class CustomVariableDictionary extends CustomVariable implements Countable public function toConfigString() { - // TODO: Implement toConfigString() method. + return c::renderDictionary($this->value); } } diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php index 92002968..59e870d6 100644 --- a/library/Director/IcingaConfig/IcingaConfigHelper.php +++ b/library/Director/IcingaConfig/IcingaConfigHelper.php @@ -106,6 +106,18 @@ class IcingaConfigHelper } + public static function renderDictionary($dictionary) + { + $vals = array(); + foreach ($dictionary as $key => $value) { + $vals[] = rtrim(self::renderKeyValue(self::renderString($key), $value)); + } + + // Prefix for toConfigString? + return "{\n" . implode("\n", $vals) . "\n}"; + + } + public static function isReserved($string) { return in_array($string, self::$reservedWords);