IcingaObject: resolving methods should be generic

This commit is contained in:
Thomas Gelf 2015-07-30 11:39:00 +02:00
parent 5b3806532f
commit 3d205724c3
1 changed files with 96 additions and 14 deletions

View File

@ -139,37 +139,119 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return $this->importedObjects; return $this->importedObjects;
} }
public function clearImportedObjects()
{
$this->importedObjects = null;
return $this;
}
public function getResolvedProperties() public function getResolvedProperties()
{ {
$res = $this->resolveProperties(); return $this->getResolved('Properties');
return $res['_MERGED_']; }
public function getInheritedProperties()
{
return $this->getInherited('Properties');
} }
public function resolveProperties() public function resolveProperties()
{ {
$props = array(); return $this->resolve('Properties');
$props['_MERGED_'] = (object) array(); }
$objects = $this->importedObjects();
public function getResolvedFields()
{
return $this->getResolved('Fields');
}
public function getInheritedFields()
{
return $this->getInherited('Fields');
}
public function resolveFields()
{
return $this->resolve('Fields');
}
public function getResolvedVars()
{
return $this->getResolved('Vars');
}
public function getInheritedVars()
{
return $this->getInherited('Vars');
}
public function resolveVars()
{
return $this->resolve('Vars');
}
public function getVars()
{
$vars = (object) array();
foreach ($this->vars() as $key => $var) {
$vars->$key = $var->getValue();
}
return $vars;
}
protected function getResolved($what)
{
$func = 'resolve' . $what;
$res = $this->$func();
return $res['_MERGED_'];
}
protected function getInherited($what)
{
$func = 'resolve' . $what;
$res = $this->$func();
return $res['_INHERITED_'];
}
protected function resolve($what)
{
$vals = array();
$vals['_MERGED_'] = (object) array();
$vals['_INHERITED_'] = (object) array();
$objects = $this->importedObjects();
$objects[$this->object_name] = $this; $objects[$this->object_name] = $this;
$blacklist = array('id', 'object_type', 'object_name');
$get = 'get' . $what;
$getResolved = 'getResolved' . $what;
if ($what === 'Properties') {
$blacklist = array('id', 'object_type', 'object_name');
} else {
$blacklist = array();
}
foreach ($objects as $name => $object) { foreach ($objects as $name => $object) {
$props[$name] = (object) array(); $vals[$name] = (object) array();
if ($name === $this->object_name) {
$pprops = $object->getProperties(); if ($name === $this->object_name || $this->object_name === null) {
$pvals = $object->$get();
} else { } else {
$pprops = $object->getResolvedProperties(); $pvals = $object->$getResolved();
} }
foreach ($pprops as $key => $value) { foreach ($pvals as $key => $value) {
if (in_array($key, $blacklist)) continue; if (in_array($key, $blacklist)) continue;
if ($value !== null) { if ($value !== null) {
$props[$name]->$key = $value; $vals[$name]->$key = $value;
$props['_MERGED_']->$key = $value; $vals['_MERGED_']->$key = $value;
if ($name !== $this->object_name) {
$vals['_INHERITED_']->$key = $value;
}
} }
} }
} }
return $props; return $vals;
} }
protected function assertCustomVarsSupport() protected function assertCustomVarsSupport()