Objects/*, CVs, others: unify setBeingLoadedFromDb

This commit is contained in:
Thomas Gelf 2021-10-05 22:52:21 +02:00
parent 835d01cdec
commit 57c4dda117
9 changed files with 89 additions and 31 deletions

View File

@ -181,7 +181,7 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
$vars->vars[$row->varname] = CustomVariable::fromDbRow($row);
}
$vars->refreshIndex();
$vars->setUnmodified();
$vars->setBeingLoadedFromDb();
return $vars;
}
@ -192,7 +192,7 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
$vars->vars[$row->varname] = CustomVariable::fromDbRow($row);
}
$vars->refreshIndex();
$vars->setUnmodified();
$vars->setBeingLoadedFromDb();
return $vars;
}
@ -237,7 +237,7 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
}
}
$this->setUnmodified();
$this->setBeingLoadedFromDb();
}
public function get($key)
@ -264,14 +264,16 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
return false;
}
public function setUnmodified()
public function setBeingLoadedFromDb()
{
$this->modified = false;
$this->storedVars = array();
foreach ($this->vars as $key => $var) {
$this->storedVars[$key] = clone($var);
$var->setUnmodified();
$var->setLoadedFromDb();
}
return $this;
}

View File

@ -288,8 +288,6 @@ class ExtensibleSet
array_merge($props, array('property' => $value))
);
}
$this->fromDb['override'] = $this->ownValues;
}
if (! empty($this->plusValues)) {
@ -300,8 +298,6 @@ class ExtensibleSet
array_merge($props, array('property' => $value))
);
}
$this->fromDb['extend'] = $this->ownValues;
}
if (! empty($this->minusValues)) {
@ -312,9 +308,18 @@ class ExtensibleSet
array_merge($props, array('property' => $value))
);
}
$this->fromDb['blacklist'] = $this->ownValues;
}
$this->setBeingLoadedFromDb();
}
public function setBeingLoadedFromDb()
{
$this->fromDb = [
'override' => $this->ownValues ?: [],
'extend' => $this->plusValues ?: [],
'blacklist' => $this->minusValues ?: [],
];
}
public function override($values)

View File

@ -341,6 +341,15 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer
return $arguments->loadFromDb();
}
public function setBeingLoadedFromDb()
{
foreach ($this->arguments as $argument) {
$argument->setBeingLoadedFromDb();
}
$this->refreshIndex();
$this->cloneStored();
}
/**
* @return $this
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
@ -353,8 +362,14 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer
if ($argument->shouldBeRemoved()) {
$deleted[] = $key;
} else {
$argument->set('command_id', $this->object->get('id'));
$argument->store($db);
if ($argument->hasBeenModified()) {
if ($argument->hasBeenLoadedFromDb()) {
$argument->setLoadedProperty('command_id', $this->object->get('id'));
} else {
$argument->set('command_id', $this->object->get('id'));
}
$argument->store($db);
}
}
}

View File

@ -1489,6 +1489,35 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
&& $this->get('object_type') === 'apply';
}
public function setBeingLoadedFromDb()
{
if ($this instanceof ObjectWithArguments && $this->gotArguments()) {
$this->arguments()->setBeingLoadedFromDb();
}
if ($this->supportsImports() && $this->gotImports()) {
$this->imports()->setBeingLoadedFromDb();
}
if ($this->supportsCustomVars() && $this->vars !== null) {
$this->vars()->setBeingLoadedFromDb();
}
if ($this->supportsGroups() && $this->groups !== null) {
$this->groups()->setBeingLoadedFromDb();
}
if ($this->supportsRanges() && $this->ranges === null) {
$this->ranges()->setBeingLoadedFromDb();
}
foreach ($this->loadedRelatedSets as $set) {
$set->setBeingLoadedFromDb();
}
foreach ($this->loadedMultiRelations as $multiRelation) {
$multiRelation->setBeingLoadedFromDb();
}
parent::setBeingLoadedFromDb();
}
/**
* @throws NotFoundError
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException

View File

@ -274,7 +274,7 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
$class = $this->getGroupClass();
$this->groups = $class::loadAll($connection, $query, 'object_name');
$this->cloneStored();
$this->setBeingLoadedFromDb();
return $this;
}
@ -314,12 +314,12 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
)
);
}
$this->cloneStored();
$this->setBeingLoadedFromDb();
return true;
}
protected function cloneStored()
public function setBeingLoadedFromDb()
{
$this->storedGroups = array();
foreach ($this->groups as $k => $v) {
@ -341,7 +341,7 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
if (PrefetchCache::shouldBeUsed()) {
$groups->groups = PrefetchCache::instance()->groups($object);
$groups->cloneStored();
$groups->setBeingLoadedFromDb();
} else {
$groups->loadFromDb();
}

View File

@ -308,7 +308,7 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer
$this->imports = array_combine($keys, $keys);
}
$this->cloneStored();
$this->setBeingLoadedFromDb();
return $this;
}
@ -355,12 +355,12 @@ class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer
]);
}
$this->cloneStored();
$this->setBeingLoadedFromDb();
return true;
}
protected function cloneStored()
public function setBeingLoadedFromDb()
{
$this->storedNames = $this->listImportNames();
$this->modified = false;

View File

@ -323,7 +323,7 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
$class = $this->getRelatedClassName();
$this->relations = $class::loadAll($connection, $query, 'object_name');
$this->cloneStored();
$this->setBeingLoadedFromDb();
return $this;
}
@ -331,13 +331,10 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
public function store()
{
$db = $this->getDb();
$stored = array_keys($this->stored);
$relations = array_keys($this->relations);
$objectId = $this->object->id;
$type = $this->getType();
$type = $this->getType();
$objectCol = $type . '_id';
$relationCol = $this->getRelationIdColumn() . '_id';
@ -369,12 +366,12 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
)
);
}
$this->cloneStored();
$this->setBeingLoadedFromDb();
return true;
}
protected function cloneStored()
public function setBeingLoadedFromDb()
{
$this->stored = array();
foreach ($this->relations as $k => $v) {

View File

@ -211,13 +211,18 @@ class IcingaScheduledDowntimeRanges implements Iterator, Countable, IcingaConfig
->order('o.range_key');
$this->ranges = IcingaScheduledDowntimeRange::loadAll($connection, $query, 'range_key');
$this->storedRanges = array();
$this->setBeingLoadedFromDb();
return $this;
}
public function setBeingLoadedFromDb()
{
$this->storedRanges = [];
foreach ($this->ranges as $key => $range) {
$this->storedRanges[$key] = clone($range);
}
return $this;
}
public function store()

View File

@ -216,13 +216,18 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere
/** @var IcingaTimePeriodRange $class */
$class = $this->getClass();
$this->ranges = $class::loadAll($connection, $query, 'range_key');
$this->storedRanges = array();
$this->setBeingLoadedFromDb();
return $this;
}
public function setBeingLoadedFromDb()
{
$this->storedRanges = [];
foreach ($this->ranges as $key => $range) {
$this->storedRanges[$key] = clone($range);
}
return $this;
}
public function store()