mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-04-07 21:05:18 +02:00
Fix errors in function comments and move IniEditor into single class file
refs #4352
This commit is contained in:
parent
56e47fd084
commit
c1338898a6
448
library/Icinga/Config/IniEditor.php
Normal file
448
library/Icinga/Config/IniEditor.php
Normal file
@ -0,0 +1,448 @@
|
||||
<?php
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
/**
|
||||
* This file is part of Icinga 2 Web.
|
||||
*
|
||||
* Icinga 2 Web - Head for multiple monitoring backends.
|
||||
* Copyright (C) 2013 Icinga Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
||||
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
||||
* @author Icinga Development Team <info@icinga.org>
|
||||
*/
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
namespace Icinga\Config;
|
||||
|
||||
use Icinga\Exception\ConfigurationError;
|
||||
|
||||
/**
|
||||
* Edit the sections and keys of an ini in-place
|
||||
*/
|
||||
class IniEditor
|
||||
{
|
||||
/**
|
||||
* The text that is edited
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* The symbol that is used to separate keys
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $nestSeparator = '.';
|
||||
|
||||
/**
|
||||
* Create a new IniEditor
|
||||
*
|
||||
* @param $content The content of the ini as string
|
||||
*/
|
||||
public function __construct($content)
|
||||
{
|
||||
$this->text = explode("\n",$content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of the given key.
|
||||
*
|
||||
* @param array $key The key to set
|
||||
* @param $value The value to set
|
||||
* @param $section The section to insert to.
|
||||
*/
|
||||
public function set(array $key,$value,$section = null)
|
||||
{
|
||||
$line = $this->getKeyLine($key,$section);
|
||||
if ($line === -1) {
|
||||
$this->insert($key,$value,$section);
|
||||
} else {
|
||||
$content = $this->formatKeyValuePair($key,$value);
|
||||
$this->updateLine($line,$content);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the value of the given array element
|
||||
*
|
||||
* @param $key The key of the array value
|
||||
* @param $section The section of the array.
|
||||
*/
|
||||
public function resetArrayElement(array $key, $section = null)
|
||||
{
|
||||
$line = $this->getArrayEl($key, $section);
|
||||
if ($line !== -1) {
|
||||
$this->deleteLine($line);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value for an array element
|
||||
*
|
||||
* @param array $key The key of the property
|
||||
* @param $value The value of the property
|
||||
* @param $section The section to use
|
||||
*/
|
||||
public function setArrayElement(array $key,$value,$section = null)
|
||||
{
|
||||
$line = $this->getArrayEl($key,$section);
|
||||
if ($line !== -1) {
|
||||
if (isset($section)) {
|
||||
$this->updateLine($line,$this->formatKeyValuePair($key,$value));
|
||||
} else {
|
||||
$section = $key[0];
|
||||
unset($key[0]);
|
||||
$this->deleteLine($line);
|
||||
$this->setSection($section);
|
||||
$this->insert($key,$value,$section);
|
||||
}
|
||||
} else {
|
||||
$this->insert($key,$value,$section);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the line of an array element
|
||||
*
|
||||
* @param array $key The key of the property.
|
||||
* @param $value The value
|
||||
* @param $section The section to use
|
||||
*
|
||||
* @return The line of the array element.
|
||||
*/
|
||||
private function getArrayEl(array $key,$section = null)
|
||||
{
|
||||
$line = isset($section) ? $this->getSectionLine($section) +1 : 0;
|
||||
$index = array_pop($key);
|
||||
$formatted = $this->formatKey($key);
|
||||
for (;$line < count($this->text);$line++) {
|
||||
$l = $this->text[$line];
|
||||
if ($this->isSectionDeclaration($l)) {
|
||||
return -1;
|
||||
}
|
||||
if (strlen($formatted) > 0) {
|
||||
if (preg_match('/^'.$formatted.'\[\]/',$l) === 1 ||
|
||||
preg_match(
|
||||
'/^'.$formatted.$this->nestSeparator.$index.'/',$l) === 1){
|
||||
return $line;
|
||||
}
|
||||
} else {
|
||||
if (preg_match('/^'.$index.'/',$l) === 1 ) {
|
||||
return $line;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* When it exists, set the key back to null
|
||||
*
|
||||
* @param array $key The key to reset
|
||||
* @param $section The section of the key
|
||||
*/
|
||||
public function reset(array $key,$section = null)
|
||||
{
|
||||
$line = $this->getKeyLine($key,$section);
|
||||
if ($line === -1) {
|
||||
return;
|
||||
}
|
||||
$this->deleteLine($line);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the section if it does not exist and set the properties
|
||||
*
|
||||
* @param $section The section name
|
||||
* @param $extend The section that should be extended by this section
|
||||
*/
|
||||
public function setSection($section,$extend = null)
|
||||
{
|
||||
if (isset($extend)) {
|
||||
$decl = '['.$section.' : '.$extend.']';
|
||||
} else {
|
||||
$decl = '['.$section.']';
|
||||
}
|
||||
$line = $this->getSectionLine($section);
|
||||
if ($line !== -1) {
|
||||
$this->deleteLine($line);
|
||||
$this->insertAtLine($line,$decl);
|
||||
} else {
|
||||
$line = $this->getLastLine();
|
||||
$this->insertAtLine($line,$decl);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a section declaration
|
||||
*
|
||||
* @param $section The section name
|
||||
*/
|
||||
public function removeSection($section)
|
||||
{
|
||||
$line = $this->getSectionLine($section);
|
||||
if ($line !== -1) {
|
||||
$this->deleteLine($line);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the key at the end of the corresponding section
|
||||
*
|
||||
* @param array $key The key to insert
|
||||
* @param $value The value to insert
|
||||
* @param array $key The key to insert
|
||||
*/
|
||||
private function insert(array $key,$value,$section = null)
|
||||
{
|
||||
$line = $this->getSectionEnd($section);
|
||||
$content = $this->formatKeyValuePair($key,$value);
|
||||
$this->insertAtLine($line,$content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the edited text
|
||||
*
|
||||
* @return string The edited text
|
||||
*/
|
||||
public function getText()
|
||||
{
|
||||
$this->cleanUpWhitespaces();
|
||||
return implode("\n",$this->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all unneeded line breaks between sections
|
||||
*/
|
||||
private function cleanUpWhitespaces()
|
||||
{
|
||||
$i = count($this->text) - 1;
|
||||
for (;$i >= 0; $i--) {
|
||||
$line = $this->text[$i];
|
||||
if ($this->isSectionDeclaration($line)) {
|
||||
$i--;
|
||||
$line = $this->text[$i];
|
||||
while ($i > 0 && preg_match('/^[\s]*$/',$line) === 1) {
|
||||
$this->deleteLine($i);
|
||||
$i--;
|
||||
$line = $this->text[$i];
|
||||
}
|
||||
if ($i !== 0) {
|
||||
$this->insertAtLine($i + 1,'');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the text at line $lineNr
|
||||
*
|
||||
* @param $lineNr The line nr the inserted line should have
|
||||
* @param $toInsert The text that will be inserted
|
||||
*/
|
||||
private function insertAtLine($lineNr,$toInsert)
|
||||
{
|
||||
$this->text = IniEditor::insertIntoArray($this->text,$lineNr,$toInsert);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the line $lineNr
|
||||
*
|
||||
* @param $lineNr The line number of the target line
|
||||
* @param $toInsert The new line content
|
||||
*/
|
||||
private function updateLine($lineNr,$content)
|
||||
{
|
||||
$this->text[$lineNr] = $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the line $lineNr
|
||||
*
|
||||
* @param $lineNr The lineNr starting at 0
|
||||
*/
|
||||
private function deleteLine($lineNr)
|
||||
{
|
||||
$this->text = $this->removeFromArray($this->text,$lineNr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a key-value pair to an INI file-entry
|
||||
*
|
||||
* @param array $key The key
|
||||
* @param $value The value
|
||||
*
|
||||
* @return string The formatted key-value pair
|
||||
*/
|
||||
private function formatKeyValuePair(array $key,$value)
|
||||
{
|
||||
return $this->formatKey($key).'='.$this->formatValue($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a key to an INI key
|
||||
*/
|
||||
private function formatKey(array $key)
|
||||
{
|
||||
return implode($this->nestSeparator,$key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first line after the given $section
|
||||
*
|
||||
* @param $section The name of the section
|
||||
*
|
||||
* @return int The line number of the section
|
||||
*/
|
||||
private function getSectionEnd($section = null)
|
||||
{
|
||||
$i = 0;
|
||||
$started = isset($section) ? false: true;
|
||||
foreach ($this->text as $line) {
|
||||
if ($started && preg_match('/^\[/',$line) === 1) {
|
||||
return $i;
|
||||
} elseif (preg_match('/^\['.$section.'.*\]/',$line) === 1) {
|
||||
$started = true;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
if (!$started) {
|
||||
return -1;
|
||||
}
|
||||
return $i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given line contains a section declaration
|
||||
*
|
||||
* @param $lineContent The content of the line
|
||||
* @param string $section The optional section name that will be assumed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isSectionDeclaration($lineContent,$section = "")
|
||||
{
|
||||
return preg_match('/^\[/'.$section,$lineContent) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the line where the section begins
|
||||
*
|
||||
* @param $section The section
|
||||
*
|
||||
* @return int The line number
|
||||
*/
|
||||
private function getSectionLine($section)
|
||||
{
|
||||
$i = 0;
|
||||
foreach ($this->text as $line) {
|
||||
if (preg_match('/^\['.$section.'/',$line)) {
|
||||
return $i;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the line number where the given key occurs
|
||||
*
|
||||
* @param array $keys The key and its parents
|
||||
* @param $section The section of the key
|
||||
*
|
||||
* @return int The line number
|
||||
*/
|
||||
private function getKeyLine(array $keys,$section = null)
|
||||
{
|
||||
$key = implode($this->nestSeparator,$keys);
|
||||
$inSection = isset($section) ? false : true;
|
||||
$i = 0;
|
||||
foreach ($this->text as $line) {
|
||||
if ($inSection && preg_match('/^\[/',$line) === 1) {
|
||||
return -1;
|
||||
}
|
||||
if ($inSection && preg_match('/^'.$key.'/',$line) === 1) {
|
||||
return $i;
|
||||
}
|
||||
if (!$inSection && preg_match('/^\['.$section.'/',$line) === 1) {
|
||||
$inSection = true;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last line number occurring in the text
|
||||
*
|
||||
* @return The line number of the last line
|
||||
*/
|
||||
private function getLastLine()
|
||||
{
|
||||
return count($this->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new element into a specific position of an array
|
||||
*
|
||||
* @param $array The array to use
|
||||
* @param $pos The target position
|
||||
* @param $element The element to insert
|
||||
*
|
||||
* @return array The changed array
|
||||
*/
|
||||
private static function insertIntoArray($array,$pos,$element)
|
||||
{
|
||||
array_splice($array, $pos, 0, $element);
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an element from an array
|
||||
*
|
||||
* @param $array The array to use
|
||||
* @param $pos The position to remove
|
||||
*/
|
||||
private function removeFromArray($array,$pos)
|
||||
{
|
||||
unset($array[$pos]);
|
||||
return array_values($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a value for INI
|
||||
*
|
||||
* @param $value The value of the string
|
||||
*
|
||||
* @return string The formatted value
|
||||
*
|
||||
* @throws Zend_Config_Exception
|
||||
*/
|
||||
private function formatValue($value)
|
||||
{
|
||||
if (is_integer($value) || is_float($value)) {
|
||||
return $value;
|
||||
} elseif (is_bool($value)) {
|
||||
return ($value ? 'true' : 'false');
|
||||
} elseif (strpos($value, '"') === false) {
|
||||
return '"' . $value . '"';
|
||||
} else {
|
||||
throw new ConfigurationError('Value can not contain double quotes "');
|
||||
}
|
||||
}
|
||||
}
|
@ -26,9 +26,10 @@
|
||||
*/
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
|
||||
namespace Icinga\Config;
|
||||
|
||||
use Zend_Config;
|
||||
use Zend_Config_Ini;
|
||||
|
||||
/**
|
||||
* A ini file adapter that respects the file structure and the comments of already
|
||||
@ -53,7 +54,7 @@ class PreservingIniWriter extends \Zend_Config_Writer_FileAbstract
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
$oldconfig = new \Zend_Config_Ini($this->_filename);
|
||||
$oldconfig = new Zend_Config_Ini($this->_filename);
|
||||
$newconfig = $this->_config;
|
||||
$editor = new IniEditor(file_get_contents($this->_filename));
|
||||
$this->diffConfigs($oldconfig,$newconfig,$editor);
|
||||
@ -63,57 +64,60 @@ class PreservingIniWriter extends \Zend_Config_Writer_FileAbstract
|
||||
/**
|
||||
* Create a property diff and apply the changes to the editor
|
||||
*
|
||||
* Compare two Zend_Config that represent the state change of an ini file and use the
|
||||
* IniEditor to write the changes back to the config, while preserving the structure and
|
||||
* the comments of the original file.
|
||||
*
|
||||
* @param Zend_Config $oldconfig The config representing the state before the change
|
||||
* @param Zend_Config $newconfig The config representing the state after the change
|
||||
* @param IniEditor $editor The editor that should be used to edit the old config file
|
||||
* @param array $parents The parent keys that should be respected when editing the config
|
||||
* @param Zend_Config $oldconfig The config representing the state before the change
|
||||
* @param Zend_Config $newconfig The config representing the state after the change
|
||||
* @param IniEditor $eeditor The editor that should be used to edit the old config file
|
||||
* @param array $parents The parent keys that should be respected when editing the config
|
||||
*/
|
||||
private function diffConfigs(
|
||||
\Zend_Config $oldconfig,
|
||||
\Zend_Config $newconfig,
|
||||
Zend_Config $oldconfig,
|
||||
Zend_Config $newconfig,
|
||||
IniEditor $editor,
|
||||
array $parents = array())
|
||||
{
|
||||
$section = empty($parents) ? null : $parents[0];
|
||||
foreach ($newconfig as $key => $value) {
|
||||
$oldvalue = $oldconfig->get($key);
|
||||
$fullKey = array_merge($parents,array($key));
|
||||
if ($value instanceof \Zend_Config) {
|
||||
if (empty($parents)) {
|
||||
$nextParents = array_merge($parents,array($key));
|
||||
$ident = empty($parents) ?
|
||||
array($key) : array_slice($nextParents,1,null,true);
|
||||
if ($value instanceof Zend_Config) {
|
||||
if (!isset($section)) {
|
||||
$extends = $newconfig->getExtends();
|
||||
$extend = array_key_exists($key,$extends) ? $extends[$key] : null;
|
||||
$extend = array_key_exists($key,$extends) ?
|
||||
$extends[$key] : null;
|
||||
$editor->setSection($key,$extend);
|
||||
}
|
||||
if (!isset($oldvalue)) {
|
||||
$this->diffConfigs(new \Zend_Config(array()),$value,$editor,$fullKey);
|
||||
} else {
|
||||
$this->diffConfigs($oldvalue,$value,$editor,$fullKey);
|
||||
$oldvalue = new Zend_Config(array());
|
||||
}
|
||||
$this->diffConfigs($oldvalue,$value,$editor,$nextParents);
|
||||
} else {
|
||||
if (is_numeric($key)){
|
||||
$editor->setArrayEl($fullKey,$value);
|
||||
$editor->setArrayElement($ident,$value,$section);
|
||||
} else {
|
||||
$editor->set($fullKey,$value);
|
||||
$editor->set($ident,$value,$section);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($oldconfig as $key => $value) {
|
||||
$fullKey = array_merge($parents,array($key));
|
||||
$o = $newconfig->get($key);
|
||||
if (!isset($o)) {
|
||||
if ($value instanceof \Zend_Config) {
|
||||
$nextParents = array_merge($parents,array($key));
|
||||
$newvalue = $newconfig->get($key);
|
||||
$ident = empty($parents) ?
|
||||
array($key) : array_slice($nextParents,1,null,true);
|
||||
if (!isset($newvalue)) {
|
||||
if ($value instanceof Zend_Config) {
|
||||
$this->diffConfigs(
|
||||
$value,new \Zend_Config(array()),$editor,$fullKey
|
||||
$value,new Zend_Config(array()),$editor,$nextParents
|
||||
);
|
||||
$editor->removeSection($key);
|
||||
if (!isset($section)) {
|
||||
$editor->removeSection($key);
|
||||
}
|
||||
} else {
|
||||
if (is_numeric($key)) {
|
||||
$editor->delArrayEl($fullKey);
|
||||
$editor->resetArrayElement($ident,$section);
|
||||
} else {
|
||||
$editor->reset($fullKey);
|
||||
$editor->reset($ident,$section);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -121,452 +125,3 @@ class PreservingIniWriter extends \Zend_Config_Writer_FileAbstract
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Edit the sections and keys of an ini in-place
|
||||
*/
|
||||
class IniEditor
|
||||
{
|
||||
/**
|
||||
* The text that is edited
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* The symbol that is used
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $nestSeparator = '.';
|
||||
|
||||
/**
|
||||
* Get the nest separator
|
||||
*
|
||||
* @return string The nest separator
|
||||
*/
|
||||
public function getNestSeparator()
|
||||
{
|
||||
return $this->nestSeparator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the nest separator
|
||||
*
|
||||
* @param $separator The nest separator
|
||||
* @return mixed The current instance of IniReader
|
||||
*/
|
||||
public function setNestSeparator($separator)
|
||||
{
|
||||
$this->nestSeparator = $separator;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new IniEditor
|
||||
*
|
||||
* @param $content The content of the ini as string
|
||||
*/
|
||||
public function __construct($content)
|
||||
{
|
||||
$this->text = explode("\n",$content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of the given key.
|
||||
*
|
||||
* Update the key, if it already exists, otherwise create it.
|
||||
*
|
||||
* @param array $key
|
||||
* @param $value
|
||||
*/
|
||||
public function set(array $key,$value)
|
||||
{
|
||||
$line = $this->getKeyLine($key);
|
||||
if ($line === -1) {
|
||||
$this->insert($key,$value);
|
||||
return;
|
||||
}
|
||||
$content = $this->formatKeyValuePair(
|
||||
$this->truncateSection($key),$value);
|
||||
$this->updateLine($line,$content);
|
||||
}
|
||||
|
||||
public function delArrayEl(array $key)
|
||||
{
|
||||
$line = $this->getArrayEl($key);
|
||||
if ($line !== -1) {
|
||||
$this->deleteLine($line);
|
||||
}
|
||||
}
|
||||
|
||||
public function setArrayEl(array $key,$value)
|
||||
{
|
||||
$line = $this->getArrayEl($key);
|
||||
if (count($key) > 1) {
|
||||
$ident = $this->truncateSection($key);
|
||||
$section = $key[0];
|
||||
} else {
|
||||
$ident = $key;
|
||||
$section = null;
|
||||
}
|
||||
if ($line !== -1) {
|
||||
if (count($ident) > 1){
|
||||
$this->updateLine($line,$this->formatKeyValuePair($ident,$value));
|
||||
} else {
|
||||
// move into own section
|
||||
$this->deleteLine($line);
|
||||
$this->setSection($section);
|
||||
$this->insert(array_merge(array($section),$ident),$value);
|
||||
}
|
||||
} else {
|
||||
$e = $this->getSectionEnd($section);
|
||||
$this->insertAtLine($e,$this->formatKeyValuePair($ident,$value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the line of an array element
|
||||
*
|
||||
* @param array $key The key of the property.
|
||||
* @param $value The value
|
||||
*/
|
||||
private function getArrayEl(array $key)
|
||||
{
|
||||
$line = 0;
|
||||
if (count($key) > 1) {
|
||||
$line = $this->getSectionDeclLine($key[0]) + 1;
|
||||
$validKey = array_slice($key,1,null,true);
|
||||
}
|
||||
$index = array_pop($validKey);
|
||||
$formattedKey = explode('=',$this->formatKeyValuePair($validKey,''));
|
||||
$formattedKey = $formattedKey[0];
|
||||
|
||||
for (;$line < count($this->text);$line++) {
|
||||
$l = $this->text[$line];
|
||||
if ($this->isSectionDecl($l)) {
|
||||
return -1;
|
||||
}
|
||||
if (strlen($formattedKey) > 0) {
|
||||
if (preg_match('/^'.$formattedKey.'\[\]/',$l) === 1 ||
|
||||
preg_match('/^'.$formattedKey.'.'.$index.'/',$l) === 1 ) {
|
||||
return $line;
|
||||
}
|
||||
} else {
|
||||
if (preg_match('/^'.$index.'/',$l) === 1 ) {
|
||||
return $line;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the given key
|
||||
*
|
||||
* Set the key to null, if it already exists. Otherwise do nothing.
|
||||
*
|
||||
* @param array $key
|
||||
*/
|
||||
public function reset(array $key)
|
||||
{
|
||||
$line = $this->getKeyLine($key);
|
||||
if ($line === -1) {
|
||||
return;
|
||||
}
|
||||
$this->deleteLine($line);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the extended section of $section
|
||||
*/
|
||||
public function setSection($section,$extend = null)
|
||||
{
|
||||
if (isset($extend)) {
|
||||
$decl = '['.$section.' : '.$extend.']';
|
||||
} else {
|
||||
$decl = '['.$section.']';
|
||||
}
|
||||
$line = $this->getSectionDeclLine($section);
|
||||
if ($line !== -1) {
|
||||
$this->deleteLine($line);
|
||||
$this->insertAtLine($line,$decl);
|
||||
} else {
|
||||
$line = $this->getLastLine();
|
||||
$this->insertAtLine($line,$decl);
|
||||
$this->insertAtLine($line,"");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the section declarationa of $section
|
||||
*/
|
||||
public function removeSection($section)
|
||||
{
|
||||
$line = $this->getSectionDeclLine($section);
|
||||
if ($line !== -1) {
|
||||
$this->deleteLine($line);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a key
|
||||
*
|
||||
* Insert the key at the end of the corresponding section.
|
||||
*
|
||||
* @param array $key The key to insert
|
||||
* @param $value The value to insert
|
||||
*/
|
||||
private function insert(array $key,$value)
|
||||
{
|
||||
if (count($key) > 1) {
|
||||
// insert into end of section
|
||||
$line = $this->getSectionEnd($key[0]);
|
||||
} else {
|
||||
// insert into section-less space
|
||||
$line = $this->getSectionEnd();
|
||||
}
|
||||
$content = $this->formatKeyValuePair($this->truncateSection($key),$value);
|
||||
$this->insertAtLine($line,$content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the edited text
|
||||
*
|
||||
* @return string The edited text
|
||||
*/
|
||||
public function getText()
|
||||
{
|
||||
// clean up whitespaces
|
||||
$i = count($this->text) - 1;
|
||||
for (;$i >= 0; $i--) {
|
||||
$line = $this->text[$i];
|
||||
if ($this->isSectionDecl($line)) {
|
||||
$i--;
|
||||
$line = $this->text[$i];
|
||||
while ($i >= 0 && preg_match('/^[\s]*$/',$line) === 1) {
|
||||
$this->deleteLine($i);
|
||||
$i--;
|
||||
$line = $this->text[$i];
|
||||
}
|
||||
if ($i !== 0) {
|
||||
$this->insertAtLine($i + 1,'');
|
||||
}
|
||||
}
|
||||
}
|
||||
return implode("\n",$this->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the text at line $lineNr
|
||||
*
|
||||
* @param $lineNr The line nr the inserted line should have
|
||||
* @param $toInsert The text that will be inserted
|
||||
*/
|
||||
private function insertAtLine($lineNr,$toInsert)
|
||||
{
|
||||
$this->text = IniEditor::insertIntoArray($this->text,$lineNr,$toInsert);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the line $lineNr
|
||||
*
|
||||
* @param $lineNr
|
||||
* @param $toInsert The lineNr starting at 0
|
||||
*/
|
||||
private function updateLine($lineNr,$toInsert)
|
||||
{
|
||||
$this->text[$lineNr] = $toInsert;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the line $lineNr
|
||||
*
|
||||
* @param $lineNr The lineNr starting at 0
|
||||
*/
|
||||
private function deleteLine($lineNr)
|
||||
{
|
||||
$this->text = $this->removeFromArray($this->text,$lineNr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a key-value pair to an INI file-entry
|
||||
*
|
||||
* @param array $key The key
|
||||
* @param $value The value
|
||||
*
|
||||
* @return string The formatted key-value pair
|
||||
*/
|
||||
private function formatKeyValuePair(array $key,$value)
|
||||
{
|
||||
return implode($this->nestSeparator,$key).'='.$this->_prepareValue($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip the section off of a key, when necessary.
|
||||
*
|
||||
* @param array $key
|
||||
* @return array
|
||||
*/
|
||||
private function truncateSection(array $key)
|
||||
{
|
||||
if (count($key) > 1) {
|
||||
unset($key[0]);
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first line after the given $section
|
||||
*
|
||||
* If section is empty, return the end of section-less
|
||||
* space at the file start.
|
||||
*
|
||||
* @param $section The name of the section
|
||||
* @return int
|
||||
*/
|
||||
private function getSectionEnd($section = null)
|
||||
{
|
||||
$i = 0;
|
||||
$started = false;
|
||||
if (!isset($section)) {
|
||||
$started = true;
|
||||
}
|
||||
foreach ($this->text as $line) {
|
||||
if ($started) {
|
||||
if (preg_match('/^\[/',$line) === 1) {
|
||||
return $i;
|
||||
}
|
||||
} elseif (preg_match('/^\['.$section.'.*\]/',$line) === 1) {
|
||||
$started = true;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
if (!$started) {
|
||||
return -1;
|
||||
}
|
||||
return $i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given line contains a section declaration
|
||||
*
|
||||
* @param $lineContent The content of the line
|
||||
* @param string $section The optional section name that will be assumed
|
||||
* @return bool
|
||||
*/
|
||||
private function isSectionDecl($lineContent,$section = "")
|
||||
{
|
||||
return preg_match('/^\[/'.$section,$lineContent) === 1;
|
||||
}
|
||||
|
||||
private function getSectionDeclLine($section)
|
||||
{
|
||||
$i = 0;
|
||||
foreach ($this->text as $line) {
|
||||
if (preg_match('/^\['.$section.'/',$line)) {
|
||||
return $i;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the line number of the given key
|
||||
*
|
||||
* When sections are active, return the first matching key in the key's
|
||||
* section, otherwise return the first matching key.
|
||||
*
|
||||
* @param array $keys The key and its parents
|
||||
*/
|
||||
private function getKeyLine(array $keys)
|
||||
{
|
||||
// remove section
|
||||
if (count($keys) > 1) {
|
||||
// the key is in a section
|
||||
$section = $keys[0];
|
||||
$key = implode($this->nestSeparator,array_slice($keys,1,null,true));
|
||||
$inSection = false;
|
||||
} else {
|
||||
// section-less key
|
||||
$section = null;
|
||||
$key = implode($this->nestSeparator,$keys);
|
||||
$inSection = true;
|
||||
}
|
||||
$i = 0;
|
||||
foreach ($this->text as $line) {
|
||||
if ($inSection && preg_match('/^\[/',$line) === 1) {
|
||||
return -1;
|
||||
}
|
||||
if ($inSection && preg_match('/^'.$key.'/',$line) === 1) {
|
||||
return $i;
|
||||
}
|
||||
if (!$inSection && preg_match('/^\['.$section.'/',$line) === 1) {
|
||||
$inSection = true;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last line number
|
||||
*
|
||||
* @return int The line nr. of the last line
|
||||
*/
|
||||
private function getLastLine()
|
||||
{
|
||||
return count($this->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new element into a specific position of an array
|
||||
*
|
||||
* @param $array The array to use
|
||||
* @param $pos The target position
|
||||
* @param $element The element to insert
|
||||
*/
|
||||
private static function insertIntoArray($array,$pos,$element)
|
||||
{
|
||||
array_splice($array, $pos, 0, $element);
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an element from an array
|
||||
*
|
||||
* @param $array The array to use
|
||||
* @param $pos The position to remove
|
||||
*/
|
||||
private function removeFromArray($array,$pos)
|
||||
{
|
||||
unset($array[$pos]);
|
||||
return array_values($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a value for INI
|
||||
*
|
||||
* @param $value
|
||||
* @return string
|
||||
* @throws Zend_Config_Exception
|
||||
*/
|
||||
protected function _prepareValue($value)
|
||||
{
|
||||
if (is_integer($value) || is_float($value)) {
|
||||
return $value;
|
||||
} elseif (is_bool($value)) {
|
||||
return ($value ? 'true' : 'false');
|
||||
} elseif (strpos($value, '"') === false) {
|
||||
return '"' . $value . '"';
|
||||
} else {
|
||||
/** @see Zend_Config_Exception */
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception('Value can not contain double quotes "');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
IcingaConfig::$configDir = dirname(__FILE__) . '/Config/files';
|
||||
IcingaConfig::$configDir = dirname(__FILE__) . '/files';
|
||||
}
|
||||
|
||||
public function testAppConfig()
|
||||
|
@ -33,6 +33,7 @@ namespace Tests\Icinga\PreservingIniWriterTest;
|
||||
require_once 'Zend/Config.php';
|
||||
require_once 'Zend/Config/Ini.php';
|
||||
require_once 'Zend/Config/Writer/Ini.php';
|
||||
require_once('../../library/Icinga/Config/IniEditor.php');
|
||||
require_once('../../library/Icinga/Config/PreservingIniWriter.php');
|
||||
|
||||
use Icinga\Config\PreservingIniWriter;
|
||||
@ -255,8 +256,7 @@ prop2="5"
|
||||
'Section property numerical created.');
|
||||
|
||||
$this->assertNull($config->parent->list->{0},
|
||||
'Section array deleted'
|
||||
);
|
||||
'Section array deleted');
|
||||
|
||||
$this->assertEquals('new',$config->parent->list->{1},
|
||||
'Section array changed.');
|
||||
|
Loading…
x
Reference in New Issue
Block a user