Fix spacing in ini files

refs #8706
This commit is contained in:
Matthias Jentsch 2015-06-08 12:22:27 +02:00
parent 1bd18c5adb
commit ddba1d86fa
2 changed files with 13 additions and 14 deletions

View File

@ -313,14 +313,19 @@ class IniEditor
*/
public function getText()
{
$this->cleanUpWhitespaces();
return rtrim(implode(PHP_EOL, $this->text)) . PHP_EOL;
$this->normalizeSectionSpacing();
// trim leading and trailing whitespaces from generated file
$txt = trim(implode(PHP_EOL, $this->text)) . PHP_EOL;
// replace linebreaks, unless they preceed a comment or a section
return preg_replace("/\n[\n]*([^;\[])/", "\n$1", $txt);
}
/**
* Remove all unneeded line breaks between sections
* normalize section spacing according to the current settings
*/
private function cleanUpWhitespaces()
private function normalizeSectionSpacing()
{
$i = count($this->text) - 1;
for (; $i > 0; $i--) {
@ -328,24 +333,18 @@ class IniEditor
if ($this->isSectionDeclaration($line) && $i > 0) {
$i--;
$line = $this->text[$i];
/*
* Ignore comments that are glued to the section declaration
*/
// ignore comments that are glued to the section declaration
while ($i > 0 && $this->isComment($line)) {
$i--;
$line = $this->text[$i];
}
/*
* Remove whitespaces between the sections
*/
// remove whitespaces between the sections
while ($i > 0 && preg_match('/^\s*$/', $line) === 1) {
$this->deleteLine($i);
$i--;
$line = $this->text[$i];
}
/*
* Refresh section separators
*/
// refresh section separators
if ($i !== 0 && $this->sectionSeparators > 0) {
$this->insertAtLine($i + 1, str_repeat(PHP_EOL, $this->sectionSeparators - 1));
}

View File

@ -60,7 +60,7 @@ class IniWriter extends Zend_Config_Writer_FileAbstract
{
if (file_exists($this->_filename)) {
$oldconfig = new Zend_Config_Ini($this->_filename);
$content = file_get_contents($this->_filename);
$content = trim(file_get_contents($this->_filename));
} else {
$oldconfig = new Zend_Config(array());
$content = '';