IcingaObjectLegacyAssignments: rendering for old...
...assignments still to be found in our activity log
This commit is contained in:
parent
6e83a4fd5b
commit
39be7b2cb0
|
@ -386,6 +386,11 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||
return $this->supportsSets;
|
||||
}
|
||||
|
||||
public function setAssignments($value)
|
||||
{
|
||||
return IcingaObjectLegacyAssignments::applyToObject($this, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @codingStandardsIgnoreStart
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Objects;
|
||||
|
||||
use Icinga\Exception\ProgrammingError;
|
||||
|
||||
/**
|
||||
* This class is required for historical reasons
|
||||
*
|
||||
* Objects with assignments in your activity log would otherwise not be able
|
||||
* to render themselves
|
||||
*/
|
||||
class IcingaObjectLegacyAssignments
|
||||
{
|
||||
public static function applyToObject(IcingaObject $object, $values)
|
||||
{
|
||||
if (! $object->supportsAssignments()) {
|
||||
throw new ProgrammingError(
|
||||
'I can only assign for applied objects, got %s',
|
||||
$object->object_type
|
||||
);
|
||||
}
|
||||
|
||||
if ($values === null) {
|
||||
return $object;
|
||||
}
|
||||
|
||||
if (! is_array($values)) {
|
||||
static::throwCompatError();
|
||||
}
|
||||
|
||||
if (empty($values)) {
|
||||
return $object;
|
||||
}
|
||||
|
||||
$assigns = array();
|
||||
$ignores = array();
|
||||
foreach ($values as $type => $value) {
|
||||
if (strpos($value, '|') !== false || strpos($value, '&' !== false)) {
|
||||
$value = '(' . $value . ')';
|
||||
}
|
||||
|
||||
if ($type === 'assign') {
|
||||
$assigns[] = $value;
|
||||
} elseif ($type === 'ignore') {
|
||||
$ignores[] = $value;
|
||||
} else {
|
||||
static::throwCompatError();
|
||||
}
|
||||
}
|
||||
|
||||
$assign = implode('|', $assigns);
|
||||
$ignore = implode('&', $ignores);
|
||||
if (empty($assign)) {
|
||||
$filter = $ignore;
|
||||
} elseif (empty($ignore)) {
|
||||
$filter = $assign;
|
||||
} else {
|
||||
if (count($assigns) === 1) {
|
||||
$filter = $assign . '&' . $ignore;
|
||||
} else {
|
||||
$filter = '(' . $assign . ')&(' . $ignore . ')';
|
||||
}
|
||||
}
|
||||
|
||||
$object->assign_filter = $filter;
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
protected static function throwCompatError()
|
||||
{
|
||||
throw new ProgrammingError(
|
||||
'You ran into an unexpected compatibility issue. Please report'
|
||||
. ' this with details helping us to reproduce this to the'
|
||||
. ' Icinga project'
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue