From fe805c82ca1093b4cc143e8a3974cceb287397df Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Mon, 10 Aug 2015 14:49:27 +0200 Subject: [PATCH] Support multi line values in INI files --- library/Icinga/File/Ini/IniParser.php | 12 +++++++++--- .../php/library/Icinga/File/Ini/IniParserTest.php | 15 +++++++++++++++ .../php/library/Icinga/File/Ini/IniWriterTest.php | 8 +++----- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/library/Icinga/File/Ini/IniParser.php b/library/Icinga/File/Ini/IniParser.php index 20ff6271d..85ab20e18 100644 --- a/library/Icinga/File/Ini/IniParser.php +++ b/library/Icinga/File/Ini/IniParser.php @@ -23,6 +23,14 @@ class IniParser const COMMENT_END = 9; const LINE_END = 10; + /** + * Cancel the parsing with an error + * + * @param $message The error description + * @param $line The line in which the error occured + * + * @throws ConfigurationError + */ private static function throwParseError($message, $line) { throw new ConfigurationError(sprintf('Ini parser error: %s. (l. %d)', $message, $line)); @@ -151,9 +159,7 @@ class IniParser break; case self::DIRECTIVE_VALUE_QUOTED: - if ($s === "\n") { - self::throwParseError('Unterminated DIRECTIVE_VALUE_QUOTED', $line); - } elseif ($s === '\\') { + if ($s === '\\') { $state = self::ESCAPE; $escaping = self::DIRECTIVE_VALUE_QUOTED; } elseif ($s !== '"') { diff --git a/test/php/library/Icinga/File/Ini/IniParserTest.php b/test/php/library/Icinga/File/Ini/IniParserTest.php index bf9143e86..6d8822870 100644 --- a/test/php/library/Icinga/File/Ini/IniParserTest.php +++ b/test/php/library/Icinga/File/Ini/IniParserTest.php @@ -60,4 +60,19 @@ EOD; 'IniParser does not recognize escaped bracket in section' ); } + + public function testMultilineValues() + { + $config = <<<'EOD' +[section] +key1 = "with some +newline in the value" +EOD; + $doc = IniParser::parseIni($config); + $this->assertEquals( + 2, + count(explode("\n", $doc->getSection('section')->getDirective('key1')->getValue())), + 'IniParser does not recognize multi-line values' + ); + } } diff --git a/test/php/library/Icinga/File/Ini/IniWriterTest.php b/test/php/library/Icinga/File/Ini/IniWriterTest.php index 18ca34046..a78e96229 100644 --- a/test/php/library/Icinga/File/Ini/IniWriterTest.php +++ b/test/php/library/Icinga/File/Ini/IniWriterTest.php @@ -257,7 +257,7 @@ EOD; ); } - public function testWhetherLinebreaksAreRemoved() + public function testWhetherLinebreaksAreProcessed() { $target = $this->writeConfigToTemporaryFile(''); $writer = new IniWriter( @@ -277,7 +277,7 @@ inkey' => 'blarg' $rendered = $writer->render(); $this->assertEquals( count(explode("\n", $rendered)), - 4, + 5, 'generated config should not contain more than three line breaks' ); } @@ -327,7 +327,6 @@ EOD; [section] key1 = "value with \"quotes\"" key2 = "value with \\" -key3 = "value with newline" EOD; $target = $this->writeConfigToTemporaryFile($config); @@ -336,8 +335,7 @@ EOD; array( 'section' => array( 'key1' => 'value with "quotes"', - 'key2' => 'value with \\', - 'key3' => 'value with' . PHP_EOL . 'newline' + 'key2' => 'value with \\' ) ) ),