2015-06-01 16:28:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Objects;
|
|
|
|
|
2015-10-16 18:42:38 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
|
2018-06-19 13:44:21 +02:00
|
|
|
use RuntimeException;
|
2015-10-16 18:42:38 +02:00
|
|
|
|
2016-03-21 10:12:49 +01:00
|
|
|
class IcingaCommandArgument extends IcingaObject
|
2015-06-01 16:28:40 +02:00
|
|
|
{
|
2015-07-02 14:31:35 +02:00
|
|
|
protected $keyName = 'id';
|
|
|
|
|
2015-06-01 16:28:40 +02:00
|
|
|
protected $table = 'icinga_command_argument';
|
|
|
|
|
2017-09-02 23:29:06 +02:00
|
|
|
protected $supportsImports = false;
|
|
|
|
|
2015-12-02 02:51:17 +01:00
|
|
|
protected $booleans = array(
|
|
|
|
'skip_key' => 'skip_key',
|
2016-03-21 10:12:49 +01:00
|
|
|
'repeat_key' => 'repeat_key',
|
|
|
|
'required' => 'required'
|
2015-12-02 02:51:17 +01:00
|
|
|
);
|
|
|
|
|
2015-06-01 16:28:40 +02:00
|
|
|
protected $defaultProperties = array(
|
2015-08-03 13:53:58 +02:00
|
|
|
'id' => null,
|
|
|
|
'command_id' => null,
|
|
|
|
'argument_name' => null,
|
|
|
|
'argument_value' => null,
|
|
|
|
'argument_format' => null,
|
|
|
|
'key_string' => null,
|
|
|
|
'description' => null,
|
|
|
|
'skip_key' => null,
|
|
|
|
'set_if' => null,
|
|
|
|
'sort_order' => null,
|
|
|
|
'repeat_key' => null,
|
|
|
|
'set_if_format' => null,
|
2016-03-21 10:12:49 +01:00
|
|
|
'required' => null,
|
2015-06-01 16:28:40 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
public function onInsert()
|
|
|
|
{
|
|
|
|
// No log right now, we have to handle "sub-objects"
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onUpdate()
|
|
|
|
{
|
|
|
|
// No log right now, we have to handle "sub-objects"
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onDelete()
|
|
|
|
{
|
|
|
|
// No log right now, we have to handle "sub-objects"
|
|
|
|
}
|
2015-10-16 18:42:38 +02:00
|
|
|
|
2016-08-26 09:00:45 +02:00
|
|
|
public function isSkippingKey()
|
|
|
|
{
|
2018-06-19 13:44:21 +02:00
|
|
|
return $this->get('skip_key') === 'y' || $this->get('argument_name') === null;
|
2016-08-26 09:00:45 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 17:06:48 +02:00
|
|
|
// Preserve is not supported
|
|
|
|
public function replaceWith(IcingaObject $object, $preserve = null)
|
2016-05-19 15:09:08 +02:00
|
|
|
{
|
|
|
|
$this->setProperties((array) $object->toPlainObject(
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
null,
|
|
|
|
false
|
|
|
|
));
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-02 23:29:06 +02:00
|
|
|
protected function makePlainArgumentValue($value, $format)
|
|
|
|
{
|
|
|
|
if ($format === 'expression') {
|
|
|
|
return (object) [
|
|
|
|
'type' => 'Function',
|
|
|
|
// TODO: Not for dummy comment
|
|
|
|
'body' => $value
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
// json or string
|
|
|
|
return $value;
|
2016-03-21 10:12:49 +01:00
|
|
|
}
|
2017-09-02 23:29:06 +02:00
|
|
|
}
|
2016-03-18 13:46:06 +01:00
|
|
|
|
2017-09-02 23:29:06 +02:00
|
|
|
protected function extractValueFromPlain($plain)
|
|
|
|
{
|
|
|
|
if ($plain->argument_value) {
|
|
|
|
return $this->makePlainArgumentValue(
|
|
|
|
$plain->argument_value,
|
|
|
|
$plain->argument_format
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return null;
|
2016-03-18 13:46:06 +01:00
|
|
|
}
|
2017-09-02 23:29:06 +02:00
|
|
|
}
|
2016-03-18 13:46:06 +01:00
|
|
|
|
2017-09-02 23:29:06 +02:00
|
|
|
protected function transformPlainArgumentValue($plain)
|
|
|
|
{
|
|
|
|
if (property_exists($plain, 'argument_value')) {
|
2017-09-05 10:22:01 +02:00
|
|
|
if (property_exists($plain, 'argument_format')) {
|
|
|
|
$format = $plain->argument_format;
|
|
|
|
} else {
|
|
|
|
$format = 'string';
|
|
|
|
}
|
2017-09-02 23:29:06 +02:00
|
|
|
$plain->value = $this->makePlainArgumentValue(
|
|
|
|
$plain->argument_value,
|
2017-09-05 10:22:01 +02:00
|
|
|
$format
|
2017-09-02 23:29:06 +02:00
|
|
|
);
|
|
|
|
unset($plain->argument_value);
|
|
|
|
unset($plain->argument_format);
|
2016-03-21 10:12:49 +01:00
|
|
|
}
|
2017-09-02 23:29:06 +02:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:45:21 +02:00
|
|
|
public function toCompatPlainObject()
|
2017-09-02 23:29:06 +02:00
|
|
|
{
|
|
|
|
$plain = parent::toPlainObject(
|
|
|
|
false,
|
2017-09-04 12:45:21 +02:00
|
|
|
true,
|
2017-09-02 23:29:06 +02:00
|
|
|
null,
|
|
|
|
false
|
|
|
|
);
|
2016-03-21 10:12:49 +01:00
|
|
|
|
2017-09-02 23:29:06 +02:00
|
|
|
unset($plain->id);
|
2017-09-04 12:45:21 +02:00
|
|
|
unset($plain->argument_name);
|
2018-10-15 13:01:39 +02:00
|
|
|
if (! isset($plain->argument_value)) {
|
|
|
|
unset($plain->argument_format);
|
|
|
|
}
|
|
|
|
if (! isset($plain->set_if)) {
|
|
|
|
unset($plain->set_if_format);
|
|
|
|
}
|
2017-09-02 23:29:06 +02:00
|
|
|
|
|
|
|
$this->transformPlainArgumentValue($plain);
|
|
|
|
|
|
|
|
// Will happen only combined with $skipDefaults
|
|
|
|
if (array_keys((array) $plain) === ['value']) {
|
|
|
|
return $plain->value;
|
|
|
|
} else {
|
|
|
|
if (property_exists($plain, 'sort_order') && $plain->sort_order !== null) {
|
|
|
|
$plain->order = $plain->sort_order;
|
|
|
|
unset($plain->sort_order);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $plain;
|
2016-03-21 10:12:49 +01:00
|
|
|
}
|
2017-09-02 23:29:06 +02:00
|
|
|
}
|
2016-03-21 10:12:49 +01:00
|
|
|
|
2017-09-02 23:29:06 +02:00
|
|
|
public function toFullPlainObject($skipDefaults = false)
|
|
|
|
{
|
|
|
|
$plain = parent::toPlainObject(
|
|
|
|
false,
|
|
|
|
$skipDefaults,
|
|
|
|
null,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
unset($plain->id);
|
|
|
|
|
|
|
|
return $plain;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toPlainObject(
|
|
|
|
$resolved = false,
|
|
|
|
$skipDefaults = false,
|
|
|
|
array $chosenProperties = null,
|
2019-02-15 01:36:33 +01:00
|
|
|
$resolveIds = true,
|
|
|
|
$keepId = false
|
2017-09-02 23:29:06 +02:00
|
|
|
) {
|
|
|
|
if ($resolved) {
|
2018-06-19 13:44:21 +02:00
|
|
|
throw new RuntimeException(
|
2017-09-02 23:29:06 +02:00
|
|
|
'A single CommandArgument cannot be resolved'
|
|
|
|
);
|
2016-03-21 10:12:49 +01:00
|
|
|
}
|
|
|
|
|
2017-09-02 23:29:06 +02:00
|
|
|
if ($chosenProperties) {
|
2018-06-19 13:44:21 +02:00
|
|
|
throw new RuntimeException(
|
2017-09-02 23:29:06 +02:00
|
|
|
'IcingaCommandArgument does not support chosenProperties[]'
|
|
|
|
);
|
2016-03-21 10:12:49 +01:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:36:33 +01:00
|
|
|
if ($keepId) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
'IcingaCommandArgument does not support $keepId'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-02 23:29:06 +02:00
|
|
|
// $resolveIds is misused here
|
2016-03-28 23:50:15 +02:00
|
|
|
if ($resolveIds) {
|
2017-09-04 12:45:21 +02:00
|
|
|
return $this->toCompatPlainObject();
|
2016-03-21 10:12:49 +01:00
|
|
|
} else {
|
2017-09-02 23:29:06 +02:00
|
|
|
return $this->toFullPlainObject($skipDefaults);
|
2016-03-21 10:12:49 +01:00
|
|
|
}
|
2016-03-18 13:46:06 +01:00
|
|
|
}
|
|
|
|
|
2015-10-16 18:42:38 +02:00
|
|
|
public function toConfigString()
|
|
|
|
{
|
|
|
|
$data = array();
|
2018-06-19 13:44:21 +02:00
|
|
|
$value = $this->get('argument_value');
|
|
|
|
if ($value) {
|
|
|
|
switch ($this->get('argument_format')) {
|
2015-10-16 18:42:38 +02:00
|
|
|
case 'string':
|
2018-06-19 13:44:21 +02:00
|
|
|
$data['value'] = c::renderString($value);
|
2015-10-16 18:42:38 +02:00
|
|
|
break;
|
2015-12-02 02:51:17 +01:00
|
|
|
case 'json':
|
2018-06-19 13:44:21 +02:00
|
|
|
if (is_object($value)) {
|
|
|
|
$data['value'] = c::renderDictionary($value);
|
|
|
|
} elseif (is_array($value)) {
|
|
|
|
$data['value'] = c::renderArray($value);
|
|
|
|
} elseif (is_null($value)) {
|
2015-12-18 16:11:36 +01:00
|
|
|
// TODO: recheck all this. I bet we never reach this:
|
|
|
|
$data['value'] = 'null';
|
2018-06-19 13:44:21 +02:00
|
|
|
} elseif (is_bool($value)) {
|
|
|
|
$data['value'] = c::renderBoolean($value);
|
2015-12-02 02:51:17 +01:00
|
|
|
} else {
|
2018-06-19 13:44:21 +02:00
|
|
|
$data['value'] = $value;
|
2015-12-02 02:51:17 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'expression':
|
2018-06-19 13:44:21 +02:00
|
|
|
$data['value'] = c::renderExpression($value);
|
2015-12-02 02:51:17 +01:00
|
|
|
break;
|
2015-10-16 18:42:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 13:44:21 +02:00
|
|
|
if ($this->get('sort_order') !== null) {
|
|
|
|
$data['order'] = $this->get('sort_order');
|
2015-10-16 18:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 13:44:21 +02:00
|
|
|
if (null !== $this->get('set_if')) {
|
|
|
|
switch ($this->get('set_if_format')) {
|
2016-09-23 17:18:58 +02:00
|
|
|
case 'expression':
|
2018-06-19 13:44:21 +02:00
|
|
|
$data['set_if'] = c::renderExpression($this->get('set_if'));
|
2016-09-23 17:18:58 +02:00
|
|
|
break;
|
2016-10-11 11:45:11 +02:00
|
|
|
case 'string':
|
|
|
|
default:
|
2018-06-19 13:44:21 +02:00
|
|
|
$data['set_if'] = c::renderString($this->get('set_if'));
|
2016-10-11 11:45:11 +02:00
|
|
|
break;
|
2016-09-23 17:18:58 +02:00
|
|
|
}
|
2015-12-02 02:51:17 +01:00
|
|
|
}
|
|
|
|
|
2018-06-19 13:44:21 +02:00
|
|
|
if (null !== $this->get('required')) {
|
|
|
|
$data['required'] = c::renderBoolean($this->get('required'));
|
2016-03-21 10:12:49 +01:00
|
|
|
}
|
|
|
|
|
2016-08-26 09:00:45 +02:00
|
|
|
if ($this->isSkippingKey()) {
|
|
|
|
$data['skip_key'] = c::renderBoolean('y');
|
2016-03-21 10:12:49 +01:00
|
|
|
}
|
|
|
|
|
2018-06-19 13:44:21 +02:00
|
|
|
if (null !== $this->get('repeat_key')) {
|
|
|
|
$data['repeat_key'] = c::renderBoolean($this->get('repeat_key'));
|
2015-12-02 02:51:17 +01:00
|
|
|
}
|
|
|
|
|
2018-06-19 13:44:21 +02:00
|
|
|
if (null !== $this->get('description')) {
|
|
|
|
$data['description'] = c::renderString($this->get('description'));
|
2015-12-02 02:51:17 +01:00
|
|
|
}
|
|
|
|
|
2018-06-19 13:44:21 +02:00
|
|
|
if (array_keys($data) === ['value']) {
|
2015-12-18 16:02:48 +01:00
|
|
|
return $data['value'];
|
|
|
|
} else {
|
|
|
|
return c::renderDictionary($data);
|
|
|
|
}
|
2015-10-16 18:42:38 +02:00
|
|
|
}
|
2015-06-01 16:28:40 +02:00
|
|
|
}
|