From e683c99a1b3044e5cad5f48cc02c451b3571ad71 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 11 Aug 2017 17:10:31 +0200 Subject: [PATCH] IcingaTemplateRepository: new template lookup... ...strategy, trying to separate concerns --- .../Repository/IcingaTemplateRepository.php | 60 +++++++++++++++++++ .../Repository/RepositoryByObjectHelper.php | 60 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 library/Director/Repository/IcingaTemplateRepository.php create mode 100644 library/Director/Repository/RepositoryByObjectHelper.php diff --git a/library/Director/Repository/IcingaTemplateRepository.php b/library/Director/Repository/IcingaTemplateRepository.php new file mode 100644 index 00000000..3d2ef83e --- /dev/null +++ b/library/Director/Repository/IcingaTemplateRepository.php @@ -0,0 +1,60 @@ +tree === null) { + $this->tree = new TemplateTree($this->type, $this->connection); + } + + return $this->tree; + } + + /** + * @param IcingaObject $object + * @return IcingaObject[] + */ + public function getTemplatesFor(IcingaObject $object) + { + $ids = $this->tree()->listAncestorIdsFor($object); + $templates = []; + foreach ($ids as $id) { + if (! array_key_exists($id, $this->loadedById)) { + // TODO: load only missing ones at once + $this->loadedById[$id] = $object::loadWithAutoIncId( + $id, + $this->connection + ); + } + + $templates[$id] = $this->loadedById[$id]; + } + + return $templates; + } + + public function persistImportNames() + { + } + + public function storeChances(Db $db) + { + } +} diff --git a/library/Director/Repository/RepositoryByObjectHelper.php b/library/Director/Repository/RepositoryByObjectHelper.php new file mode 100644 index 00000000..15276b20 --- /dev/null +++ b/library/Director/Repository/RepositoryByObjectHelper.php @@ -0,0 +1,60 @@ +type = $type; + $this->connection = $connection; + } + + /** + * @param $type + * @param Db $connection + * @return static + */ + public static function instanceByType($type, Db $connection) + { + if (!array_key_exists($type, self::$instances)) { + self::$instances[$type] = new static($type, $connection); + } + + return self::$instances[$type]; + } + + /** + * @param IcingaObject $object + * @return static + */ + public static function instanceByObject(IcingaObject $object, Db $connection = null) + { + if (null === $connection) { + $connection = $object->getConnection(); + } + + if (! $connection) { + var_dump($object->hasBeenLoadedFromDb()); exit; + echo '
';
+            debug_print_backtrace();
+            echo '
'; + throw new \Exception('SDFA'); + } + return static::instanceByType( + $object->getShortTableName(), + $connection + ); + } +}