mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-09-26 03:09:11 +02:00
IcingaObject: move render helpers to helper class
Introduced IcingaConfigHelper, as not only IcingaObject classes will need those helper methods. Calls are static right now.
This commit is contained in:
parent
03ad5f7376
commit
9e52629d6e
35
library/Director/IcingaConfigHelper.php
Normal file
35
library/Director/IcingaConfigHelper.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Module\Director;
|
||||||
|
|
||||||
|
class IcingaConfigHelper
|
||||||
|
{
|
||||||
|
public static function renderKeyValue($key, $value, $prefix = '')
|
||||||
|
{
|
||||||
|
return sprintf(
|
||||||
|
"%s %s = %s\n",
|
||||||
|
$prefix,
|
||||||
|
$key,
|
||||||
|
$value
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function renderBoolean($value)
|
||||||
|
{
|
||||||
|
if ($value === 'y') {
|
||||||
|
return 'true';
|
||||||
|
} elseif ($value === 'n') {
|
||||||
|
return 'false';
|
||||||
|
} else {
|
||||||
|
throw new ProgrammingError('%s is not a valid boolean', $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function renderString($string)
|
||||||
|
{
|
||||||
|
$string = preg_replace('~\\\~', '\\\\', $string);
|
||||||
|
$string = preg_replace('~"~', '\\"', $string);
|
||||||
|
|
||||||
|
return sprintf('"%s"', $string);
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
namespace Icinga\Module\Director\Objects;
|
namespace Icinga\Module\Director\Objects;
|
||||||
|
|
||||||
use Icinga\Module\Director\Data\Db\DbObject;
|
use Icinga\Module\Director\Data\Db\DbObject;
|
||||||
|
use Icinga\Module\Director\IcingaConfigHelper as c;
|
||||||
use Icinga\Exception\ProgrammingError;
|
use Icinga\Exception\ProgrammingError;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
@ -72,37 +73,16 @@ abstract class IcingaObject extends DbObject
|
|||||||
if (method_exists($this, $method)) {
|
if (method_exists($this, $method)) {
|
||||||
$out .= $this->$method($value);
|
$out .= $this->$method($value);
|
||||||
} else {
|
} else {
|
||||||
$out .= $this->renderKeyValue($key, $this->renderString($value));
|
$out .= c::renderKeyValue($key, c::renderString($value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderKeyValue($key, $value, $prefix = '')
|
|
||||||
{
|
|
||||||
return sprintf(
|
|
||||||
"%s %s = %s\n",
|
|
||||||
$prefix,
|
|
||||||
$key,
|
|
||||||
$value
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function renderBoolean($value)
|
|
||||||
{
|
|
||||||
if ($value === 'y') {
|
|
||||||
return 'true';
|
|
||||||
} elseif ($value === 'n') {
|
|
||||||
return 'false';
|
|
||||||
} else {
|
|
||||||
throw new ProgrammingError('%s is not a valid boolean', $value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function renderBooleanProperty($key)
|
protected function renderBooleanProperty($key)
|
||||||
{
|
{
|
||||||
return $this->renderKeyValue($key, $this->renderBoolean($this->$key));
|
return c::renderKeyValue($key, c::renderBoolean($this->$key));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderSuffix()
|
protected function renderSuffix()
|
||||||
@ -127,17 +107,17 @@ abstract class IcingaObject extends DbObject
|
|||||||
|
|
||||||
protected function renderCommandProperty($commandId, $propertyName = 'check_command')
|
protected function renderCommandProperty($commandId, $propertyName = 'check_command')
|
||||||
{
|
{
|
||||||
return $this->renderKeyValue(
|
return c::renderKeyValue(
|
||||||
$propertyName,
|
$propertyName,
|
||||||
$this->renderString($this->connection->getCommandName($commandId))
|
c::renderString($this->connection->getCommandName($commandId))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderZoneProperty($zoneId, $propertyName = 'zone')
|
protected function renderZoneProperty($zoneId, $propertyName = 'zone')
|
||||||
{
|
{
|
||||||
return $this->renderKeyValue(
|
return c::renderKeyValue(
|
||||||
$propertyName,
|
$propertyName,
|
||||||
$this->renderString($this->connection->getZoneName($zoneId))
|
c::renderString($this->connection->getZoneName($zoneId))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +132,7 @@ abstract class IcingaObject extends DbObject
|
|||||||
"%s %s %s {\n",
|
"%s %s %s {\n",
|
||||||
$this->getObjectTypeName(),
|
$this->getObjectTypeName(),
|
||||||
$this->getType(),
|
$this->getType(),
|
||||||
$this->renderString($this->getObjectName())
|
c::renderString($this->getObjectName())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,14 +147,6 @@ abstract class IcingaObject extends DbObject
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderString($string)
|
|
||||||
{
|
|
||||||
$string = preg_replace('~\\\~', '\\\\', $string);
|
|
||||||
$string = preg_replace('~"~', '\\"', $string);
|
|
||||||
|
|
||||||
return sprintf('"%s"', $string);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getType()
|
protected function getType()
|
||||||
{
|
{
|
||||||
if ($this->type === null) {
|
if ($this->type === null) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user