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:
Thomas Gelf 2015-06-11 07:44:22 +02:00
parent 03ad5f7376
commit 9e52629d6e
2 changed files with 43 additions and 36 deletions

View 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);
}
}

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\IcingaConfigHelper as c;
use Icinga\Exception\ProgrammingError;
use Exception;
@ -72,37 +73,16 @@ abstract class IcingaObject extends DbObject
if (method_exists($this, $method)) {
$out .= $this->$method($value);
} else {
$out .= $this->renderKeyValue($key, $this->renderString($value));
$out .= c::renderKeyValue($key, c::renderString($value));
}
}
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)
{
return $this->renderKeyValue($key, $this->renderBoolean($this->$key));
return c::renderKeyValue($key, c::renderBoolean($this->$key));
}
protected function renderSuffix()
@ -127,17 +107,17 @@ abstract class IcingaObject extends DbObject
protected function renderCommandProperty($commandId, $propertyName = 'check_command')
{
return $this->renderKeyValue(
return c::renderKeyValue(
$propertyName,
$this->renderString($this->connection->getCommandName($commandId))
c::renderString($this->connection->getCommandName($commandId))
);
}
protected function renderZoneProperty($zoneId, $propertyName = 'zone')
{
return $this->renderKeyValue(
return c::renderKeyValue(
$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",
$this->getObjectTypeName(),
$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()
{
if ($this->type === null) {