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; return $this->key;
} }
public function getValue() abstract public function setValue($value);
{
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;
}
public function hasBeenModified() public function hasBeenModified()
{ {
return $this->modified; return $this->modified;
} }
public function setModified($modified = true)
{
$this->modified = $modified;
if (! $this->modified) {
$this->storedValue = clone($this->value);
}
return $this;
}
public function setUnmodified() public function setUnmodified()
{ {
$this->modified = false; return $this->setModified(false);
$this->storedValue = clone($this->value);
return $this;
} }
abstract public function equals(CustomVariable $var); abstract public function equals(CustomVariable $var);
@ -89,6 +75,15 @@ abstract class CustomVariable
return ! $this->equals($var); 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) public static function create($key, $value)
{ {
if (is_string($value)) { if (is_string($value)) {

View File

@ -2,6 +2,8 @@
namespace Icinga\Module\Director\CustomVariable; namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\IcingaConfigHelper as c;
class CustomVariableArray extends CustomVariable class CustomVariableArray extends CustomVariable
{ {
public function equals(CustomVariable $var) public function equals(CustomVariable $var)
@ -13,9 +15,34 @@ class CustomVariableArray extends CustomVariable
return $var->getValue() === $this->getValue(); 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(); 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() public function toConfigString()
{ {
return c::renderKeyValue( return c::renderString($this->getValue());
c::escapeIfReserved($this->getKey()),
c::renderString($this->getValue())
);
} }
} }

View File

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