2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2013-07-10 11:40:48 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-08-12 15:02:25 +02:00
|
|
|
namespace Icinga\Application;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2014-03-11 15:43:41 +01:00
|
|
|
use Zend_Config;
|
2014-02-20 13:53:28 +01:00
|
|
|
use Zend_Config_Ini;
|
|
|
|
use Icinga\Exception\NotReadableError;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-07-10 11:40:48 +02:00
|
|
|
/**
|
|
|
|
* Global registry of application and module configuration.
|
|
|
|
*/
|
2014-03-11 15:43:41 +01:00
|
|
|
class Config extends Zend_Config
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-10 11:40:48 +02:00
|
|
|
/**
|
2013-07-12 15:37:36 +02:00
|
|
|
* Configuration directory where ALL (application and module) configuration is located
|
2013-08-26 16:56:23 +02:00
|
|
|
*
|
2013-07-10 11:40:48 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $configDir;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-07-10 11:40:48 +02:00
|
|
|
/**
|
2014-08-29 12:23:48 +02:00
|
|
|
* The INI file this configuration has been loaded from or should be written to
|
2013-08-26 16:56:23 +02:00
|
|
|
*
|
2013-07-10 11:40:48 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2014-08-29 12:23:48 +02:00
|
|
|
protected $configFile;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-07-10 11:40:48 +02:00
|
|
|
/**
|
2013-07-12 15:37:36 +02:00
|
|
|
* Application config instances per file
|
2013-08-26 16:56:23 +02:00
|
|
|
*
|
2013-07-10 11:40:48 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $app = array();
|
|
|
|
|
|
|
|
/**
|
2013-07-12 15:37:36 +02:00
|
|
|
* Module config instances per file
|
2013-08-26 16:56:23 +02:00
|
|
|
*
|
2013-07-10 11:40:48 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $modules = array();
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-07-10 11:40:48 +02:00
|
|
|
/**
|
2014-08-29 12:23:48 +02:00
|
|
|
* Load configuration from the given INI file
|
|
|
|
*
|
|
|
|
* @param string $file The file to parse
|
2013-07-10 11:40:48 +02:00
|
|
|
*
|
2014-02-20 13:53:28 +01:00
|
|
|
* @throws NotReadableError When the file does not exist or cannot be read
|
2013-07-10 11:40:48 +02:00
|
|
|
*/
|
2014-08-29 12:23:48 +02:00
|
|
|
public static function fromIni($file)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2014-08-29 12:23:48 +02:00
|
|
|
$config = new static(array(), true);
|
|
|
|
$filepath = realpath($file);
|
|
|
|
|
2014-03-11 15:43:41 +01:00
|
|
|
if ($filepath === false) {
|
2014-08-29 12:23:48 +02:00
|
|
|
$config->setConfigFile($file);
|
2014-06-02 14:41:17 +02:00
|
|
|
} elseif (is_readable($filepath)) {
|
2014-08-29 12:23:48 +02:00
|
|
|
$config->setConfigFile($filepath);
|
|
|
|
$config->merge(new Zend_Config_Ini($filepath));
|
2014-03-11 15:43:41 +01:00
|
|
|
} else {
|
2014-08-29 16:05:56 +02:00
|
|
|
throw new NotReadableError('Cannot read config file "%s". Permission denied', $filepath);
|
2014-03-11 15:43:41 +01:00
|
|
|
}
|
2014-08-29 12:23:48 +02:00
|
|
|
|
|
|
|
return $config;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 11:40:48 +02:00
|
|
|
/**
|
2013-07-12 15:37:36 +02:00
|
|
|
* Retrieve a application config instance
|
2013-07-10 11:40:48 +02:00
|
|
|
*
|
2013-08-26 16:56:23 +02:00
|
|
|
* @param string $configname The configuration name (without ini suffix) to read and return
|
|
|
|
* @param bool $fromDisk When set true, the configuration will be read from the disk, even
|
|
|
|
* if it already has been read
|
|
|
|
*
|
|
|
|
* @return Config The configuration object that has been requested
|
2013-07-10 11:40:48 +02:00
|
|
|
*/
|
2013-08-26 16:56:23 +02:00
|
|
|
public static function app($configname = 'config', $fromDisk = false)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-08-26 16:56:23 +02:00
|
|
|
if (!isset(self::$app[$configname]) || $fromDisk) {
|
2014-08-29 12:23:48 +02:00
|
|
|
self::$app[$configname] = Config::fromIni(self::resolvePath($configname . '.ini'));
|
2013-07-12 12:11:59 +02:00
|
|
|
}
|
2013-07-10 11:40:48 +02:00
|
|
|
return self::$app[$configname];
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2014-09-01 15:53:32 +02:00
|
|
|
/**
|
|
|
|
* Set module config
|
|
|
|
*
|
|
|
|
* @param string $moduleName
|
|
|
|
* @param string $configName
|
|
|
|
* @param Zend_Config $config
|
|
|
|
*/
|
|
|
|
public static function setModuleConfig($moduleName, $configName, Zend_Config $config)
|
|
|
|
{
|
|
|
|
self::$modules[$moduleName][$configName] = $config;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:40:48 +02:00
|
|
|
/**
|
2013-07-12 15:37:36 +02:00
|
|
|
* Retrieve a module config instance
|
2013-07-10 11:40:48 +02:00
|
|
|
*
|
2013-08-28 13:31:18 +02:00
|
|
|
* @param string $modulename The name of the module to look for configurations
|
|
|
|
* @param string $configname The configuration name (without ini suffix) to read and return
|
|
|
|
* @param string $fromDisk Whether to read the configuration from disk
|
|
|
|
*
|
|
|
|
* @return Config The configuration object that has been requested
|
2013-07-10 11:40:48 +02:00
|
|
|
*/
|
2013-08-28 13:31:18 +02:00
|
|
|
public static function module($modulename, $configname = 'config', $fromDisk = false)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-10 11:40:48 +02:00
|
|
|
if (!isset(self::$modules[$modulename])) {
|
|
|
|
self::$modules[$modulename] = array();
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2013-07-10 11:40:48 +02:00
|
|
|
$moduleConfigs = self::$modules[$modulename];
|
2013-08-28 13:31:18 +02:00
|
|
|
if (!isset($moduleConfigs[$configname]) || $fromDisk) {
|
2014-08-29 12:23:48 +02:00
|
|
|
$moduleConfigs[$configname] = Config::fromIni(
|
2014-06-02 14:41:17 +02:00
|
|
|
self::resolvePath('modules/' . $modulename . '/' . $configname . '.ini')
|
2014-03-11 15:43:41 +01:00
|
|
|
);
|
2013-07-10 11:40:48 +02:00
|
|
|
}
|
|
|
|
return $moduleConfigs[$configname];
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 11:40:48 +02:00
|
|
|
/**
|
2013-07-12 15:37:36 +02:00
|
|
|
* Retrieve names of accessible sections or properties
|
2013-07-10 11:40:48 +02:00
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function keys($name = null)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-10 11:40:48 +02:00
|
|
|
if ($name === null) {
|
|
|
|
return array_keys($this->toArray());
|
|
|
|
} elseif ($this->$name === null) {
|
|
|
|
return array();
|
|
|
|
} else {
|
|
|
|
return array_keys($this->$name->toArray());
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
|
2013-08-27 17:17:09 +02:00
|
|
|
/**
|
2014-08-29 12:23:48 +02:00
|
|
|
* Return this config's file path
|
2013-08-27 17:17:09 +02:00
|
|
|
*
|
2014-08-29 12:23:48 +02:00
|
|
|
* @return string
|
2013-08-27 17:17:09 +02:00
|
|
|
*/
|
2013-08-27 14:37:22 +02:00
|
|
|
public function getConfigFile()
|
2013-08-06 11:53:42 +02:00
|
|
|
{
|
|
|
|
return $this->configFile;
|
|
|
|
}
|
2013-08-27 17:17:09 +02:00
|
|
|
|
2014-08-29 12:23:48 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2013-08-27 17:17:09 +02:00
|
|
|
/**
|
2014-06-02 14:41:17 +02:00
|
|
|
* Prepend configuration base dir if input is relative
|
2013-08-27 17:17:09 +02:00
|
|
|
*
|
2014-06-02 14:41:17 +02:00
|
|
|
* @param string $path Input path
|
|
|
|
* @return string Absolute path
|
2013-08-27 17:17:09 +02:00
|
|
|
*/
|
|
|
|
public static function resolvePath($path)
|
|
|
|
{
|
2014-06-25 11:43:39 +02:00
|
|
|
return self::$configDir . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR);
|
2013-08-27 17:17:09 +02:00
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|