2015-06-26 10:39:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Objects;
|
|
|
|
|
2016-10-14 12:33:46 +02:00
|
|
|
use Countable;
|
|
|
|
use Exception;
|
2015-06-26 10:39:30 +02:00
|
|
|
use Iterator;
|
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
|
2016-10-14 12:33:46 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
|
2016-07-28 08:57:27 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
|
2015-06-26 10:39:30 +02:00
|
|
|
|
|
|
|
class IcingaObjectImports implements Iterator, Countable, IcingaConfigRenderer
|
|
|
|
{
|
2016-10-14 12:33:46 +02:00
|
|
|
protected $storedNames = array();
|
2015-06-26 10:39:30 +02:00
|
|
|
|
2016-11-01 18:28:36 +01:00
|
|
|
/** @var array A list of our imports, key and value are the import name */
|
2015-06-26 10:39:30 +02:00
|
|
|
protected $imports = array();
|
|
|
|
|
2016-10-14 12:33:46 +02:00
|
|
|
/** @var IcingaObject[] A list of all objects we have seen, referred by name */
|
2016-03-07 02:05:10 +01:00
|
|
|
protected $objects = array();
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
protected $modified = false;
|
|
|
|
|
2016-10-14 12:33:46 +02:00
|
|
|
/** @var IcingaObject The parent object */
|
2015-06-26 10:39:30 +02:00
|
|
|
protected $object;
|
|
|
|
|
|
|
|
private $position = 0;
|
|
|
|
|
|
|
|
protected $idx = array();
|
|
|
|
|
|
|
|
public function __construct(IcingaObject $object)
|
|
|
|
{
|
|
|
|
$this->object = $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return count($this->imports);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rewind()
|
|
|
|
{
|
|
|
|
$this->position = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-12 17:22:11 +02:00
|
|
|
public function setModified()
|
|
|
|
{
|
|
|
|
$this->modified = true;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
public function hasBeenModified()
|
|
|
|
{
|
|
|
|
return $this->modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function current()
|
|
|
|
{
|
|
|
|
if (! $this->valid()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-07 02:05:10 +01:00
|
|
|
return $this->getObject(
|
|
|
|
$this->imports[$this->idx[$this->position]]
|
|
|
|
);
|
2015-06-26 10:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function key()
|
|
|
|
{
|
|
|
|
return $this->idx[$this->position];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function next()
|
|
|
|
{
|
|
|
|
++$this->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function valid()
|
|
|
|
{
|
|
|
|
return array_key_exists($this->position, $this->idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get($key)
|
|
|
|
{
|
|
|
|
if (array_key_exists($key, $this->imports)) {
|
2016-03-07 02:05:10 +01:00
|
|
|
return $this->getObject($this->imports[$key]);
|
2015-06-26 10:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set($import)
|
|
|
|
{
|
2016-10-12 17:22:11 +02:00
|
|
|
if (empty($import)) {
|
2016-05-20 08:12:46 +02:00
|
|
|
if (empty($this->imports)) {
|
|
|
|
return $this;
|
|
|
|
} else {
|
|
|
|
return $this->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-05 10:49:37 +01:00
|
|
|
if (! is_array($import)) {
|
|
|
|
$import = array($import);
|
|
|
|
}
|
2016-05-20 08:12:46 +02:00
|
|
|
|
2016-10-12 17:22:11 +02:00
|
|
|
$existing = $this->listImportNames();
|
|
|
|
$new = $this->listNamesForGivenImports($import);
|
2015-06-26 15:54:00 +02:00
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
if ($existing === $new) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->imports = array();
|
|
|
|
return $this->add($import);
|
|
|
|
}
|
|
|
|
|
2016-10-12 17:22:11 +02:00
|
|
|
protected function listNamesForGivenImports($imports)
|
|
|
|
{
|
|
|
|
$list = array();
|
|
|
|
$class = $this->getImportClass();
|
|
|
|
foreach ($imports as $i) {
|
|
|
|
|
|
|
|
if ($i instanceof $class) {
|
|
|
|
$list[] = $i->object_name;
|
|
|
|
} else {
|
|
|
|
$list[] = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
/**
|
|
|
|
* Magic isset check
|
|
|
|
*
|
2016-11-01 18:28:36 +01:00
|
|
|
* @param string $import
|
|
|
|
*
|
2015-06-26 10:39:30 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function __isset($import)
|
|
|
|
{
|
|
|
|
return array_key_exists($import, $this->imports);
|
|
|
|
}
|
|
|
|
|
2015-06-29 12:50:59 +02:00
|
|
|
public function clear()
|
|
|
|
{
|
2016-03-05 10:49:37 +01:00
|
|
|
if ($this->imports === array()) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-29 12:50:59 +02:00
|
|
|
$this->imports = array();
|
|
|
|
$this->modified = true;
|
|
|
|
|
2016-10-12 17:22:11 +02:00
|
|
|
return $this->refreshIndex();
|
2015-06-29 12:50:59 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
public function remove($import)
|
|
|
|
{
|
|
|
|
if (array_key_exists($import, $this->imports)) {
|
|
|
|
unset($this->imports[$import]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->modified = true;
|
2015-06-29 12:50:59 +02:00
|
|
|
|
2016-10-12 17:22:11 +02:00
|
|
|
return $this->refreshIndex();
|
2015-06-26 10:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function refreshIndex()
|
|
|
|
{
|
|
|
|
$this->idx = array_keys($this->imports);
|
2016-10-26 16:15:57 +02:00
|
|
|
$this->object->templateResolver()->refreshObject($this->object);
|
2016-10-12 17:22:11 +02:00
|
|
|
return $this;
|
2015-06-26 10:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function add($import)
|
|
|
|
{
|
2016-03-05 12:56:29 +01:00
|
|
|
$class = $this->getImportClass();
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
if (is_array($import)) {
|
|
|
|
foreach ($import as $i) {
|
2016-03-05 12:56:29 +01:00
|
|
|
// Gracefully ignore null members or empty strings
|
|
|
|
if (! $i instanceof $class && strlen($i) === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
$this->add($i);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($import instanceof $class) {
|
2016-10-12 17:22:11 +02:00
|
|
|
$name = $import->object_name;
|
|
|
|
if (array_key_exists($name, $this->imports)) {
|
2016-06-16 21:20:11 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-10-12 17:22:11 +02:00
|
|
|
$this->imports[$name] = $name;
|
|
|
|
$this->objects[$name] = $import;
|
2015-06-26 10:39:30 +02:00
|
|
|
} elseif (is_string($import)) {
|
2016-06-16 21:20:11 +02:00
|
|
|
if (array_key_exists($import, $this->imports)) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-07 02:05:10 +01:00
|
|
|
$this->imports[$import] = $import;
|
2015-06-26 10:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->modified = true;
|
|
|
|
$this->refreshIndex();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-11-01 18:28:36 +01:00
|
|
|
/**
|
|
|
|
* @return IcingaObject[]
|
|
|
|
*/
|
2016-06-16 21:40:22 +02:00
|
|
|
public function getObjects()
|
|
|
|
{
|
|
|
|
$list = array();
|
|
|
|
foreach ($this->listImportNames() as $name) {
|
|
|
|
$list[$name] = $this->getObject($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2016-03-07 02:05:10 +01:00
|
|
|
protected function getObject($name)
|
|
|
|
{
|
|
|
|
if (array_key_exists($name, $this->objects)) {
|
|
|
|
return $this->objects[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
$connection = $this->object->getConnection();
|
2016-11-01 18:28:36 +01:00
|
|
|
/** @var IcingaObject $class */
|
2016-03-07 02:05:10 +01:00
|
|
|
$class = $this->getImportClass();
|
|
|
|
if (is_array($this->object->getKeyName())) {
|
|
|
|
// Services only
|
2016-10-12 17:22:11 +02:00
|
|
|
$import = $class::load(
|
|
|
|
array(
|
|
|
|
'object_name' => $name,
|
|
|
|
'object_type' => 'template'
|
|
|
|
),
|
|
|
|
$connection
|
|
|
|
);
|
2016-03-07 02:05:10 +01:00
|
|
|
} else {
|
|
|
|
$import = $class::load($name, $connection);
|
|
|
|
}
|
|
|
|
|
2016-11-01 18:28:36 +01:00
|
|
|
return $this->objects[$import->getObjectName()] = $import;
|
2016-03-07 02:05:10 +01:00
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
protected function getImportTableName()
|
|
|
|
{
|
|
|
|
return $this->object->getTableName() . '_inheritance';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function listImportNames()
|
|
|
|
{
|
|
|
|
return array_keys($this->imports);
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:11:51 +02:00
|
|
|
public function listOriginalImportNames()
|
|
|
|
{
|
2016-10-14 12:33:46 +02:00
|
|
|
return $this->storedNames;
|
2015-08-28 17:11:51 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
public function getType()
|
|
|
|
{
|
|
|
|
return $this->object->getShortTableName();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function loadFromDb()
|
|
|
|
{
|
2016-10-12 17:22:11 +02:00
|
|
|
$resolver = $this->object->templateResolver();
|
|
|
|
$this->objects = $resolver->fetchParents();
|
2016-10-14 12:33:46 +02:00
|
|
|
if (empty($this->objects)) {
|
|
|
|
$this->imports = array();
|
|
|
|
} else {
|
|
|
|
$keys = array_keys($this->objects);
|
|
|
|
$this->imports = array_combine($keys, $keys);
|
2016-03-07 02:05:10 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 17:22:11 +02:00
|
|
|
$this->cloneStored();
|
2016-10-09 14:02:22 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
public function store()
|
|
|
|
{
|
2016-10-12 17:22:11 +02:00
|
|
|
if (! $this->hasBeenModified()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-01 18:28:36 +01:00
|
|
|
$objectId = (int) $this->object->get('id');
|
2015-06-26 10:39:30 +02:00
|
|
|
$type = $this->getType();
|
|
|
|
|
|
|
|
$objectCol = $type . '_id';
|
2015-06-26 15:54:00 +02:00
|
|
|
$importCol = 'parent_' . $type . '_id';
|
2016-10-14 12:33:46 +02:00
|
|
|
$table = $this->getImportTableName();
|
2016-11-01 18:28:36 +01:00
|
|
|
$db = $this->object->getDb();
|
2015-06-26 10:39:30 +02:00
|
|
|
|
2015-06-30 10:46:15 +02:00
|
|
|
if ($this->object->hasBeenLoadedFromDb()) {
|
2016-11-01 18:28:36 +01:00
|
|
|
$db->delete(
|
2016-10-14 12:33:46 +02:00
|
|
|
$table,
|
2016-10-12 17:22:11 +02:00
|
|
|
$objectCol . ' = ' . $objectId
|
|
|
|
);
|
2015-06-30 10:46:15 +02:00
|
|
|
}
|
2015-06-26 10:39:30 +02:00
|
|
|
|
2015-06-26 15:54:00 +02:00
|
|
|
$weight = 1;
|
2016-10-14 12:33:46 +02:00
|
|
|
foreach ($this->getObjects() as $import) {
|
2016-11-01 18:28:36 +01:00
|
|
|
$db->insert(
|
2016-10-14 12:33:46 +02:00
|
|
|
$table,
|
2015-06-26 10:39:30 +02:00
|
|
|
array(
|
|
|
|
$objectCol => $objectId,
|
2016-11-01 18:28:36 +01:00
|
|
|
$importCol => $import->get('id'),
|
2015-06-26 15:54:00 +02:00
|
|
|
'weight' => $weight++
|
2015-06-26 10:39:30 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2015-06-26 15:54:00 +02:00
|
|
|
|
2015-08-28 17:11:51 +02:00
|
|
|
$this->cloneStored();
|
2015-06-26 10:39:30 +02:00
|
|
|
|
2015-06-30 10:46:15 +02:00
|
|
|
return true;
|
2015-06-26 10:39:30 +02:00
|
|
|
}
|
|
|
|
|
2015-08-28 17:11:51 +02:00
|
|
|
protected function cloneStored()
|
|
|
|
{
|
2016-10-14 12:33:46 +02:00
|
|
|
$this->storedNames = $this->listImportNames();
|
2016-05-20 08:12:46 +02:00
|
|
|
$this->modified = false;
|
2015-08-28 17:11:51 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
protected function getImportClass()
|
|
|
|
{
|
|
|
|
return get_class($this->object);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function loadForStoredObject(IcingaObject $object)
|
|
|
|
{
|
2016-10-20 21:17:52 +02:00
|
|
|
$obj = new static($object);
|
|
|
|
return $obj->loadFromDb();
|
2015-06-26 10:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function toConfigString()
|
|
|
|
{
|
|
|
|
$ret = '';
|
|
|
|
|
2016-10-12 17:22:11 +02:00
|
|
|
foreach ($this->listImportNames() as $name) {
|
2016-03-07 02:05:10 +01:00
|
|
|
$ret .= ' import ' . c::renderString($name) . "\n";
|
2015-06-26 10:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($ret !== '') {
|
|
|
|
$ret .= "\n";
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2016-07-28 08:57:27 +02:00
|
|
|
public function toLegacyConfigString()
|
|
|
|
{
|
|
|
|
$ret = '';
|
|
|
|
|
2016-10-12 17:22:11 +02:00
|
|
|
foreach ($this->listImportNames() as $name) {
|
|
|
|
$ret .= c1::renderKeyValue('use', c1::renderString($name)) . "\n";
|
2016-07-28 08:57:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($ret !== '') {
|
|
|
|
$ret .= "\n";
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->toConfigString();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
trigger_error($e);
|
2016-02-26 11:58:37 +01:00
|
|
|
$previousHandler = set_exception_handler(
|
|
|
|
function () {
|
|
|
|
}
|
|
|
|
);
|
2015-06-26 10:39:30 +02:00
|
|
|
restore_error_handler();
|
|
|
|
if ($previousHandler !== null) {
|
|
|
|
call_user_func($previousHandler, $e);
|
|
|
|
die();
|
|
|
|
} else {
|
|
|
|
die($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-30 09:34:27 +02:00
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
unset($this->object);
|
2016-10-14 12:33:46 +02:00
|
|
|
unset($this->objects);
|
2016-06-30 09:34:27 +02:00
|
|
|
}
|
2015-06-26 10:39:30 +02:00
|
|
|
}
|