[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaObject: start to dry out relation-related...

git at icinga.org git at icinga.org
Mon Oct 10 12:00:29 CEST 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Sun Oct  9 12:48:13 2016 +0000

IcingaObject: start to dry out relation-related...

...code. Still weird and hard to understand because of lazy-loading,
can only get better. All this is required to be able to render objects
having obsolete dependencies in our activity log

---

 library/Director/Objects/IcingaObject.php |   96 +++++++++++++++++++++--------
 1 file changed, 72 insertions(+), 24 deletions(-)

diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php
index 4c73bb0..1063d0c 100644
--- a/library/Director/Objects/IcingaObject.php
+++ b/library/Director/Objects/IcingaObject.php
@@ -126,6 +126,13 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
         return array_key_exists($property, $this->intervalProperties);
     }
 
+    /**
+     * Whether a property ends with _id and might refer another object
+     *
+     * @param $property string Property name, like zone_id
+     *
+     * @return bool
+     */
     public function propertyIsRelation($property)
     {
         if ($key = $this->stripIdSuffix($property)) {
@@ -140,7 +147,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
         $end = substr($key, -3);
 
         if ('_id' === $end) {
-            return $end;
+            return substr($key, 0, -3);
         }
 
         return false;
@@ -234,6 +241,15 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
         return $sets;
     }
 
+    /**
+     * Whether the given property name is a short name for a relation
+     *
+     * This might be 'zone' for 'zone_id'
+     *
+     * @param string $property Property name
+     *
+     * @return bool
+     */
     public function hasRelation($property)
     {
         return array_key_exists($property, $this->relations);
@@ -431,21 +447,33 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
         return array_key_exists($name, $this->unresolvedRelatedProperties);
     }
 
-    public function get($key)
+    protected function getRelationId($key)
     {
-        if ($this->hasUnresolvedRelatedProperty($key . '_id')) {
-            return $this->unresolvedRelatedProperties[$key . '_id'];
+        if ($this->hasUnresolvedRelatedProperty($key)) {
+            $this->resolveUnresolvedRelatedProperty($key);
         }
 
-        if (substr($key, -3) === '_id') {
-            $short = substr($key, 0, -3);
-            if ($this->hasRelation($short)) {
-                if ($this->hasUnresolvedRelatedProperty($key)) {
-                    $this->resolveUnresolvedRelatedProperty($key);
-                }
-            }
+        return parent::get($key);
+    }
+
+    protected function getRelatedProperty($key)
+    {
+        $idKey = $key . '_id';
+        if ($this->hasUnresolvedRelatedProperty($idKey)) {
+            return $this->unresolvedRelatedProperties[$idKey];
+        }
+
+        if ($id = $this->get($idKey)) {
+            $class = $this->getRelationClass($key);
+            $object = $class::loadWithAutoIncId($id, $this->connection);
+            return $object->object_name;
         }
 
+        return null;
+    }
+
+    public function get($key)
+    {
         if (substr($key, 0, 5) === 'vars.') {
             $var = $this->vars()->get(substr($key, 5));
             if ($var === null) {
@@ -455,14 +483,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
             }
         }
 
-        if ($this->hasRelation($key)) {
-            if ($id = $this->get($key . '_id')) {
-                $class = $this->getRelationClass($key);
-                $object = $class::loadWithAutoIncId($id, $this->connection);
-                return $object->object_name;
-            }
+        // e.g. zone_id
+        if ($this->propertyIsRelation($key)) {
+            return $this->getRelationId($key);
+        }
 
-            return null;
+        // e.g. zone
+        if ($this->hasRelation($key)) {
+            return $this->getRelatedProperty($key);
         }
 
         if ($this->propertyIsRelatedSet($key)) {
@@ -518,13 +546,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
             return parent::set($key, $this->normalizeBoolean($value));
         }
 
-        if ($this->hasRelation($key)) {
-            if (strlen($value) === 0) {
-                return parent::set($key . '_id', null);
-            }
+        // e.g. zone_id
+        if ($this->propertyIsRelation($key)) {
+            return $this->setRelation($key, $value);
+        }
 
-            $this->unresolvedRelatedProperties[$key . '_id'] = $value;
-            return $this;
+        // e.g. zone
+        if ($this->hasRelation($key)) {
+            return $this->setUnresolvedRelation($key, $value);
         }
 
         if ($this->propertyIsMultiRelation($key)) {
@@ -544,6 +573,25 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
         return parent::set($key, $value);
     }
 
+    private function setRelation($key, $value)
+    {
+        if ((int) $key !== (int) $this->$key) {
+            unset($this->unresolvedRelatedProperties[$key]);
+        }
+        return parent::set($key, $value);
+    }
+
+    private function setUnresolvedRelation($key, $value)
+    {
+        if (strlen($value) === 0) {
+            unset($this->unresolvedRelatedProperties[$key . '_id']);
+            return parent::set($key . '_id', null);
+        }
+
+        $this->unresolvedRelatedProperties[$key . '_id'] = $value;
+        return $this;
+    }
+
     protected function setRanges($ranges)
     {
         $this->ranges()->set((array) $ranges);



More information about the icinga-checkins mailing list