DbObjectTypeRegistry: move logic from IcingaObject

This commit is contained in:
Thomas Gelf 2021-08-04 13:18:30 +02:00
parent be9fcfc3c2
commit 66de47b10b
5 changed files with 72 additions and 43 deletions

View File

@ -0,0 +1,53 @@
<?php
namespace Icinga\Module\Director\Data\Db;
use Icinga\Module\Director\Db;
class DbObjectTypeRegistry
{
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);
}
public static function makeObject($type, $properties = [], Db $db = null)
{
/** @var DbObject $class fake hint for the IDE, it's a string */
$class = self::classByType($type);
return $class::create($properties, $db);
}
}

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Import;
use Icinga\Application\Config;
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Forms\ImportSourceForm;
use Icinga\Module\Director\Hook\ImportSourceHook;
@ -30,7 +31,7 @@ class ImportSourceDirectorObject extends ImportSourceHook
$objectClass = $this->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()

View File

@ -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())) {

View File

@ -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);

View File

@ -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();