2013-08-01 09:18:14 +02:00
|
|
|
<?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;
|
|
|
|
|
2013-08-06 17:11:44 +02:00
|
|
|
use Zend_Config;
|
|
|
|
use Zend_Config_Ini;
|
2013-08-06 16:28:26 +02:00
|
|
|
|
2013-08-01 09:18:14 +02:00
|
|
|
/**
|
2013-08-06 16:28:26 +02:00
|
|
|
* A ini file adapter that respects the file structure and the comments of already
|
|
|
|
* existing ini files
|
2013-08-01 09:18:14 +02:00
|
|
|
*/
|
2013-08-06 16:28:26 +02:00
|
|
|
class PreservingIniWriter extends \Zend_Config_Writer_FileAbstract
|
2013-08-01 09:18:14 +02:00
|
|
|
{
|
|
|
|
/**
|
2013-08-06 16:28:26 +02:00
|
|
|
* Create a new instance of PreservingIniWriter
|
2013-08-01 09:18:14 +02:00
|
|
|
*
|
2013-08-06 16:28:26 +02:00
|
|
|
* @param array $options The options passed to the base class
|
2013-08-01 09:18:14 +02:00
|
|
|
*/
|
2013-08-06 16:28:26 +02:00
|
|
|
function __construct(array $options)
|
2013-08-01 09:18:14 +02:00
|
|
|
{
|
2013-08-06 16:28:26 +02:00
|
|
|
parent::__construct($options);
|
2013-08-01 09:18:14 +02:00
|
|
|
}
|
|
|
|
|
2013-08-06 16:28:26 +02:00
|
|
|
/**
|
|
|
|
* Render the Zend_Config into a config file string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function render()
|
2013-08-01 09:18:14 +02:00
|
|
|
{
|
2013-08-06 17:11:44 +02:00
|
|
|
$oldconfig = new Zend_Config_Ini($this->_filename);
|
2013-08-06 16:28:26 +02:00
|
|
|
$newconfig = $this->_config;
|
|
|
|
$editor = new IniEditor(file_get_contents($this->_filename));
|
|
|
|
$this->diffConfigs($oldconfig,$newconfig,$editor);
|
|
|
|
return $editor->getText();
|
2013-08-01 09:18:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-06 16:28:26 +02:00
|
|
|
* Create a property diff and apply the changes to the editor
|
|
|
|
*
|
2013-08-06 17:11:44 +02:00
|
|
|
* @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
|
2013-08-01 09:18:14 +02:00
|
|
|
*/
|
2013-08-06 16:28:26 +02:00
|
|
|
private function diffConfigs(
|
2013-08-06 17:11:44 +02:00
|
|
|
Zend_Config $oldconfig,
|
|
|
|
Zend_Config $newconfig,
|
2013-08-06 16:28:26 +02:00
|
|
|
IniEditor $editor,
|
|
|
|
array $parents = array())
|
2013-08-01 09:18:14 +02:00
|
|
|
{
|
2013-08-06 17:11:44 +02:00
|
|
|
$section = empty($parents) ? null : $parents[0];
|
2013-08-06 16:28:26 +02:00
|
|
|
foreach ($newconfig as $key => $value) {
|
|
|
|
$oldvalue = $oldconfig->get($key);
|
2013-08-06 17:11:44 +02:00
|
|
|
$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)) {
|
2013-08-06 16:28:26 +02:00
|
|
|
$extends = $newconfig->getExtends();
|
2013-08-06 17:11:44 +02:00
|
|
|
$extend = array_key_exists($key,$extends) ?
|
|
|
|
$extends[$key] : null;
|
2013-08-06 16:28:26 +02:00
|
|
|
$editor->setSection($key,$extend);
|
|
|
|
}
|
|
|
|
if (!isset($oldvalue)) {
|
2013-08-06 17:11:44 +02:00
|
|
|
$oldvalue = new Zend_Config(array());
|
2013-08-06 16:28:26 +02:00
|
|
|
}
|
2013-08-06 17:11:44 +02:00
|
|
|
$this->diffConfigs($oldvalue,$value,$editor,$nextParents);
|
2013-08-06 16:28:26 +02:00
|
|
|
} else {
|
|
|
|
if (is_numeric($key)){
|
2013-08-06 17:11:44 +02:00
|
|
|
$editor->setArrayElement($ident,$value,$section);
|
2013-08-06 16:28:26 +02:00
|
|
|
} else {
|
2013-08-06 17:11:44 +02:00
|
|
|
$editor->set($ident,$value,$section);
|
2013-08-06 16:28:26 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-01 09:18:14 +02:00
|
|
|
}
|
2013-08-06 16:28:26 +02:00
|
|
|
foreach ($oldconfig as $key => $value) {
|
2013-08-06 17:11:44 +02:00
|
|
|
$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) {
|
2013-08-06 16:28:26 +02:00
|
|
|
$this->diffConfigs(
|
2013-08-06 17:11:44 +02:00
|
|
|
$value,new Zend_Config(array()),$editor,$nextParents
|
2013-08-06 16:28:26 +02:00
|
|
|
);
|
2013-08-06 17:11:44 +02:00
|
|
|
if (!isset($section)) {
|
|
|
|
$editor->removeSection($key);
|
|
|
|
}
|
2013-08-06 16:28:26 +02:00
|
|
|
} else {
|
|
|
|
if (is_numeric($key)) {
|
2013-08-06 17:11:44 +02:00
|
|
|
$editor->resetArrayElement($ident,$section);
|
2013-08-06 16:28:26 +02:00
|
|
|
} else {
|
2013-08-06 17:11:44 +02:00
|
|
|
$editor->reset($ident,$section);
|
2013-08-06 16:28:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|