From 378423ff1022c7e0921b30d9376f3a17a38c7ee4 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 7 Mar 2016 02:05:10 +0100 Subject: [PATCH] IcingaObjectImports: implement lazy loading reefs #11301 --- .../Director/Objects/IcingaObjectImports.php | 56 +++++++++++++------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/library/Director/Objects/IcingaObjectImports.php b/library/Director/Objects/IcingaObjectImports.php index 919309d7..e8d936fe 100644 --- a/library/Director/Objects/IcingaObjectImports.php +++ b/library/Director/Objects/IcingaObjectImports.php @@ -14,6 +14,8 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer protected $imports = array(); + protected $objects = array(); + protected $modified = false; protected $object; @@ -48,7 +50,9 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer return null; } - return $this->imports[$this->idx[$this->position]]; + return $this->getObject( + $this->imports[$this->idx[$this->position]] + ); } public function key() @@ -69,7 +73,7 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer public function get($key) { if (array_key_exists($key, $this->imports)) { - return $this->imports[$key]; + return $this->getObject($this->imports[$key]); } return null; @@ -166,18 +170,11 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer return $this; } - $connection = $this->object->getConnection(); - if ($import instanceof $class) { - $this->imports[$import->object_name] = $import; + $this->imports[$import->object_name] = $import->object_name; + $this->objects[$import->object_name] = $import; } elseif (is_string($import)) { - if (is_array($this->object->getKeyName())) { - // Services only - $import = $class::load(array('object_name' => $import), $connection); - } else { - $import = $class::load($import, $connection); - } - $this->imports[$import->object_name] = $import; + $this->imports[$import] = $import; } $this->modified = true; @@ -186,6 +183,24 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer return $this; } + protected function getObject($name) + { + if (array_key_exists($name, $this->objects)) { + return $this->objects[$name]; + } + + $connection = $this->object->getConnection(); + $class = $this->getImportClass(); + if (is_array($this->object->getKeyName())) { + // Services only + $import = $class::load(array('object_name' => $name), $connection); + } else { + $import = $class::load($name, $connection); + } + + return $this->objects[$import->object_name] = $import; + } + protected function getImportTableName() { return $this->object->getTableName() . '_inheritance'; @@ -230,9 +245,13 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer ->order('oi.weight'); $class = $this->getImportClass(); - $this->imports = $class::loadAll($connection, $query, 'object_name'); + $this->objects = $class::loadAll($connection, $query, 'object_name'); + foreach ($this->objects as $k => $obj) { + $this->imports[$k] = $k; + } + $this->storedImports = array(); - foreach ($this->imports as $k => $v) { + foreach ($this->objects as $k => $v) { $this->storedImports[$k] = clone($v); } @@ -258,7 +277,8 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer } $weight = 1; - foreach ($this->imports as $import) { + foreach ($this->imports as $importName) { + $import = $this->getObject($importName); $this->object->db->insert( $this->getImportTableName(), array( @@ -277,7 +297,7 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer protected function cloneStored() { $this->storedImports = array(); - foreach ($this->imports as $k => $v) { + foreach ($this->objects as $k => $v) { $this->storedImports[$k] = clone($v); } } @@ -297,8 +317,8 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer { $ret = ''; - foreach ($this->imports as $name => & $o) { - $ret .= ' import ' . c::renderString($o->object_name) . "\n"; + foreach ($this->imports as $name => $o) { + $ret .= ' import ' . c::renderString($name) . "\n"; } if ($ret !== '') {