TemplateTree: allow to fetch ancestors...

...for unstored objects
This commit is contained in:
Thomas Gelf 2017-08-21 14:48:08 +02:00
parent 2a836ee884
commit 67c22a51eb
1 changed files with 22 additions and 4 deletions

View File

@ -28,6 +28,8 @@ class TemplateTree
protected $names; protected $names;
protected $templateNameToId;
public function __construct($type, Db $connection) public function __construct($type, Db $connection)
{ {
$this->type = $type; $this->type = $type;
@ -136,13 +138,28 @@ class TemplateTree
// Special, this is for calls from onStore() // Special, this is for calls from onStore()
return $this->getAncestorsById($id); return $this->getAncestorsById($id);
} else { } else {
return []; return $this->getAncestorsForUnstoredObject($object);
// TODO: Figure out whether to implement this:
// throw new NotImplementedError('Not yet');
// return $this->getAncestorsForUnstoredObject($object);
} }
} }
protected function getAncestorsForUnstoredObject(IcingaObject $object)
{
$names = $object->imports()->listImportNames();
$ancestors = [];
foreach ($names as $name) {
$pid = $this->templateNameToId[$name];
$this->getAncestorsById($pid, $ancestors);
// Hint: inheritance order matters
if (false !== ($key = array_search($name, $ancestors))) {
unset($ancestors[$key]);
}
$ancestors[$pid] = $name;
}
return $ancestors;
}
protected function requireObjectMaps() protected function requireObjectMaps()
{ {
if ($this->objectMaps === null) { if ($this->objectMaps === null) {
@ -304,6 +321,7 @@ class TemplateTree
$this->children = $children; $this->children = $children;
$this->rootNodes = $rootNodes; $this->rootNodes = $rootNodes;
$this->names = $names; $this->names = $names;
$this->templateNameToId = array_flip($names);
Benchmark::measure(sprintf('"%s" Template Tree ready', $this->type)); Benchmark::measure(sprintf('"%s" Template Tree ready', $this->type));
} }