2015-06-11 08:15:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\CustomVariable;
|
|
|
|
|
2015-06-11 22:44:17 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
|
2015-06-16 17:58:47 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
|
2015-06-15 16:35:18 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaObject;
|
2015-06-15 17:16:09 +02:00
|
|
|
use Iterator;
|
|
|
|
use Countable;
|
2015-06-11 21:33:47 +02:00
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
|
2015-06-11 08:15:51 +02:00
|
|
|
{
|
|
|
|
protected $storedVars = array();
|
|
|
|
|
|
|
|
protected $vars = array();
|
|
|
|
|
|
|
|
protected $modified = false;
|
|
|
|
|
2015-06-15 17:16:09 +02:00
|
|
|
private $position = 0;
|
|
|
|
|
|
|
|
protected $idx = array();
|
|
|
|
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return count($this->vars);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rewind()
|
|
|
|
{
|
|
|
|
$this->position = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function current()
|
|
|
|
{
|
|
|
|
if (! $this->valid()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->vars[$this->idx[$this->position]];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function key()
|
|
|
|
{
|
|
|
|
return $this->idx[$this->position];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function next()
|
|
|
|
{
|
|
|
|
++$this->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function valid()
|
|
|
|
{
|
|
|
|
return array_key_exists($this->position, $this->idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-11 08:15:51 +02:00
|
|
|
/**
|
|
|
|
* 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-24 09:42:50 +02:00
|
|
|
if (isset($this->$key)) {
|
|
|
|
if ($value->equals($this->get($key))) {
|
|
|
|
return $this;
|
|
|
|
} else {
|
|
|
|
$this->vars[$key]->setValue($value->getValue());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->vars[$key] = $value;
|
2015-06-11 08:15:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->modified = true;
|
2015-06-15 17:16:09 +02:00
|
|
|
$this->refreshIndex();
|
2015-06-11 08:15:51 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-15 17:16:09 +02:00
|
|
|
protected function refreshIndex()
|
|
|
|
{
|
2015-07-31 14:35:49 +02:00
|
|
|
$this->idx = array();
|
|
|
|
foreach ($this->vars as $name => $var) {
|
|
|
|
if (! $var->hasBeenDeleted()) {
|
|
|
|
$this->idx[] = $name;
|
|
|
|
}
|
|
|
|
}
|
2015-06-15 17:16:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-15 16:35:18 +02:00
|
|
|
public static function loadForStoredObject(IcingaObject $object)
|
|
|
|
{
|
|
|
|
$db = $object->getDb();
|
|
|
|
|
|
|
|
$query = $db->select()->from(
|
|
|
|
array('v' => $object->getVarsTableName()),
|
|
|
|
array(
|
|
|
|
'v.varname',
|
|
|
|
'v.varvalue',
|
|
|
|
'v.format',
|
|
|
|
)
|
2015-07-24 10:57:05 +02:00
|
|
|
)->where(sprintf('v.%s = ?', $object->getVarsIdColumn()), $object->id);
|
2015-06-15 16:35:18 +02:00
|
|
|
|
|
|
|
$vars = new CustomVariables;
|
|
|
|
foreach ($db->fetchAll($query) as $row) {
|
|
|
|
$vars->vars[$row->varname] = CustomVariable::fromDbRow($row);
|
|
|
|
}
|
2015-06-15 17:16:09 +02:00
|
|
|
$vars->refreshIndex();
|
2015-06-15 16:35:18 +02:00
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2015-06-15 17:16:09 +02:00
|
|
|
public function storeToDb(IcingaObject $object)
|
|
|
|
{
|
|
|
|
$db = $object->getDb();
|
|
|
|
$table = $object->getVarsTableName();
|
|
|
|
$foreignColumn = $object->getVarsIdColumn();
|
2015-07-24 10:57:05 +02:00
|
|
|
$foreignId = $object->id;
|
2015-06-15 17:16:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
foreach ($this->vars as $var) {
|
|
|
|
if ($var->isNew()) {
|
|
|
|
$db->insert(
|
|
|
|
$table,
|
|
|
|
array(
|
|
|
|
$foreignColumn => $foreignId,
|
|
|
|
'varname' => $var->getKey(),
|
|
|
|
'varvalue' => $var->getDbValue(),
|
|
|
|
'format' => $var->getDbFormat()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$where = $db->quoteInto(sprintf('%s = ?', $foreignColumn), $foreignId)
|
2015-06-24 09:43:48 +02:00
|
|
|
. $db->quoteInto(' AND varname = ?', $var->getKey());
|
2015-06-15 17:16:09 +02:00
|
|
|
|
|
|
|
if ($var->hasBeenDeleted()) {
|
|
|
|
$db->delete($table, $where);
|
|
|
|
} elseif ($var->hasBeenModified()) {
|
|
|
|
$db->update(
|
|
|
|
$table,
|
2015-06-24 09:43:48 +02:00
|
|
|
array('varvalue' => $var->getDbValue()),
|
2015-06-15 17:16:09 +02:00
|
|
|
$where
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-07-24 10:57:05 +02:00
|
|
|
|
|
|
|
$this->setUnmodified();
|
2015-06-15 17:16:09 +02:00
|
|
|
}
|
|
|
|
|
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-16 17:58:47 +02:00
|
|
|
}
|
2015-06-11 08:15:51 +02:00
|
|
|
|
2015-06-11 19:23:18 +02:00
|
|
|
public function toConfigString()
|
|
|
|
{
|
|
|
|
$out = '';
|
|
|
|
|
|
|
|
foreach ($this->vars as $key => $var) {
|
2015-06-11 21:33:47 +02:00
|
|
|
$out .= c::renderKeyValue(
|
2015-06-11 23:02:43 +02:00
|
|
|
'vars.' . c::escapeIfReserved($key),
|
|
|
|
$var->toConfigString()
|
2015-06-11 21:33:47 +02:00
|
|
|
);
|
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)
|
|
|
|
{
|
2015-06-15 17:16:09 +02:00
|
|
|
if (! array_key_exists($key, $this->vars)) {
|
2015-07-31 14:35:49 +02:00
|
|
|
return;
|
2015-06-11 08:15:51 +02:00
|
|
|
}
|
2015-06-15 17:16:09 +02:00
|
|
|
|
2015-07-31 14:35:49 +02:00
|
|
|
$this->vars[$key]->delete();
|
|
|
|
$this->modified = true;
|
2015-06-15 17:16:09 +02:00
|
|
|
|
|
|
|
$this->refreshIndex();
|
2015-06-11 08:15:51 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|