2015-06-11 07:44:22 +02:00
|
|
|
<?php
|
|
|
|
|
2015-06-11 22:44:17 +02:00
|
|
|
namespace Icinga\Module\Director\IcingaConfig;
|
2015-06-11 07:44:22 +02:00
|
|
|
|
|
|
|
class IcingaConfigHelper
|
|
|
|
{
|
2015-06-11 07:52:26 +02:00
|
|
|
/**
|
|
|
|
* Reserved words according to
|
|
|
|
* http://docs.icinga.org/icinga2/snapshot/doc/module/icinga2/chapter/language-reference#reserved-keywords
|
|
|
|
*/
|
|
|
|
protected static $reservedWords = array(
|
|
|
|
'object',
|
|
|
|
'template',
|
|
|
|
'include',
|
|
|
|
'include_recursive',
|
|
|
|
'library',
|
|
|
|
'null',
|
|
|
|
'true',
|
|
|
|
'false',
|
|
|
|
'const',
|
|
|
|
'var',
|
|
|
|
'this',
|
|
|
|
'use',
|
|
|
|
'apply',
|
|
|
|
'to',
|
|
|
|
'where',
|
|
|
|
'import',
|
|
|
|
'assign',
|
|
|
|
'ignore',
|
|
|
|
'function',
|
|
|
|
'return',
|
|
|
|
'for',
|
|
|
|
'if',
|
|
|
|
'else',
|
|
|
|
'in',
|
|
|
|
);
|
|
|
|
|
2015-06-11 23:02:43 +02:00
|
|
|
public static function renderKeyValue($key, $value, $prefix = ' ')
|
2015-06-11 07:44:22 +02:00
|
|
|
{
|
2015-06-11 23:02:43 +02:00
|
|
|
$string = sprintf(
|
|
|
|
"%s = %s",
|
2015-06-11 07:44:22 +02:00
|
|
|
$key,
|
|
|
|
$value
|
|
|
|
);
|
2015-06-11 23:02:43 +02:00
|
|
|
|
|
|
|
if ($prefix && strpos($string, "\n") !== false) {
|
|
|
|
return $prefix . implode("\n" . $prefix, explode("\n", $string)) . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $prefix . $string . "\n";
|
2015-06-11 07:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 08:12:54 +02:00
|
|
|
// TODO: Find out how to allow multiline {{{...}}} strings.
|
|
|
|
// Parameter? Dedicated method? Always if \n is found?
|
2015-06-11 07:44:22 +02:00
|
|
|
public static function renderString($string)
|
|
|
|
{
|
2015-06-11 08:12:54 +02:00
|
|
|
$special = array(
|
|
|
|
'/\\\/',
|
|
|
|
'/"/',
|
|
|
|
'/\$/',
|
|
|
|
'/\t/',
|
|
|
|
'/\r/',
|
|
|
|
'/\n/',
|
|
|
|
// '/\b/', -> doesn't work
|
|
|
|
'/\f/'
|
|
|
|
);
|
|
|
|
|
|
|
|
$replace = array(
|
2015-06-11 21:32:46 +02:00
|
|
|
'\\\\\\',
|
2015-06-11 08:12:54 +02:00
|
|
|
'\\"',
|
|
|
|
'\\$',
|
|
|
|
'\\t',
|
|
|
|
'\\r',
|
|
|
|
'\\n',
|
|
|
|
// '\\b',
|
|
|
|
'\\f',
|
|
|
|
);
|
|
|
|
|
|
|
|
$string = preg_replace($special, $replace, $string);
|
2015-06-11 07:44:22 +02:00
|
|
|
|
2015-06-11 08:12:54 +02:00
|
|
|
return '"' . $string . '"';
|
2015-06-11 07:44:22 +02:00
|
|
|
}
|
2015-06-11 07:52:26 +02:00
|
|
|
|
2015-06-16 17:54:38 +02:00
|
|
|
// Requires an array
|
2015-06-15 14:32:35 +02:00
|
|
|
public static function renderArray($array)
|
|
|
|
{
|
2015-06-16 17:54:38 +02:00
|
|
|
$data = array();
|
|
|
|
foreach ($array as $entry) {
|
|
|
|
if ($entry instanceof IcingaConfigRenderer) {
|
|
|
|
$data[] = $entry;
|
|
|
|
} else {
|
|
|
|
$data[] = self::renderString($entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$str = '[ ' . implode(', ', $data) . ' ]';
|
2015-06-15 14:32:35 +02:00
|
|
|
|
|
|
|
if (strlen($str) < 60) {
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefix for toConfigString?
|
2015-06-16 17:54:38 +02:00
|
|
|
return "[\n " . implode(",\n ", $data) . "\n]";
|
2015-06-15 14:32:35 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:51:06 +02:00
|
|
|
public static function renderDictionary($dictionary)
|
|
|
|
{
|
|
|
|
$vals = array();
|
|
|
|
foreach ($dictionary as $key => $value) {
|
|
|
|
$vals[] = rtrim(self::renderKeyValue(self::renderString($key), $value));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefix for toConfigString?
|
|
|
|
return "{\n" . implode("\n", $vals) . "\n}";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-11 07:52:26 +02:00
|
|
|
public static function isReserved($string)
|
|
|
|
{
|
|
|
|
return in_array($string, self::$reservedWords);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function escapeIfReserved($string)
|
|
|
|
{
|
|
|
|
if (self::isReserved($string)) {
|
|
|
|
return '@' . $string;
|
|
|
|
} else {
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 07:44:22 +02:00
|
|
|
}
|