2012-08-04 10:40:48 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This file is part of MySQLDumper released under the GNU/GPL 2 license
|
|
|
|
* http://www.mysqldumper.net
|
|
|
|
*
|
2012-08-08 22:36:28 +00:00
|
|
|
* @package MySQLDumper
|
|
|
|
* @subpackage Config
|
|
|
|
* @version SVN: $Rev$
|
|
|
|
* @author $Author$
|
2012-08-04 10:40:48 +00:00
|
|
|
*/
|
|
|
|
/**
|
2012-08-08 22:36:28 +00:00
|
|
|
* Class to handle the configuration.
|
2012-08-04 10:40:48 +00:00
|
|
|
*
|
2012-08-08 22:36:28 +00:00
|
|
|
* @package MySQLDumper
|
|
|
|
* @subpackage Config
|
2012-08-04 10:40:48 +00:00
|
|
|
*/
|
|
|
|
class Msd_Config
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The configuration itself.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_config = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of the IO-Handler.
|
|
|
|
*
|
|
|
|
* @var Msd_Config_IoHandler_Interface
|
|
|
|
*/
|
|
|
|
private $_ioHandler = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Status for the automatic configuration saving on class destruction.
|
|
|
|
* It's disabled by default.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $_autosave = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class constructor
|
|
|
|
*
|
|
|
|
* @param Msd_Config_IoHandler_Interface|string $ioHandler Instance or name of the IO-Handler.
|
|
|
|
* @param array $handlerOptions Options for the IO-Handler.
|
|
|
|
*
|
|
|
|
* @throws Msd_Config_Exception
|
2012-08-08 22:36:28 +00:00
|
|
|
*
|
|
|
|
* @return Msd_Config
|
2012-08-04 10:40:48 +00:00
|
|
|
*/
|
|
|
|
public function __construct($ioHandler, $handlerOptions = array())
|
|
|
|
{
|
|
|
|
if (is_string($ioHandler)) {
|
|
|
|
$pluginLoader = new Zend_Loader_PluginLoader(
|
|
|
|
array(
|
2012-08-04 17:09:48 +00:00
|
|
|
'Msd_Config_IoHandler_' => APPLICATION_PATH . '/../library/Msd/Config/IoHandler/',
|
2012-08-04 10:40:48 +00:00
|
|
|
'Module_Config_IoHandler_' => APPLICATION_PATH . '/../modules/library/Config/IoHandler/',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$className = $pluginLoader->load($ioHandler);
|
|
|
|
$ioHandler = new $className($handlerOptions);
|
|
|
|
}
|
|
|
|
if (!$ioHandler instanceof Msd_Config_IoHandler_Interface) {
|
|
|
|
throw new Msd_Config_Exception(
|
|
|
|
"Invalid IO-Handler specified; The IO-Handler must implement Msd_Config_IoHandler_Interface"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$this->_ioHandler = $ioHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the configuration from the IO-Handler.
|
|
|
|
* The filename is used for static storage.
|
|
|
|
*
|
|
|
|
* @param string $configFilename Name of file on the static storage.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function load($configFilename)
|
|
|
|
{
|
|
|
|
$this->_config = $this->_ioHandler->load($configFilename);
|
2012-08-04 17:09:48 +00:00
|
|
|
$this->_setPaths();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add paths to config
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function _setPaths()
|
|
|
|
{
|
|
|
|
$workRoot = realpath(APPLICATION_PATH . '/..') . '/work/';
|
|
|
|
$directories = array(
|
|
|
|
'work' => $workRoot,
|
|
|
|
'log' => $workRoot . 'log',
|
|
|
|
'backup' => $workRoot . 'backup',
|
|
|
|
'config' => $workRoot . 'config',
|
|
|
|
'iconPath' => 'css/' . $this->getParam('interface.theme', 'msd') . '/icons'
|
|
|
|
);
|
|
|
|
$this->setParam('paths', $directories);
|
2012-08-04 10:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the configuration for the next request.
|
|
|
|
* The filename is used for static storage.
|
|
|
|
*
|
2012-08-22 16:43:15 +00:00
|
|
|
* @param string filename The file name of the fiel to save (without path)
|
|
|
|
*
|
2012-08-04 10:40:48 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2012-08-22 16:43:15 +00:00
|
|
|
public function save($filename = null)
|
2012-08-04 10:40:48 +00:00
|
|
|
{
|
2012-08-22 16:43:15 +00:00
|
|
|
if ($filename !== null) {
|
|
|
|
$this->_ioHandler->setConfigFilename($filename);
|
2012-08-21 20:22:56 +00:00
|
|
|
}
|
|
|
|
|
2012-08-04 10:40:48 +00:00
|
|
|
return $this->_ioHandler->save($this->_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the value of a configuration parameter.
|
|
|
|
*
|
2012-08-04 17:09:48 +00:00
|
|
|
* @param string $paramName Name of the configuration parameter. May be prefixed with section.
|
2012-08-04 10:40:48 +00:00
|
|
|
* @param mixed $defaultValue Default value to return, if param isn't set.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getParam($paramName, $defaultValue = null)
|
|
|
|
{
|
2012-08-09 19:53:40 +00:00
|
|
|
if (isset($this->_config[$paramName])) {
|
|
|
|
return $this->_config[$paramName];
|
|
|
|
}
|
|
|
|
|
2012-08-04 17:09:48 +00:00
|
|
|
// check for section e.g. interface.theme
|
|
|
|
if (strpos($paramName, '.') !== false) {
|
|
|
|
list($section, $paramName) = explode('.', $paramName);
|
|
|
|
if (isset($this->_config[$section][$paramName])) {
|
|
|
|
return $this->_config[$section][$paramName];
|
|
|
|
} else {
|
|
|
|
return $defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-04 10:40:48 +00:00
|
|
|
return $defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a configuration parameter.
|
|
|
|
* If auto-save is enabled the configuration is also saved.
|
|
|
|
*
|
|
|
|
* @param $paramName
|
|
|
|
* @param $paramValue
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setParam($paramName, $paramValue)
|
|
|
|
{
|
2012-08-09 19:53:40 +00:00
|
|
|
if (strpos($paramName, '.') !== false) {
|
2012-08-04 17:09:48 +00:00
|
|
|
list($section, $paramName) = explode('.', $paramName);
|
|
|
|
$this->_config[$section][$paramName] = $paramValue;
|
|
|
|
} else {
|
|
|
|
$this->_config[$paramName] = $paramValue;
|
|
|
|
}
|
|
|
|
|
2012-08-04 10:40:48 +00:00
|
|
|
if ($this->_autosave) {
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class destructor.
|
|
|
|
* If auto-save is enabled the configuration will be saved.
|
2012-08-08 22:36:28 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-08-04 10:40:48 +00:00
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if ($this->_autosave) {
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the whole configuration.
|
|
|
|
* If auto-save is enabled the configuration is also saved.
|
|
|
|
*
|
|
|
|
* @param array $config New configuration.
|
2012-08-08 22:36:28 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-08-04 10:40:48 +00:00
|
|
|
*/
|
|
|
|
public function setConfig($config)
|
|
|
|
{
|
2012-08-08 22:36:28 +00:00
|
|
|
$this->_config = (array) $config;
|
2012-08-04 10:40:48 +00:00
|
|
|
if ($this->_autosave) {
|
|
|
|
$this->save();
|
|
|
|
}
|
2012-08-08 22:36:28 +00:00
|
|
|
$this->_setPaths();
|
2012-08-04 10:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the whole configuration.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getConfig()
|
|
|
|
{
|
|
|
|
return $this->_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables automatic configuration saving.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function enableAutosave()
|
|
|
|
{
|
|
|
|
$this->_autosave = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables automatic configuration saving.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function disableAutosave()
|
|
|
|
{
|
|
|
|
$this->_autosave = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the autosave status.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isAutosaveActive()
|
|
|
|
{
|
|
|
|
return $this->_autosave;
|
|
|
|
}
|
2012-08-04 17:09:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the title from a configuration file without aplying it.
|
|
|
|
*
|
|
|
|
* @param string $fileName The file name of the configuration
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getConfigTitle($fileName)
|
|
|
|
{
|
|
|
|
$configData = parse_ini_file($this->getParam('paths.config') . '/' . $fileName . '.ini', true);
|
|
|
|
return $configData['general']['title'];
|
|
|
|
}
|
2012-08-04 10:40:48 +00:00
|
|
|
}
|