Support multi line values in INI files

This commit is contained in:
Matthias Jentsch 2015-08-10 14:49:27 +02:00
parent 0468bddc83
commit fe805c82ca
3 changed files with 27 additions and 8 deletions

View File

@ -23,6 +23,14 @@ class IniParser
const COMMENT_END = 9; const COMMENT_END = 9;
const LINE_END = 10; 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) private static function throwParseError($message, $line)
{ {
throw new ConfigurationError(sprintf('Ini parser error: %s. (l. %d)', $message, $line)); throw new ConfigurationError(sprintf('Ini parser error: %s. (l. %d)', $message, $line));
@ -151,9 +159,7 @@ class IniParser
break; break;
case self::DIRECTIVE_VALUE_QUOTED: case self::DIRECTIVE_VALUE_QUOTED:
if ($s === "\n") { if ($s === '\\') {
self::throwParseError('Unterminated DIRECTIVE_VALUE_QUOTED', $line);
} elseif ($s === '\\') {
$state = self::ESCAPE; $state = self::ESCAPE;
$escaping = self::DIRECTIVE_VALUE_QUOTED; $escaping = self::DIRECTIVE_VALUE_QUOTED;
} elseif ($s !== '"') { } elseif ($s !== '"') {

View File

@ -60,4 +60,19 @@ EOD;
'IniParser does not recognize escaped bracket in section' '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'
);
}
} }

View File

@ -257,7 +257,7 @@ EOD;
); );
} }
public function testWhetherLinebreaksAreRemoved() public function testWhetherLinebreaksAreProcessed()
{ {
$target = $this->writeConfigToTemporaryFile(''); $target = $this->writeConfigToTemporaryFile('');
$writer = new IniWriter( $writer = new IniWriter(
@ -277,7 +277,7 @@ inkey' => 'blarg'
$rendered = $writer->render(); $rendered = $writer->render();
$this->assertEquals( $this->assertEquals(
count(explode("\n", $rendered)), count(explode("\n", $rendered)),
4, 5,
'generated config should not contain more than three line breaks' 'generated config should not contain more than three line breaks'
); );
} }
@ -327,7 +327,6 @@ EOD;
[section] [section]
key1 = "value with \"quotes\"" key1 = "value with \"quotes\""
key2 = "value with \\" key2 = "value with \\"
key3 = "value with newline"
EOD; EOD;
$target = $this->writeConfigToTemporaryFile($config); $target = $this->writeConfigToTemporaryFile($config);
@ -336,8 +335,7 @@ EOD;
array( array(
'section' => array( 'section' => array(
'key1' => 'value with "quotes"', 'key1' => 'value with "quotes"',
'key2' => 'value with \\', 'key2' => 'value with \\'
'key3' => 'value with' . PHP_EOL . 'newline'
) )
) )
), ),