diff --git a/library/Director/Db/Cache/PrefetchCache.php b/library/Director/Db/Cache/PrefetchCache.php index 207b6c6c..44a1ea6e 100644 --- a/library/Director/Db/Cache/PrefetchCache.php +++ b/library/Director/Db/Cache/PrefetchCache.php @@ -7,6 +7,7 @@ use Icinga\Module\Director\CustomVariable\CustomVariable; use Icinga\Module\Director\Db; use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaTemplateResolver; +use Icinga\Module\Director\Resolver\TemplateTree; /** * Central prefetch cache @@ -29,6 +30,8 @@ class PrefetchCache protected $renderedVars = array(); + protected $templateTrees = array(); + public static function initialize(Db $db) { self::$instance = new static($db); @@ -78,6 +81,11 @@ class PrefetchCache return $this->templateResolver($object)->setObject($object)->fetchParents(); } + public function listImportNames(IcingaObject $object) + { + return $this->templateTree($object)->listParentNamesForObject($object); + } + /* Hint: not implemented, this happens in DbObject right now public function byObjectType($type) { @@ -136,6 +144,19 @@ class PrefetchCache return $this->groupsCaches[$key]; } + protected function templateTree(IcingaObject $object) + { + $key = $object->getShortTableName(); + if (! array_key_exists($key, $this->templateTrees)) { + $this->templateTrees[$key] = new TemplateTree( + $key, + $object->getConnection() + ); + } + + return $this->templateTrees[$key]; + } + public function __destruct() { unset($this->groupsCaches); diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index 2d5c30c4..4595a465 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -18,6 +18,7 @@ use Icinga\Module\Director\IcingaConfig\IcingaConfig; use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer; use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c; use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1; +use Icinga\Module\Director\Resolver\TemplateTree; abstract class IcingaObject extends DbObject implements IcingaConfigRenderer { @@ -130,6 +131,8 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer private $templateResolver; + protected static $tree; + /** * @return Db */ @@ -2526,9 +2529,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer if ($this->supportsImports()) { if ($resolved) { - $props['imports'] = array(); + $props['imports'] = []; } else { - $props['imports'] = $this->imports()->listImportNames(); + $props['imports'] = $this->listImportNames(); } } @@ -2587,6 +2590,15 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer return (object) $props; } + public function listImportNames() + { + if (PrefetchCache::shouldBeUsed()) { + return PrefetchCache::instance()->listImportNames($this); + } else { + return $this->imports()->listImportNames(); + } + } + protected function differsFromDefaultValue($key, $value) { if (array_key_exists($key, $this->defaultProperties)) {