CustomVariable: lots of rendering improvements

This commit is contained in:
Thomas Gelf 2015-06-11 21:33:47 +02:00
parent 29230ad7b5
commit 00162bccf8
4 changed files with 77 additions and 33 deletions

View File

@ -44,40 +44,26 @@ abstract class CustomVariable
return $this->key;
}
public function getValue()
{
return $this->value;
}
public function setValue($value)
{
if ($value instanceof CustomVariable) {
if (! $this->equals($value)) {
$this->reallySetValue($value);
}
} elseif ($value !== $this->value) {
$this->reallySetValue($value);
}
return $this;
}
protected function reallySetValue($value)
{
$this->modified = true;
$this->value = $value;
}
abstract public function setValue($value);
public function hasBeenModified()
{
return $this->modified;
}
public function setModified($modified = true)
{
$this->modified = $modified;
if (! $this->modified) {
$this->storedValue = clone($this->value);
}
return $this;
}
public function setUnmodified()
{
$this->modified = false;
$this->storedValue = clone($this->value);
return $this;
return $this->setModified(false);
}
abstract public function equals(CustomVariable $var);
@ -89,6 +75,15 @@ abstract class CustomVariable
return ! $this->equals($var);
}
public static function wantCustomVariable($key, $value)
{
if ($value instanceof CustomVariable) {
return $value;
}
return self::create($key, $value);
}
public static function create($key, $value)
{
if (is_string($value)) {

View File

@ -2,6 +2,8 @@
namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\IcingaConfigHelper as c;
class CustomVariableArray extends CustomVariable
{
public function equals(CustomVariable $var)
@ -13,9 +15,34 @@ class CustomVariableArray extends CustomVariable
return $var->getValue() === $this->getValue();
}
public function toConfigString()
public function setValue($value)
{
// TODO: Implement toConfigString() method.
$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 toConfigString()
{
$str = '[ ' . implode(', ', $this->value) . ' ]';
if (strlen($str) < 60) {
return $str;
}
// Prefix for toConfigString?
return "[\n " . implode(",\n ", $this->value) . "\n]";
}
}

View File

@ -11,11 +11,27 @@ class CustomVariableString extends CustomVariable
return $var->getValue() === $this->getValue();
}
public function getValue()
{
return $this->value;
}
public function setValue($value)
{
if (! is_string($value)) {
$value = (string) $value;
}
if ($value !== $this->value) {
$this->value = $value;
$this->setModified();
}
return $this;
}
public function toConfigString()
{
return c::renderKeyValue(
c::escapeIfReserved($this->getKey()),
c::renderString($this->getValue())
);
return c::renderString($this->getValue());
}
}

View File

@ -2,6 +2,8 @@
namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\IcingaConfigHelper as c;
class CustomVariables
{
protected $storedVars = array();
@ -62,7 +64,11 @@ class CustomVariables
$out = '';
foreach ($this->vars as $key => $var) {
$out .= $var->toConfigString();
$out .= c::renderKeyValue(
c::escapeIfReserved($key),
$var->toConfigString(),
' vars.'
);
}
return $out;