2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Application;
|
|
|
|
|
2013-07-12 12:11:59 +02:00
|
|
|
use Icinga\Application\Modules\Module;
|
2013-06-07 11:44:37 +02:00
|
|
|
use Zend_Config_Ini;
|
|
|
|
use Zend_Config;
|
|
|
|
|
|
|
|
class Config extends Zend_Config_Ini
|
|
|
|
{
|
|
|
|
protected $data;
|
|
|
|
protected static $instance;
|
|
|
|
protected $configDir;
|
|
|
|
|
|
|
|
public function listAll($what)
|
|
|
|
{
|
|
|
|
if ($this->$what === null) {
|
|
|
|
return array();
|
|
|
|
} else {
|
|
|
|
return array_keys($this->$what->toArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfigDir()
|
|
|
|
{
|
|
|
|
return $this->configDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct($filename, $section = null, $options = false)
|
|
|
|
{
|
|
|
|
$options['allowModifications'] = true;
|
|
|
|
$this->configDir = dirname($filename);
|
|
|
|
return parent::__construct($filename, $section, $options);
|
|
|
|
}
|
|
|
|
|
2013-07-12 12:11:59 +02:00
|
|
|
public static function module($name, $file = null)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 12:11:59 +02:00
|
|
|
if ($file === null) {
|
|
|
|
$file = $name . '.ini'; // TODO: default should be module/config.ini
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2013-07-12 12:11:59 +02:00
|
|
|
$filename = Module::get($name)->getConfigDir() . '/' . $file;
|
|
|
|
if (file_exists($filename)) {
|
|
|
|
$config = new Config($filename);
|
|
|
|
// Compat: $config->$module->$whatever
|
|
|
|
self::getInstance()->$name = $config;
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
return null;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
$res = parent::__get($key);
|
|
|
|
if ($res === null) {
|
|
|
|
$app = Icinga::app();
|
|
|
|
if ($app->hasModule($key)) {
|
|
|
|
$filename = $app->getModule($key)->getConfigDir() . "/$key.ini";
|
|
|
|
} else {
|
|
|
|
$filename = $this->configDir . '/' . $key . '.ini';
|
|
|
|
}
|
|
|
|
if (file_exists($filename)) {
|
|
|
|
$res = $this->$key = new Config($filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getInstance($configFile = null)
|
|
|
|
{
|
|
|
|
if (self::$instance === null) {
|
|
|
|
if ($configFile === null) {
|
|
|
|
$configFile = dirname(dirname(dirname(dirname(__FILE__))))
|
|
|
|
. '/config/icinga.ini';
|
|
|
|
}
|
|
|
|
self::$instance = new Config($configFile);
|
|
|
|
}
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
}
|