Icinga\App..\Config: Rename __construct to fromIni and add setConfigPath

Prior to this change it was not possible to create an instance of Config
without passing a config file.
This commit is contained in:
Johannes Meyer 2014-08-29 12:23:48 +02:00
parent 79b0ed64ec
commit e020dd3541

View File

@ -7,7 +7,6 @@ namespace Icinga\Application;
use Zend_Config; use Zend_Config;
use Zend_Config_Ini; use Zend_Config_Ini;
use Icinga\Exception\NotReadableError; use Icinga\Exception\NotReadableError;
use Icinga\Exception\ProgrammingError;
/** /**
* Global registry of application and module configuration. * Global registry of application and module configuration.
@ -22,11 +21,11 @@ class Config extends Zend_Config
public static $configDir; public static $configDir;
/** /**
* The INI file this configuration has been loaded from * The INI file this configuration has been loaded from or should be written to
* *
* @var string * @var string
*/ */
private $configFile; protected $configFile;
/** /**
* Application config instances per file * Application config instances per file
@ -42,27 +41,28 @@ class Config extends Zend_Config
*/ */
protected static $modules = array(); protected static $modules = array();
private $instance;
/** /**
* Load configuration from the config file $filename * Load configuration from the given INI file
*
* @param string $file The file to parse
* *
* @param string $filename The filename to parse
* @throws NotReadableError When the file does not exist or cannot be read * @throws NotReadableError When the file does not exist or cannot be read
*/ */
public function __construct($filename) public static function fromIni($file)
{ {
parent::__construct(array(), true); $config = new static(array(), true);
$filepath = realpath($filename); $filepath = realpath($file);
if ($filepath === false) { if ($filepath === false) {
$this->configFile = $filename; $config->setConfigFile($file);
} elseif (is_readable($filepath)) { } elseif (is_readable($filepath)) {
$this->configFile = $filepath; $config->setConfigFile($filepath);
$this->merge(new Zend_Config_Ini($filepath)); $config->merge(new Zend_Config_Ini($filepath));
} else { } else {
throw new NotReadableError('Cannot read config file "' . $filename . '". Permission denied'); throw new NotReadableError('Cannot read config file "' . $filepath . '". Permission denied');
} }
return $config;
} }
/** /**
@ -77,7 +77,7 @@ class Config extends Zend_Config
public static function app($configname = 'config', $fromDisk = false) public static function app($configname = 'config', $fromDisk = false)
{ {
if (!isset(self::$app[$configname]) || $fromDisk) { if (!isset(self::$app[$configname]) || $fromDisk) {
self::$app[$configname] = new Config(self::resolvePath($configname . '.ini')); self::$app[$configname] = Config::fromIni(self::resolvePath($configname . '.ini'));
} }
return self::$app[$configname]; return self::$app[$configname];
} }
@ -98,7 +98,7 @@ class Config extends Zend_Config
} }
$moduleConfigs = self::$modules[$modulename]; $moduleConfigs = self::$modules[$modulename];
if (!isset($moduleConfigs[$configname]) || $fromDisk) { if (!isset($moduleConfigs[$configname]) || $fromDisk) {
$moduleConfigs[$configname] = new Config( $moduleConfigs[$configname] = Config::fromIni(
self::resolvePath('modules/' . $modulename . '/' . $configname . '.ini') self::resolvePath('modules/' . $modulename . '/' . $configname . '.ini')
); );
} }
@ -123,15 +123,28 @@ class Config extends Zend_Config
} }
/** /**
* Return the application wide config file * Return this config's file path
* *
* @return string * @return string
*/ */
public function getConfigFile() public function getConfigFile()
{ {
return $this->configFile; return $this->configFile;
} }
/**
* Set this config's file path
*
* @param string $filepath The path to the config file
*
* @return self
*/
public function setConfigFile($filepath)
{
$this->configFile = $filepath;
return $this;
}
/** /**
* Prepend configuration base dir if input is relative * Prepend configuration base dir if input is relative
* *