IcingaObjectGroups: simplify setting groups

This commit is contained in:
Thomas Gelf 2016-03-05 10:39:15 +01:00
parent 67d9d8aa5f
commit 092bb8c3f6
2 changed files with 35 additions and 6 deletions

View File

@ -222,11 +222,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
public function set($key, $value)
{
if ($key === 'groups') {
$this->groups()->set($value);
return $this;
} elseif ($key === 'imports') {
if ($key === 'imports') {
$this->imports()->set($value);
return $this;
@ -488,6 +484,17 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return $vars;
}
public function getGroups()
{
return $this->groups()->listGroupNames();
}
public function setGroups($groups)
{
$this->groups()->set($groups);
return $this;
}
protected function getResolved($what)
{
$func = 'resolve' . $what;

View File

@ -77,17 +77,33 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
public function set($group)
{
if (! is_array($group)) {
$group = array($group);
}
$existing = array_keys($this->groups);
$new = array();
$class = $this->getGroupClass();
foreach ($group as $g) {
$unset = array();
foreach ($group as $k => $g) {
if ($g instanceof $class) {
$new[] = $g->object_name;
} else {
if (empty($g)) {
$unset[] = $k;
continue;
}
$new[] = $g;
}
}
foreach ($unset as $k) {
unset($group[$k]);
}
sort($existing);
sort($new);
if ($existing === $new) {
@ -95,6 +111,12 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
}
$this->groups = array();
if (empty($group)) {
$this->modified = true;
$this->refreshIndex();
return $this;
}
return $this->add($group);
}