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() public function getText()
{ {
$this->cleanUpWhitespaces(); $this->normalizeSectionSpacing();
return rtrim(implode(PHP_EOL, $this->text)) . PHP_EOL;
// 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; $i = count($this->text) - 1;
for (; $i > 0; $i--) { for (; $i > 0; $i--) {
@ -328,24 +333,18 @@ class IniEditor
if ($this->isSectionDeclaration($line) && $i > 0) { if ($this->isSectionDeclaration($line) && $i > 0) {
$i--; $i--;
$line = $this->text[$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)) { while ($i > 0 && $this->isComment($line)) {
$i--; $i--;
$line = $this->text[$i]; $line = $this->text[$i];
} }
/* // remove whitespaces between the sections
* Remove whitespaces between the sections
*/
while ($i > 0 && preg_match('/^\s*$/', $line) === 1) { while ($i > 0 && preg_match('/^\s*$/', $line) === 1) {
$this->deleteLine($i); $this->deleteLine($i);
$i--; $i--;
$line = $this->text[$i]; $line = $this->text[$i];
} }
/* // refresh section separators
* Refresh section separators
*/
if ($i !== 0 && $this->sectionSeparators > 0) { if ($i !== 0 && $this->sectionSeparators > 0) {
$this->insertAtLine($i + 1, str_repeat(PHP_EOL, $this->sectionSeparators - 1)); $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)) { if (file_exists($this->_filename)) {
$oldconfig = new Zend_Config_Ini($this->_filename); $oldconfig = new Zend_Config_Ini($this->_filename);
$content = file_get_contents($this->_filename); $content = trim(file_get_contents($this->_filename));
} else { } else {
$oldconfig = new Zend_Config(array()); $oldconfig = new Zend_Config(array());
$content = ''; $content = '';