2015-06-11 08:15:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\CustomVariable;
|
|
|
|
|
|
|
|
class CustomVariables
|
|
|
|
{
|
|
|
|
protected $storedVars = array();
|
|
|
|
|
|
|
|
protected $vars = array();
|
|
|
|
|
|
|
|
protected $modified = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic setter
|
|
|
|
*
|
|
|
|
* @param string $property
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function set($key, $value)
|
|
|
|
{
|
|
|
|
$key = (string) $key;
|
|
|
|
|
2015-06-11 19:46:36 +02:00
|
|
|
if (! $value instanceof CustomVariable) {
|
|
|
|
$value = CustomVariable::create($key, $value);
|
2015-06-11 08:15:51 +02:00
|
|
|
}
|
|
|
|
|
2015-06-11 19:46:36 +02:00
|
|
|
if (isset($this->$key) && $value->equals($this->get($key))) {
|
2015-06-11 08:15:51 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->vars[$key] = $value;
|
|
|
|
$this->modified = true;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-11 19:46:36 +02:00
|
|
|
public function get($key)
|
|
|
|
{
|
|
|
|
if (array_key_exists($key, $this->vars)) {
|
|
|
|
return $this->vars[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-06-11 08:15:51 +02:00
|
|
|
public function hasBeenModified()
|
|
|
|
{
|
2015-06-11 19:12:20 +02:00
|
|
|
return $this->modified;
|
2015-06-11 08:15:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setUnmodified()
|
|
|
|
{
|
|
|
|
$this->modified = false;
|
|
|
|
$this->storedVars = $this->vars;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-11 19:23:18 +02:00
|
|
|
public function toConfigString()
|
|
|
|
{
|
|
|
|
$out = '';
|
|
|
|
|
|
|
|
foreach ($this->vars as $key => $var) {
|
2015-06-11 19:46:36 +02:00
|
|
|
$out .= $var->toConfigString();
|
2015-06-11 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2015-06-11 08:15:51 +02:00
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
return $this->get($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic setter
|
|
|
|
*
|
|
|
|
* @param string $key Key
|
|
|
|
* @param mixed $val Value
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __set($key, $val)
|
|
|
|
{
|
|
|
|
$this->set($key, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic isset check
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function __isset($key)
|
|
|
|
{
|
2015-06-11 19:46:36 +02:00
|
|
|
return array_key_exists($key, $this->vars);
|
2015-06-11 08:15:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic unsetter
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __unset($key)
|
|
|
|
{
|
|
|
|
if (! array_key_exists($key, $this->properties)) {
|
|
|
|
throw new Exception('Trying to unset invalid key');
|
|
|
|
}
|
|
|
|
$this->properties[$key] = $this->defaultProperties[$key];
|
|
|
|
}
|
|
|
|
|
2015-06-11 19:23:18 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 08:15:51 +02:00
|
|
|
}
|