[icinga-checkins] icinga.org: icingaweb2-module-director/feature/service-set-12891: TemplateResolver: no more false alarms...

git at icinga.org git at icinga.org
Fri Oct 14 15:44:44 CEST 2016


Module: icingaweb2-module-director
Branch: feature/service-set-12891
Commit: 2df71442403bd7db1d92870d9810092219c70093
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=2df71442403bd7db1d92870d9810092219c70093

Author: Thomas Gelf <thomas at gelf.net>
Date:   Fri Oct 14 11:26:41 2016 +0000

TemplateResolver: no more false alarms...

...when multiple paths lead to the same template

refs #11803

---

 .../Director/Objects/IcingaTemplateResolver.php    |   28 +++++++++++++-------
 library/Director/Web/Form/DirectorObjectForm.php   |   27 +++++++++++--------
 2 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/library/Director/Objects/IcingaTemplateResolver.php b/library/Director/Objects/IcingaTemplateResolver.php
index 20eed6d..b22c688 100644
--- a/library/Director/Objects/IcingaTemplateResolver.php
+++ b/library/Director/Objects/IcingaTemplateResolver.php
@@ -172,12 +172,13 @@ class IcingaTemplateResolver
         return $this->resolveParentNames($name);
     }
 
-    protected function resolveParentNames($name, &$list = array())
+    protected function resolveParentNames($name, &$list = array(), $path = array())
     {
+        $this->assertNotInList($name, $path);
+        $path[$name] = true;
         foreach ($this->listParentNames($name) as $parent) {
-            $this->assertNotInList($parent, $list, $name);
             $list[$parent] = true;
-            $this->resolveParentNames($parent, $list);
+            $this->resolveParentNames($parent, $list, $path);
             unset($list[$parent]);
             $list[$parent] = true;
         }
@@ -185,14 +186,21 @@ class IcingaTemplateResolver
         return array_keys($list);
     }
 
-    protected function resolveParentIds($id = null, &$list = array())
+    protected function resolveParentIds($id = null, &$list = array(), $path = array())
     {
-        foreach ($this->listParentIds($id) as $parent) {
-            if ($id !== null) {
-                $this->assertNotInList($parent, $list, $id);
+        if ($id === null) {
+            if ($check = $this->object->id) {
+                $this->assertNotInList($check, $path);
+                $path[$check] = true;
             }
+        } else {
+            $this->assertNotInList($id, $path);
+            $path[$id] = true;
+        }
+
+        foreach ($this->listParentIds($id) as $parent) {
             $list[$parent] = true;
-            $this->resolveParentIds($parent, $list);
+            $this->resolveParentIds($parent, $list, $path);
             unset($list[$parent]);
             $list[$parent] = true;
         }
@@ -200,11 +208,11 @@ class IcingaTemplateResolver
         return array_keys($list);
     }
 
-    protected function assertNotInList($id, & $list, $root)
+    protected function assertNotInList($id, & $list)
     {
         if (array_key_exists($id, $list)) {
             $list = array_keys($list);
-            array_unshift($list, $root);
+            $list[] = $id;
             throw new NestingError(
                 'Loop detected: %s',
                 implode(' -> ', $this->getNamesForIds($list))
diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php
index 9ab41d9..037e176 100644
--- a/library/Director/Web/Form/DirectorObjectForm.php
+++ b/library/Director/Web/Form/DirectorObjectForm.php
@@ -27,7 +27,7 @@ abstract class DirectorObjectForm extends QuickForm
 
     protected $displayGroups = array();
 
-    protected $resolvedImports = false;
+    protected $resolvedImports;
 
     protected $listUrl;
 
@@ -69,11 +69,10 @@ abstract class DirectorObjectForm extends QuickForm
 
     protected function assertResolvedImports()
     {
-        if ($this->resolvedImports) {
-            return $this;
+        if ($this->resolvedImports !== null) {
+            return $this->resolvedImports;
         }
 
-        $this->resolvedImports = true;
         $object = $this->object;
 
         if (! $object instanceof IcingaObject) {
@@ -88,9 +87,15 @@ abstract class DirectorObjectForm extends QuickForm
                 $object->imports = $el->getValue();
             }
         }
-        $object->resolveUnresolvedRelatedProperties();
 
-        return $this;
+        try {
+            $object->templateResolver()->listResolvedParentIds();
+        } catch (NestingError $e) {
+            $this->addUniqueErrorMessage($e->getMessage());
+            return $this->resolvedImports = false;
+        }
+
+        return $this->resolvedImports = true;
     }
 
     public function isObject()
@@ -273,7 +278,9 @@ abstract class DirectorObjectForm extends QuickForm
 
     protected function handleCustomVars($object, & $values)
     {
-        IcingaObjectFieldLoader::addFieldsToForm($this, $object, $values);
+        if ($this->assertResolvedImports()) {
+            IcingaObjectFieldLoader::addFieldsToForm($this, $object, $values);
+        }
     }
 
     protected function isNew()
@@ -729,11 +736,9 @@ abstract class DirectorObjectForm extends QuickForm
 
         if ($object->hasProperty($name)) {
             if ($resolved && $object->supportsImports()) {
-                $this->assertResolvedImports();
-                try {
+                if ($this->assertResolvedImports()) {
                     $objectProperty = $object->getResolvedProperty($name);
-                } catch (NestingError $e) {
-                    $this->addUniqueErrorMessage($e->getMessage());
+                } else {
                     $objectProperty = $object->$name;
                 }
             } else {



More information about the icinga-checkins mailing list