diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
commit | f66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch) | |
tree | fbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/IcingaConfig/IcingaLegacyConfigHelper.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip |
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/IcingaConfig/IcingaLegacyConfigHelper.php')
-rw-r--r-- | library/Director/IcingaConfig/IcingaLegacyConfigHelper.php | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/library/Director/IcingaConfig/IcingaLegacyConfigHelper.php b/library/Director/IcingaConfig/IcingaLegacyConfigHelper.php new file mode 100644 index 0000000..38d93ee --- /dev/null +++ b/library/Director/IcingaConfig/IcingaLegacyConfigHelper.php @@ -0,0 +1,110 @@ +<?php + +namespace Icinga\Module\Director\IcingaConfig; + +use InvalidArgumentException; + +class IcingaLegacyConfigHelper +{ + public static function renderKeyValue($key, $value, $prefix = ' ') + { + return self::renderKeyOperatorValue($key, "\t", $value, $prefix); + } + + public static function renderKeyOperatorValue($key, $operator, $value, $prefix = ' ') + { + $string = sprintf( + "%s%s%s", + $key, + $operator, + $value + ); + + if ($prefix && strpos($string, "\n") !== false) { + return $prefix . implode("\n" . $prefix, explode("\n", $string)) . "\n"; + } + + return $prefix . $string . "\n"; + } + + public static function renderBoolean($value) + { + if ($value === 'y') { + return '1'; + } elseif ($value === 'n') { + return '0'; + } else { + throw new InvalidArgumentException('%s is not a valid boolean', $value); + } + } + + // TODO: Double-check legacy "encoding" + public static function renderString($string) + { + $special = [ + '/\\\/', + '/\$/', + '/\t/', + '/\r/', + '/\n/', + // '/\b/', -> doesn't work + '/\f/', + ]; + + $replace = [ + '\\\\\\', + '\\$', + '\\t', + '\\r', + '\\n', + // '\\b', + '\\f', + ]; + + $string = preg_replace($special, $replace, $string); + + return $string; + } + + /** + * @param array $array + * @return string + */ + public static function renderArray($array) + { + $data = []; + foreach ($array as $entry) { + if ($entry instanceof IcingaConfigRenderer) { + // $data[] = $entry; + $data[] = 'INVALID_ARRAY_MEMBER'; + } else { + $data[] = self::renderString($entry); + } + } + + return implode(', ', $data); + } + + public static function renderDictionary($dictionary) + { + return 'INVALID_DICTIONARY'; + } + + public static function renderExpression($string) + { + return 'INVALID_EXPRESSION'; + } + + public static function alreadyRendered($string) + { + return new IcingaConfigRendered($string); + } + + public static function renderInterval($interval) + { + if ($interval < 60) { + $interval = 60; + } + return $interval / 60; + } +} |