From 66de47b10b753ed1f3e376533d6e674ed8ff02db Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 4 Aug 2021 13:18:30 +0200 Subject: [PATCH] DbObjectTypeRegistry: move logic from IcingaObject --- .../Director/Data/Db/DbObjectTypeRegistry.php | 53 +++++++++++++++++++ .../Import/ImportSourceDirectorObject.php | 3 +- library/Director/Objects/IcingaObject.php | 53 +++++-------------- .../Director/Objects/ObjectApplyMatches.php | 3 +- .../HostGroupMembershipResolverTest.php | 3 +- 5 files changed, 72 insertions(+), 43 deletions(-) create mode 100644 library/Director/Data/Db/DbObjectTypeRegistry.php diff --git a/library/Director/Data/Db/DbObjectTypeRegistry.php b/library/Director/Data/Db/DbObjectTypeRegistry.php new file mode 100644 index 00000000..a09c32d5 --- /dev/null +++ b/library/Director/Data/Db/DbObjectTypeRegistry.php @@ -0,0 +1,53 @@ +getSetting('object_class'); $objectType = $this->getSetting('object_type'); /** @var IcingaObject $class fake type hint, it's a string */ - $class = IcingaObject::classByType($objectClass); + $class = DbObjectTypeRegistry::classByType($objectClass); if ($objectType) { $dummy = $class::create(); $query = $db->getDbAdapter()->select() diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index 5f27de97..a51f5bbd 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -9,6 +9,7 @@ use Icinga\Data\Filter\FilterExpression; use Icinga\Exception\NotFoundError; use Icinga\Module\Director\CustomVariable\CustomVariables; use Icinga\Module\Director\Data\Db\DbDataFormatter; +use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry; use Icinga\Module\Director\IcingaConfig\AssignRenderer; use Icinga\Module\Director\Data\Db\DbObject; use Icinga\Module\Director\Db\Cache\PrefetchCache; @@ -359,7 +360,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer public static function prefetchAllRelationsByType($type, Db $db) { /** @var static $class */ - $class = self::classByType($type); + $class = DbObjectTypeRegistry::classByType($type); /** @var static $dummy */ $dummy = $class::create([], $db); $dummy->prefetchAllRelatedTypes(); @@ -2521,42 +2522,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer )); } + /** + * @deprecated use DbObjectTypeRegistry::classByType() + * @param $type + * @return string + */ public static function classByType($type) { - // allow for icinga_host and host - $type = lcfirst(preg_replace('/^icinga_/', '', $type)); - - // Hint: Sync/Import are not IcingaObjects, this should be reconsidered: - if (strpos($type, 'import') === 0 || strpos($type, 'sync') === 0) { - $prefix = ''; - } elseif (strpos($type, 'data') === false) { - $prefix = 'Icinga'; - } else { - $prefix = 'Director'; - } - - // TODO: Provide a more sophisticated solution - if ($type === 'hostgroup') { - $type = 'hostGroup'; - } elseif ($type === 'usergroup') { - $type = 'userGroup'; - } elseif ($type === 'timeperiod') { - $type = 'timePeriod'; - } elseif ($type === 'servicegroup') { - $type = 'serviceGroup'; - } elseif ($type === 'service_set') { - $type = 'serviceSet'; - } elseif ($type === 'apiuser') { - $type = 'apiUser'; - } elseif ($type === 'host_template_choice') { - $type = 'templateChoiceHost'; - } elseif ($type === 'service_template_choice') { - $type = 'TemplateChoiceService'; - } elseif ($type === 'scheduled_downtime' || $type === 'scheduled-downtime') { - $type = 'ScheduledDowntime'; - } - - return 'Icinga\\Module\\Director\\Objects\\' . $prefix . ucfirst($type); + return DbObjectTypeRegistry::classByType($type); } /** @@ -2569,7 +2542,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer public static function createByType($type, $properties = [], Db $db = null) { /** @var IcingaObject $class */ - $class = self::classByType($type); + $class = DbObjectTypeRegistry::classByType($type); return $class::create($properties, $db); } @@ -2584,7 +2557,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer public static function loadByType($type, $id, Db $db) { /** @var IcingaObject $class */ - $class = self::classByType($type); + $class = DbObjectTypeRegistry::classByType($type); return $class::load($id, $db); } @@ -2598,7 +2571,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer public static function existsByType($type, $id, Db $db) { /** @var IcingaObject $class */ - $class = self::classByType($type); + $class = DbObjectTypeRegistry::classByType($type); return $class::exists($id, $db); } @@ -2610,7 +2583,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer public static function loadAllByType($type, Db $db, $query = null, $keyColumn = null) { /** @var DbObject $class */ - $class = self::classByType($type); + $class = DbObjectTypeRegistry::classByType($type); if ($keyColumn === null) { if (method_exists($class, 'getKeyColumnName')) { @@ -2645,7 +2618,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer public static function loadAllExternalObjectsByType($type, Db $db) { /** @var IcingaObject $class */ - $class = self::classByType($type); + $class = DbObjectTypeRegistry::classByType($type); $dummy = $class::create(); if (is_array($dummy->getKeyName())) { diff --git a/library/Director/Objects/ObjectApplyMatches.php b/library/Director/Objects/ObjectApplyMatches.php index fa4bda87..ee6e6c85 100644 --- a/library/Director/Objects/ObjectApplyMatches.php +++ b/library/Director/Objects/ObjectApplyMatches.php @@ -8,6 +8,7 @@ use Icinga\Data\Filter\FilterExpression; use Icinga\Exception\ProgrammingError; use Icinga\Module\Director\Application\MemoryLimit; use Icinga\Module\Director\Data\AssignFilterHelper; +use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry; use Icinga\Module\Director\Db; use Icinga\Module\Director\Db\Cache\PrefetchCache; use stdClass; @@ -127,7 +128,7 @@ abstract class ObjectApplyMatches Benchmark::measure("ObjectApplyMatches: prefetching $type"); PrefetchCache::initialize($db); /** @var IcingaObject $class */ - $class = IcingaObject::classByType($type); + $class = DbObjectTypeRegistry::classByType($type); $all = $class::prefetchAll($db); Benchmark::measure("ObjectApplyMatches: related objects for $type"); $class::prefetchAllRelationsByType($type, $db); diff --git a/test/php/library/Director/Objects/HostGroupMembershipResolverTest.php b/test/php/library/Director/Objects/HostGroupMembershipResolverTest.php index 902977e0..ffe1ff57 100644 --- a/test/php/library/Director/Objects/HostGroupMembershipResolverTest.php +++ b/test/php/library/Director/Objects/HostGroupMembershipResolverTest.php @@ -3,6 +3,7 @@ namespace Tests\Icinga\Module\Director\Objects; use Icinga\Exception\NotFoundError; +use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry; use Icinga\Module\Director\Exception\DuplicateKeyException; use Icinga\Module\Director\Objects\HostGroupMembershipResolver; use Icinga\Module\Director\Objects\IcingaObject; @@ -83,7 +84,7 @@ class HostGroupMembershipResolverTest extends BaseTestCase protected function objects($type) { /** @var IcingaObject $class */ - $class = IcingaObject::classByType($type); + $class = DbObjectTypeRegistry::classByType($type); /** @var IcingaObject $dummy */ $dummy = $class::create();