CustomVariable: render expressions in Arrays...

...and introduce a new abstract method
This commit is contained in:
Thomas Gelf 2016-11-12 15:03:11 +01:00
parent d32f22d493
commit a6928a8bc1
7 changed files with 23 additions and 9 deletions

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\CustomVariable;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Db\Cache\PrefetchCache; use Icinga\Module\Director\Db\Cache\PrefetchCache;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer; use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
use Exception; use Exception;
@ -81,6 +82,8 @@ abstract class CustomVariable implements IcingaConfigRenderer
abstract public function getValue(); abstract public function getValue();
abstract public function toConfigString($renderExpressions = false);
public function isNew() public function isNew()
{ {
return ! $this->loadedFromDb; return ! $this->loadedFromDb;

View File

@ -69,9 +69,14 @@ class CustomVariableArray extends CustomVariable
return $this; return $this;
} }
public function toConfigString() public function toConfigString($renderExpressions = false)
{ {
return c::renderArray($this->value); $parts = array();
foreach ($this->value as $k => $v) {
$parts[] = $v->toConfigString($renderExpressions);
}
return c::renderEscapedArray($parts);
} }
public function __clone() public function __clone()

View File

@ -41,7 +41,7 @@ class CustomVariableBoolean extends CustomVariable
return $this; return $this;
} }
public function toConfigString() public function toConfigString($renderExpressions = false)
{ {
return $this->value ? 'true' : 'false'; return $this->value ? 'true' : 'false';
} }

View File

@ -109,8 +109,9 @@ class CustomVariableDictionary extends CustomVariable implements Countable
return $this->value[$key]; return $this->value[$key];
} }
public function toConfigString() public function toConfigString($renderExpressions = false)
{ {
// TODO
return c::renderDictionary($this->value); return c::renderDictionary($this->value);
} }

View File

@ -40,7 +40,7 @@ class CustomVariableNull extends CustomVariable
return $this; return $this;
} }
public function toConfigString() public function toConfigString($renderExpressions = false)
{ {
return 'null'; return 'null';
} }

View File

@ -57,7 +57,7 @@ class CustomVariableNumber extends CustomVariable
return $this; return $this;
} }
public function toConfigString() public function toConfigString($renderExpressions = false)
{ {
if (is_int($this->value)) { if (is_int($this->value)) {
return (string) $this->value; return (string) $this->value;

View File

@ -123,15 +123,20 @@ class IcingaConfigHelper
$data[] = self::renderString($entry); $data[] = self::renderString($entry);
} }
} }
$str = '[ ' . implode(', ', $data) . ' ]';
return static::renderEscapedArray($data);
}
public static function renderEscapedArray($array)
{
$str = '[ ' . implode(', ', $array) . ' ]';
if (strlen($str) < 60) { if (strlen($str) < 60) {
return $str; return $str;
} }
// Prefix for toConfigString? // Prefix for toConfigString?
return "[\n " . implode(",\n ", $data) . "\n]"; return "[\n " . implode(",\n ", $array) . "\n]";
} }
public static function renderDictionary($dictionary) public static function renderDictionary($dictionary)