[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaObject: add helper methods for relations

git at icinga.org git at icinga.org
Wed Oct 5 22:13:57 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: d1b7083145aa10c87d47200556752393ff750227
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=d1b7083145aa10c87d47200556752393ff750227

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Oct  5 17:58:48 2016 +0000

IcingaObject: add helper methods for relations

---

 library/Director/Objects/IcingaObject.php |   48 +++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php
index e390821..a8fc622 100644
--- a/library/Director/Objects/IcingaObject.php
+++ b/library/Director/Objects/IcingaObject.php
@@ -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()
     {



More information about the icinga-checkins mailing list