Commit 06a5420d authored by Navid Sassan's avatar Navid Sassan
Browse files

added first prototype of merge when using sync rules

parent a56ff2b8
......@@ -25,6 +25,8 @@ abstract class CustomVariable implements IcingaConfigRenderer
protected $deleted = false;
protected $supportsMerging = false;
protected $checksum;
protected function __construct($key, $value = null)
......@@ -283,4 +285,15 @@ abstract class CustomVariable implements IcingaConfigRenderer
die();
}
}
/**
* Whether this CustomVariable supports merging
*
* @return bool
*/
public function supportsMerging()
{
return $this->supportsMerging;
}
}
......@@ -10,6 +10,8 @@ class CustomVariableArray extends CustomVariable
/** @var CustomVariable[] */
protected $value;
protected $supportsMerging = true;
public function equals(CustomVariable $var)
{
if (! $var instanceof CustomVariableArray) {
......@@ -97,4 +99,13 @@ class CustomVariableArray extends CustomVariable
{
return c1::renderArray($this->value);
}
public function merge(CustomVariableArray $other)
{
if ($this->equals($other)) {
return $this;
}
$this->value = array_unique(array_merge($value, $other));
return $this;
}
}
......@@ -153,6 +153,52 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
return $this;
}
/**
* TODO
*
* @param string $key
* @param mixed $value
*
* @return self
*/
public function mergeOrSet($key, $value)
{
$key = (string) $key;
if ($value instanceof CustomVariable) {
$value = clone($value);
} else {
if ($value === null) {
$this->__unset($key);
return $this;
}
$value = CustomVariable::create($key, $value);
}
// Hint: isset($this->$key) wouldn't conflict with protected properties
if ($this->__isset($key)) {
if ($value->equals($this->get($key))) {
return $this;
} else {
if (get_class($this->vars[$key]) === get_class($value)) {
if ($value->supportsMerging()) {
$value->merge($this->vars[$key]);
}
$this->vars[$key]->setValue($value->getValue())->setModified();
} else {
$this->vars[$key] = $value->setLoadedFromDb()->setModified();
}
}
} else {
$this->vars[$key] = $value->setModified();
}
$this->modified = true;
$this->refreshIndex();
return $this;
}
protected function refreshIndex()
{
$this->idx = array();
......
......@@ -2746,7 +2746,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
} else {
/** @var CustomVariables $vars */
foreach ($vars as $key => $var) {
$myVars->set($key, $var);
$myVars->mergeOrSet($key, $var);
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment