mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-09-21 17:07:54 +02:00
DbObjectTypeRegistry: move logic from IcingaObject
This commit is contained in:
parent
be9fcfc3c2
commit
66de47b10b
53
library/Director/Data/Db/DbObjectTypeRegistry.php
Normal file
53
library/Director/Data/Db/DbObjectTypeRegistry.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
namespace Icinga\Module\Director\Import;
|
namespace Icinga\Module\Director\Import;
|
||||||
|
|
||||||
use Icinga\Application\Config;
|
use Icinga\Application\Config;
|
||||||
|
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
|
||||||
use Icinga\Module\Director\Db;
|
use Icinga\Module\Director\Db;
|
||||||
use Icinga\Module\Director\Forms\ImportSourceForm;
|
use Icinga\Module\Director\Forms\ImportSourceForm;
|
||||||
use Icinga\Module\Director\Hook\ImportSourceHook;
|
use Icinga\Module\Director\Hook\ImportSourceHook;
|
||||||
@ -30,7 +31,7 @@ class ImportSourceDirectorObject extends ImportSourceHook
|
|||||||
$objectClass = $this->getSetting('object_class');
|
$objectClass = $this->getSetting('object_class');
|
||||||
$objectType = $this->getSetting('object_type');
|
$objectType = $this->getSetting('object_type');
|
||||||
/** @var IcingaObject $class fake type hint, it's a string */
|
/** @var IcingaObject $class fake type hint, it's a string */
|
||||||
$class = IcingaObject::classByType($objectClass);
|
$class = DbObjectTypeRegistry::classByType($objectClass);
|
||||||
if ($objectType) {
|
if ($objectType) {
|
||||||
$dummy = $class::create();
|
$dummy = $class::create();
|
||||||
$query = $db->getDbAdapter()->select()
|
$query = $db->getDbAdapter()->select()
|
||||||
|
@ -9,6 +9,7 @@ use Icinga\Data\Filter\FilterExpression;
|
|||||||
use Icinga\Exception\NotFoundError;
|
use Icinga\Exception\NotFoundError;
|
||||||
use Icinga\Module\Director\CustomVariable\CustomVariables;
|
use Icinga\Module\Director\CustomVariable\CustomVariables;
|
||||||
use Icinga\Module\Director\Data\Db\DbDataFormatter;
|
use Icinga\Module\Director\Data\Db\DbDataFormatter;
|
||||||
|
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
|
||||||
use Icinga\Module\Director\IcingaConfig\AssignRenderer;
|
use Icinga\Module\Director\IcingaConfig\AssignRenderer;
|
||||||
use Icinga\Module\Director\Data\Db\DbObject;
|
use Icinga\Module\Director\Data\Db\DbObject;
|
||||||
use Icinga\Module\Director\Db\Cache\PrefetchCache;
|
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)
|
public static function prefetchAllRelationsByType($type, Db $db)
|
||||||
{
|
{
|
||||||
/** @var static $class */
|
/** @var static $class */
|
||||||
$class = self::classByType($type);
|
$class = DbObjectTypeRegistry::classByType($type);
|
||||||
/** @var static $dummy */
|
/** @var static $dummy */
|
||||||
$dummy = $class::create([], $db);
|
$dummy = $class::create([], $db);
|
||||||
$dummy->prefetchAllRelatedTypes();
|
$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)
|
public static function classByType($type)
|
||||||
{
|
{
|
||||||
// allow for icinga_host and host
|
return DbObjectTypeRegistry::classByType($type);
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2569,7 +2542,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||||||
public static function createByType($type, $properties = [], Db $db = null)
|
public static function createByType($type, $properties = [], Db $db = null)
|
||||||
{
|
{
|
||||||
/** @var IcingaObject $class */
|
/** @var IcingaObject $class */
|
||||||
$class = self::classByType($type);
|
$class = DbObjectTypeRegistry::classByType($type);
|
||||||
return $class::create($properties, $db);
|
return $class::create($properties, $db);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2584,7 +2557,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||||||
public static function loadByType($type, $id, Db $db)
|
public static function loadByType($type, $id, Db $db)
|
||||||
{
|
{
|
||||||
/** @var IcingaObject $class */
|
/** @var IcingaObject $class */
|
||||||
$class = self::classByType($type);
|
$class = DbObjectTypeRegistry::classByType($type);
|
||||||
return $class::load($id, $db);
|
return $class::load($id, $db);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2598,7 +2571,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||||||
public static function existsByType($type, $id, Db $db)
|
public static function existsByType($type, $id, Db $db)
|
||||||
{
|
{
|
||||||
/** @var IcingaObject $class */
|
/** @var IcingaObject $class */
|
||||||
$class = self::classByType($type);
|
$class = DbObjectTypeRegistry::classByType($type);
|
||||||
return $class::exists($id, $db);
|
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)
|
public static function loadAllByType($type, Db $db, $query = null, $keyColumn = null)
|
||||||
{
|
{
|
||||||
/** @var DbObject $class */
|
/** @var DbObject $class */
|
||||||
$class = self::classByType($type);
|
$class = DbObjectTypeRegistry::classByType($type);
|
||||||
|
|
||||||
if ($keyColumn === null) {
|
if ($keyColumn === null) {
|
||||||
if (method_exists($class, 'getKeyColumnName')) {
|
if (method_exists($class, 'getKeyColumnName')) {
|
||||||
@ -2645,7 +2618,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||||||
public static function loadAllExternalObjectsByType($type, Db $db)
|
public static function loadAllExternalObjectsByType($type, Db $db)
|
||||||
{
|
{
|
||||||
/** @var IcingaObject $class */
|
/** @var IcingaObject $class */
|
||||||
$class = self::classByType($type);
|
$class = DbObjectTypeRegistry::classByType($type);
|
||||||
$dummy = $class::create();
|
$dummy = $class::create();
|
||||||
|
|
||||||
if (is_array($dummy->getKeyName())) {
|
if (is_array($dummy->getKeyName())) {
|
||||||
|
@ -8,6 +8,7 @@ use Icinga\Data\Filter\FilterExpression;
|
|||||||
use Icinga\Exception\ProgrammingError;
|
use Icinga\Exception\ProgrammingError;
|
||||||
use Icinga\Module\Director\Application\MemoryLimit;
|
use Icinga\Module\Director\Application\MemoryLimit;
|
||||||
use Icinga\Module\Director\Data\AssignFilterHelper;
|
use Icinga\Module\Director\Data\AssignFilterHelper;
|
||||||
|
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
|
||||||
use Icinga\Module\Director\Db;
|
use Icinga\Module\Director\Db;
|
||||||
use Icinga\Module\Director\Db\Cache\PrefetchCache;
|
use Icinga\Module\Director\Db\Cache\PrefetchCache;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
@ -127,7 +128,7 @@ abstract class ObjectApplyMatches
|
|||||||
Benchmark::measure("ObjectApplyMatches: prefetching $type");
|
Benchmark::measure("ObjectApplyMatches: prefetching $type");
|
||||||
PrefetchCache::initialize($db);
|
PrefetchCache::initialize($db);
|
||||||
/** @var IcingaObject $class */
|
/** @var IcingaObject $class */
|
||||||
$class = IcingaObject::classByType($type);
|
$class = DbObjectTypeRegistry::classByType($type);
|
||||||
$all = $class::prefetchAll($db);
|
$all = $class::prefetchAll($db);
|
||||||
Benchmark::measure("ObjectApplyMatches: related objects for $type");
|
Benchmark::measure("ObjectApplyMatches: related objects for $type");
|
||||||
$class::prefetchAllRelationsByType($type, $db);
|
$class::prefetchAllRelationsByType($type, $db);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace Tests\Icinga\Module\Director\Objects;
|
namespace Tests\Icinga\Module\Director\Objects;
|
||||||
|
|
||||||
use Icinga\Exception\NotFoundError;
|
use Icinga\Exception\NotFoundError;
|
||||||
|
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
|
||||||
use Icinga\Module\Director\Exception\DuplicateKeyException;
|
use Icinga\Module\Director\Exception\DuplicateKeyException;
|
||||||
use Icinga\Module\Director\Objects\HostGroupMembershipResolver;
|
use Icinga\Module\Director\Objects\HostGroupMembershipResolver;
|
||||||
use Icinga\Module\Director\Objects\IcingaObject;
|
use Icinga\Module\Director\Objects\IcingaObject;
|
||||||
@ -83,7 +84,7 @@ class HostGroupMembershipResolverTest extends BaseTestCase
|
|||||||
protected function objects($type)
|
protected function objects($type)
|
||||||
{
|
{
|
||||||
/** @var IcingaObject $class */
|
/** @var IcingaObject $class */
|
||||||
$class = IcingaObject::classByType($type);
|
$class = DbObjectTypeRegistry::classByType($type);
|
||||||
|
|
||||||
/** @var IcingaObject $dummy */
|
/** @var IcingaObject $dummy */
|
||||||
$dummy = $class::create();
|
$dummy = $class::create();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user