Commit 4bacd184 authored by Ad Schellevis's avatar Ad Schellevis

add config backup/restore methods

parent 6f022153
......@@ -230,9 +230,13 @@ class Config extends Singleton
throw new ConfigException('empty file') ;
}
set_error_handler(function(){throw new ConfigException("invalid config xml") ;}) ;
$this->configxml = new \DOMDocument('1.0');
$this->configxml->loadXML($xml);
$this->simplexml = simplexml_import_dom($this->configxml);
restore_error_handler();
$this->isValid = true;
}
......@@ -267,6 +271,57 @@ class Config extends Singleton
}
/**
* return list of config backups
* @return array list of backups
*/
public function getBackups()
{
$target_dir = dirname($this->config_file)."/backup/";
if (file_exists($target_dir)) {
$backups = glob($target_dir."config*.xml");
rsort($backups);
return $backups;
}
return array();
}
/**
* restore and load backup config
* @param $filename
* @return bool restored, valid config loaded
* @throws ConfigException no config loaded
*/
public function restoreBackup($filename)
{
if ($this->isValid) {
// if current config is valid,
$configxml = $this->configxml;
$simplexml = $this->simplexml;
try {
// try to restore config
copy($filename, $this->config_file) ;
$this->load();
return true;
} catch (ConfigException $e) {
// copy / load failed, restore previous version
$this->configxml = $configxml;
$this->simplexml = $simplexml;
$this->isValid = true;
$this->save(null, true);
}
} else {
// we don't have a valid config loaded, just copy and load the requested one
copy($filename, $this->config_file) ;
$this->load();
return true;
}
return false;
}
/**
* save config to filesystem
* @param string|null $message log message
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment