ExtensibleSet: allow to load stored data

This commit is contained in:
Thomas Gelf 2016-02-29 15:39:25 +01:00
parent df60644786
commit bbcac7a00d
1 changed files with 137 additions and 0 deletions

View File

@ -3,7 +3,9 @@
namespace Icinga\Module\Director\IcingaConfig;
use Icinga\Exception\InvalidPropertyException;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\Objects\IcingaObject;
class ExtensibleSet
{
@ -19,6 +21,22 @@ class ExtensibleSet
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)
{
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)
{
$this->ownValues = array();
@ -110,6 +225,7 @@ class ExtensibleSet
$prefix
);
}
if (! empty($this->minusValues)) {
$parts[] = c::renderKeyOperatorValue(
$key,
@ -122,6 +238,22 @@ class ExtensibleSet
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()
{
return $this->resolvedValues !== null;
@ -221,6 +353,11 @@ class ExtensibleSet
return $this;
}
protected function translate($string)
{
return mt('director', $string);
}
protected function wantArray($values)
{
if (is_array($values)) {