Skip to content

Commit

Permalink
Icinga\App..\Config: Rename __construct to fromIni and add setConfigPath
Browse files Browse the repository at this point in the history
Prior to this change it was not possible to create an instance of Config
without passing a config file.
  • Loading branch information
Johannes Meyer committed Aug 29, 2014
1 parent 79b0ed6 commit e020dd3
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions library/Icinga/Application/Config.php
Expand Up @@ -7,7 +7,6 @@
use Zend_Config;
use Zend_Config_Ini;
use Icinga\Exception\NotReadableError;
use Icinga\Exception\ProgrammingError;

/**
* Global registry of application and module configuration.
Expand All @@ -22,11 +21,11 @@ class Config extends Zend_Config
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
*/
private $configFile;
protected $configFile;

/**
* Application config instances per file
Expand All @@ -42,27 +41,28 @@ class Config extends Zend_Config
*/
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
*/
public function __construct($filename)
public static function fromIni($file)
{
parent::__construct(array(), true);
$filepath = realpath($filename);
$config = new static(array(), true);
$filepath = realpath($file);

if ($filepath === false) {
$this->configFile = $filename;
$config->setConfigFile($file);
} elseif (is_readable($filepath)) {
$this->configFile = $filepath;
$this->merge(new Zend_Config_Ini($filepath));
$config->setConfigFile($filepath);
$config->merge(new Zend_Config_Ini($filepath));
} else {
throw new NotReadableError('Cannot read config file "' . $filename . '". Permission denied');
throw new NotReadableError('Cannot read config file "' . $filepath . '". Permission denied');
}

return $config;
}

/**
Expand All @@ -77,7 +77,7 @@ public function __construct($filename)
public static function app($configname = 'config', $fromDisk = false)
{
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];
}
Expand All @@ -98,7 +98,7 @@ public static function module($modulename, $configname = 'config', $fromDisk = f
}
$moduleConfigs = self::$modules[$modulename];
if (!isset($moduleConfigs[$configname]) || $fromDisk) {
$moduleConfigs[$configname] = new Config(
$moduleConfigs[$configname] = Config::fromIni(
self::resolvePath('modules/' . $modulename . '/' . $configname . '.ini')
);
}
Expand All @@ -123,15 +123,28 @@ public function keys($name = null)
}

/**
* Return the application wide config file
* Return this config's file path
*
* @return string
* @return string
*/
public function getConfigFile()
{
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
*
Expand Down

0 comments on commit e020dd3

Please sign in to comment.