IcingaObjectImports: simplify code

This commit is contained in:
Thomas Gelf 2016-10-14 10:33:46 +00:00
parent 041fd52174
commit cfbf40147c

View File

@ -2,24 +2,27 @@
namespace Icinga\Module\Director\Objects; namespace Icinga\Module\Director\Objects;
use Countable;
use Exception;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Iterator; use Iterator;
use Countable;
use Icinga\Module\Director\Db\Cache\PrefetchCache;
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c; use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1; use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer
{ {
protected $storedImports = array(); protected $storedNames = array();
/** @var Array A list of our imports, key and value are the import name */
protected $imports = array(); protected $imports = array();
/** @var IcingaObject[] A list of all objects we have seen, referred by name */
protected $objects = array(); protected $objects = array();
protected $modified = false; protected $modified = false;
/** @var IcingaObject The parent object */
protected $object; protected $object;
private $position = 0; private $position = 0;
@ -251,7 +254,7 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer
public function listOriginalImportNames() public function listOriginalImportNames()
{ {
return array_keys($this->storedImports); return $this->storedNames;
} }
public function getType() public function getType()
@ -259,34 +262,21 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer
return $this->object->getShortTableName(); return $this->object->getShortTableName();
} }
// TODO: prefetch
protected function loadFromDb() protected function loadFromDb()
{ {
$resolver = $this->object->templateResolver(); $resolver = $this->object->templateResolver();
// Force nesting error
$resolver->listResolvedParentIds();
$this->objects = $resolver->fetchParents(); $this->objects = $resolver->fetchParents();
$this->imports = array(); if (empty($this->objects)) {
foreach ($this->objects as $k => $obj) { $this->imports = array();
$this->imports[$k] = $k; } else {
$keys = array_keys($this->objects);
$this->imports = array_combine($keys, $keys);
} }
$this->cloneStored(); $this->cloneStored();
return $this; return $this;
} }
protected function loadFromPrefetchCache()
{
$this->storedImports = $this->objects = PrefetchCache::instance()->imports($this->object);
$this->imports = array();
foreach ($this->objects as $o) {
$this->imports[$o->object_name] = $o->object_name;
}
return $this;
}
public function store() public function store()
{ {
if (! $this->hasBeenModified()) { if (! $this->hasBeenModified()) {
@ -298,19 +288,19 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer
$objectCol = $type . '_id'; $objectCol = $type . '_id';
$importCol = 'parent_' . $type . '_id'; $importCol = 'parent_' . $type . '_id';
$table = $this->getImportTableName();
if ($this->object->hasBeenLoadedFromDb()) { if ($this->object->hasBeenLoadedFromDb()) {
$this->object->db->delete( $this->object->db->delete(
$this->getImportTableName(), $table,
$objectCol . ' = ' . $objectId $objectCol . ' = ' . $objectId
); );
} }
$weight = 1; $weight = 1;
foreach ($this->imports as $importName) { foreach ($this->getObjects() as $import) {
$import = $this->getObject($importName);
$this->object->db->insert( $this->object->db->insert(
$this->getImportTableName(), $table,
array( array(
$objectCol => $objectId, $objectCol => $objectId,
$importCol => $import->id, $importCol => $import->id,
@ -326,10 +316,7 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer
protected function cloneStored() protected function cloneStored()
{ {
$this->storedImports = array(); $this->storedNames = $this->listImportNames();
foreach ($this->objects as $k => $v) {
$this->storedImports[$k] = clone($v);
}
$this->modified = false; $this->modified = false;
} }
@ -340,12 +327,7 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer
public static function loadForStoredObject(IcingaObject $object) public static function loadForStoredObject(IcingaObject $object)
{ {
$imports = new static($object); return (new static($object))->loadFromDb();
if (PrefetchCache::shouldBeUsed()) {
return $imports->loadFromPrefetchCache();
} else {
return $imports->loadFromDb();
}
} }
public function toConfigString() public function toConfigString()
@ -398,9 +380,7 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer
public function __destruct() public function __destruct()
{ {
unset($this->storedImport);
unset($this->imports);
unset($this->objects);
unset($this->object); unset($this->object);
unset($this->objects);
} }
} }