mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-31 01:34:12 +02:00
ExtensibleSet: allow to load stored data
This commit is contained in:
parent
df60644786
commit
bbcac7a00d
@ -3,7 +3,9 @@
|
|||||||
namespace Icinga\Module\Director\IcingaConfig;
|
namespace Icinga\Module\Director\IcingaConfig;
|
||||||
|
|
||||||
use Icinga\Exception\InvalidPropertyException;
|
use Icinga\Exception\InvalidPropertyException;
|
||||||
|
use Icinga\Exception\ProgrammingError;
|
||||||
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
|
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
|
||||||
|
use Icinga\Module\Director\Objects\IcingaObject;
|
||||||
|
|
||||||
class ExtensibleSet
|
class ExtensibleSet
|
||||||
{
|
{
|
||||||
@ -19,6 +21,22 @@ class ExtensibleSet
|
|||||||
|
|
||||||
protected $inheritedValues = array();
|
protected $inheritedValues = array();
|
||||||
|
|
||||||
|
protected $fromDb = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var IcingaObject
|
||||||
|
*/
|
||||||
|
protected $object;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Object property name pointing to this set
|
||||||
|
*
|
||||||
|
* This also implies set table called <object_table>_<propertyName>_set
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $propertyName;
|
||||||
|
|
||||||
public function __construct($values = null)
|
public function __construct($values = null)
|
||||||
{
|
{
|
||||||
if (null !== $values) {
|
if (null !== $values) {
|
||||||
@ -26,6 +44,103 @@ class ExtensibleSet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function forIcingaObject(IcingaObject $object, $propertyName)
|
||||||
|
{
|
||||||
|
$set = new static;
|
||||||
|
|
||||||
|
if ($set->foreignKey === null) {
|
||||||
|
throw new ProgrammingError(
|
||||||
|
'ExtensibleSet::forIcingaObject requires implementations with a defined $foreignKey'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($object->hasBeenLoadedFromDb()) {
|
||||||
|
$set->object = $object;
|
||||||
|
$set->loadFromDb();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasBeenLoadedFromDb()
|
||||||
|
{
|
||||||
|
return $this->fromDb !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasBeenModified()
|
||||||
|
{
|
||||||
|
if ($this->hasBeenLoadedFromDb()) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if ($this->ownValues === null
|
||||||
|
&& empty($this->plusValues)
|
||||||
|
&& empty($this->minusValues)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function loadFromDb()
|
||||||
|
{
|
||||||
|
$db = $this->object->getDb();
|
||||||
|
|
||||||
|
$query = $db->select()->from(
|
||||||
|
$this->tableName(),
|
||||||
|
array(
|
||||||
|
'property',
|
||||||
|
'merge_behaviour'
|
||||||
|
)
|
||||||
|
)->where($this->foreignKey() . ' = ?', $this->object->id);
|
||||||
|
|
||||||
|
$byBehaviour = array(
|
||||||
|
'override' => array(),
|
||||||
|
'extend' => array(),
|
||||||
|
'blacklist' => array(),
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($db->fetchAll($query) as $row) {
|
||||||
|
if (! array_key_exists($row->merge_behaviour, byBehaviour)) {
|
||||||
|
throw new ProgrammingError(
|
||||||
|
'Got unknown merge_behaviour "%s". Schema change?',
|
||||||
|
$row->merge_behaviour
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$byBehaviour[$row->merge_behaviour][] = $row->property;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($byBehaviour as $method => &$values) {
|
||||||
|
sort($values);
|
||||||
|
$this->$method($values);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($byBehaviour['override'])) {
|
||||||
|
$byBehaviour['override'] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fromDb = $byBehaviour;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function foreignKey()
|
||||||
|
{
|
||||||
|
return $this->object->getShortTableName() . '_id';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tableName()
|
||||||
|
{
|
||||||
|
return implode(
|
||||||
|
'_',
|
||||||
|
$this->object->getTableName(),
|
||||||
|
$this->propertyName,
|
||||||
|
'set'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function override($values)
|
public function override($values)
|
||||||
{
|
{
|
||||||
$this->ownValues = array();
|
$this->ownValues = array();
|
||||||
@ -110,6 +225,7 @@ class ExtensibleSet
|
|||||||
$prefix
|
$prefix
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! empty($this->minusValues)) {
|
if (! empty($this->minusValues)) {
|
||||||
$parts[] = c::renderKeyOperatorValue(
|
$parts[] = c::renderKeyOperatorValue(
|
||||||
$key,
|
$key,
|
||||||
@ -122,6 +238,22 @@ class ExtensibleSet
|
|||||||
return implode('', $parts);
|
return implode('', $parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isRestricted()
|
||||||
|
{
|
||||||
|
return $this->allowedValues === null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function enumAllowedValues()
|
||||||
|
{
|
||||||
|
if ($this->isRestricted()) {
|
||||||
|
throw new ProgrammingError(
|
||||||
|
'No allowed value set available, this set is not restricted'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_combine($this->allowedValues, $this->allowedValues);
|
||||||
|
}
|
||||||
|
|
||||||
protected function hasBeenResolved()
|
protected function hasBeenResolved()
|
||||||
{
|
{
|
||||||
return $this->resolvedValues !== null;
|
return $this->resolvedValues !== null;
|
||||||
@ -221,6 +353,11 @@ class ExtensibleSet
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function translate($string)
|
||||||
|
{
|
||||||
|
return mt('director', $string);
|
||||||
|
}
|
||||||
|
|
||||||
protected function wantArray($values)
|
protected function wantArray($values)
|
||||||
{
|
{
|
||||||
if (is_array($values)) {
|
if (is_array($values)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user