icingaweb2/library/Icinga/Application/Config.php

116 lines
3.0 KiB
PHP
Raw Normal View History

<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Application;
2013-07-12 15:37:36 +02:00
use Icinga\Protocol\Ldap\Exception;
use Zend_Config_Ini;
/**
* Global registry of application and module configuration.
*/
class Config extends Zend_Config_Ini
{
/**
2013-07-12 15:37:36 +02:00
* Configuration directory where ALL (application and module) configuration is located
* @var string
*/
public static $configDir;
/**
2013-07-12 15:37:36 +02:00
* The INI file this configuration has been loaded from
* @var string
*/
protected $configFile;
/**
2013-07-12 15:37:36 +02:00
* Application config instances per file
* @var array
*/
protected static $app = array();
/**
2013-07-12 15:37:36 +02:00
* Module config instances per file
* @var array
*/
protected static $modules = array();
/**
2013-07-12 15:37:36 +02:00
* Load configuration from the config file $filename
*
* @see Zend_Config_Ini::__construct
*
* @param string $filename
2013-07-12 15:37:36 +02:00
* @throws Exception
*/
public function __construct($filename)
{
if (!@is_readable($filename)) {
2013-07-12 15:37:36 +02:00
throw new Exception('Cannot read config file: ' . $filename);
};
$this->configFile = $filename;
2013-07-12 15:37:36 +02:00
$section = null;
$options = array(
'allowModifications' => true
);
parent::__construct($filename, $section, $options);
}
/**
2013-07-12 15:37:36 +02:00
* Retrieve a application config instance
*
* @param string $configname
* @return mixed
*/
public static function app($configname = 'config')
{
if (!isset(self::$app[$configname])) {
$filename = self::$configDir . '/' . $configname . '.ini';
2013-07-12 15:37:36 +02:00
self::$app[$configname] = new Config(realpath($filename));
2013-07-12 12:11:59 +02:00
}
return self::$app[$configname];
}
/**
2013-07-12 15:37:36 +02:00
* Retrieve a module config instance
*
* @param string $modulename
* @param string $configname
* @return Config
*/
public static function module($modulename, $configname = 'config')
{
if (!isset(self::$modules[$modulename])) {
self::$modules[$modulename] = array();
}
$moduleConfigs = self::$modules[$modulename];
if (!isset($moduleConfigs[$configname])) {
$filename = self::$configDir . '/modules/' . $modulename . '/' . $configname . '.ini';
2013-07-12 15:37:36 +02:00
if (file_exists($filename)) {
$moduleConfigs[$configname] = new Config(realpath($filename));
} else {
$moduleConfigs[$configname] = null;
}
}
return $moduleConfigs[$configname];
}
/**
2013-07-12 15:37:36 +02:00
* Retrieve names of accessible sections or properties
*
* @param $name
* @return array
*/
public function keys($name = null)
{
if ($name === null) {
return array_keys($this->toArray());
} elseif ($this->$name === null) {
return array();
} else {
return array_keys($this->$name->toArray());
}
}
}