2015-06-11 08:15:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\CustomVariable;
|
|
|
|
|
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
|
|
|
|
abstract class CustomVariable
|
|
|
|
{
|
|
|
|
protected $key;
|
|
|
|
|
|
|
|
protected $value;
|
|
|
|
|
|
|
|
protected $storedValue;
|
|
|
|
|
|
|
|
protected $type;
|
|
|
|
|
|
|
|
protected $modified = false;
|
|
|
|
|
|
|
|
protected function __construct($key, $value = null)
|
|
|
|
{
|
|
|
|
$this->key = $key;
|
|
|
|
$this->setValue($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function is($type)
|
|
|
|
{
|
|
|
|
return $this->getType() === $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getType()
|
|
|
|
{
|
|
|
|
if ($this->type === null) {
|
|
|
|
$parts = explode('\\', get_class($this));
|
|
|
|
$class = end($parts);
|
|
|
|
// strlen('CustomVariable') === 9
|
|
|
|
$this->type = substr(end($parts), 9);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2015-06-11 19:46:36 +02:00
|
|
|
public function getKey()
|
|
|
|
{
|
|
|
|
return $this->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValue()
|
|
|
|
{
|
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
|
2015-06-11 08:15:51 +02:00
|
|
|
public function setValue($value)
|
|
|
|
{
|
|
|
|
if ($value instanceof CustomVariable) {
|
|
|
|
if (! $this->equals($value)) {
|
2015-06-11 19:46:36 +02:00
|
|
|
$this->reallySetValue($value);
|
2015-06-11 08:15:51 +02:00
|
|
|
}
|
|
|
|
} elseif ($value !== $this->value) {
|
2015-06-11 19:46:36 +02:00
|
|
|
$this->reallySetValue($value);
|
2015-06-11 08:15:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function reallySetValue($value)
|
|
|
|
{
|
|
|
|
$this->modified = true;
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasBeenModified()
|
|
|
|
{
|
|
|
|
return $this->modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUnmodified()
|
|
|
|
{
|
|
|
|
$this->modified = false;
|
|
|
|
$this->storedValue = clone($this->value);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract public function equals(CustomVariable $var);
|
|
|
|
|
|
|
|
abstract public function toConfigString();
|
|
|
|
|
|
|
|
public function differsFrom(CustomVariable $var)
|
|
|
|
{
|
|
|
|
return ! $this->equals($var);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function create($key, $value)
|
|
|
|
{
|
|
|
|
if (is_string($value)) {
|
|
|
|
|
|
|
|
return new CustomVariableString($key, $value);
|
|
|
|
|
|
|
|
} elseif (is_array($value)) {
|
|
|
|
|
2015-06-11 19:46:36 +02:00
|
|
|
foreach (array_keys($value) as $key) {
|
2015-06-11 08:15:51 +02:00
|
|
|
if (! is_int($key) || ctype_digit($key)) {
|
|
|
|
return new CustomVariableDictionary($key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new CustomVariableArray($key, array_values($value));
|
|
|
|
|
|
|
|
} elseif (is_object($value)) {
|
|
|
|
// TODO: check for specific class/stdClass/interface?
|
|
|
|
return new CustomVariableDictionary($key, $value);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
throw new ProgrammingError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->toConfigString();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
trigger_error($e);
|
|
|
|
$previousHandler = set_exception_handler(function () {});
|
|
|
|
restore_error_handler();
|
|
|
|
call_user_func($previousHandler, $e);
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|