IcingaObject: add helper methods for relations

This commit is contained in:
Thomas Gelf 2016-10-05 17:58:48 +00:00
parent c4c5706a62
commit d1b7083145
1 changed files with 48 additions and 0 deletions

View File

@ -66,6 +66,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
protected $loadedMultiRelations = array();
/**
* Allows to set properties pointing to related objects by name without
* loading the related object.
*
* @var array
*/
protected $unresolvedRelatedProperties = array();
protected $loadedRelatedSets = array();
@ -117,6 +123,26 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return array_key_exists($property, $this->intervalProperties);
}
public function propertyIsRelation($property)
{
if ($key = $this->stripIdSuffix($property)) {
return $this->hasRelation($key);
} else {
return false;
}
}
protected function stripIdSuffix($key)
{
$end = substr($key, -3);
if ('_id' === $end) {
return $end;
}
return false;
}
public function propertyIsRelatedSet($property)
{
return array_key_exists($property, $this->relatedSets);
@ -215,6 +241,11 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return __NAMESPACE__ . '\\' . $this->relations[$property];
}
protected function getRelationObjectClass($property)
{
return $this->relations[$property];
}
protected function getRelatedObjectName($property, $id)
{
return $this->getRelatedObject($property, $id)->object_name;
@ -317,6 +348,15 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return $this->isApplyRule();
}
/**
* It sometimes makes sense to defer lookups for related properties. This
* kind of lazy-loading allows us to for example set host = 'localhost' and
* render an object even when no such host exists. Think of the activity log,
* one might want to visualize a history host or service template even when
* the related command has been deleted in the meantime.
*
* @return self
*/
public function resolveUnresolvedRelatedProperties()
{
foreach ($this->unresolvedRelatedProperties as $name => $p) {
@ -1291,6 +1331,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
}
protected function renderLegacyRelationProperty($propertyName, $id, $renderKey = null)
{
return $this->renderLegacyObjectProperty(
$renderKey ?: $propertyName,
c::renderString($this->getRelatedObjectName($propertyName, $id))
);
}
// Disabled is a virtual property
protected function renderDisabled()
{