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 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 !== '"') {

View File

@ -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'
);
}
}

View File

@ -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 \\'
)
)
),