2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2013-07-10 11:40:48 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-07-15 12:26:10 +02:00
|
|
|
/**
|
|
|
|
* This file is part of Icinga 2 Web.
|
2013-08-12 15:02:25 +02:00
|
|
|
*
|
2013-07-15 12:26:10 +02:00
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
2013-08-12 15:02:25 +02:00
|
|
|
*
|
2013-07-15 12:26:10 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2013-08-12 15:02:25 +02:00
|
|
|
*
|
2013-07-15 12:26:10 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2013-08-12 15:02:25 +02:00
|
|
|
*
|
2013-07-15 12:26:10 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2013-08-12 15:02:25 +02:00
|
|
|
*
|
2013-07-15 12:26:10 +02:00
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*/
|
2013-07-10 11:40:48 +02:00
|
|
|
// {{{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
|
|
|
|
|
|
|
use Zend_Config_Ini;
|
|
|
|
|
2013-07-10 11:40:48 +02:00
|
|
|
/**
|
|
|
|
* Global registry of application and module configuration.
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
class Config extends Zend_Config_Ini
|
|
|
|
{
|
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-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
|
|
|
/**
|
2013-07-12 15:37:36 +02:00
|
|
|
* The INI file this configuration has been loaded from
|
2013-07-10 11:40:48 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private $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-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-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
|
|
|
/**
|
2013-07-12 15:37:36 +02:00
|
|
|
* Load configuration from the config file $filename
|
2013-07-10 11:40:48 +02:00
|
|
|
*
|
|
|
|
* @see Zend_Config_Ini::__construct
|
|
|
|
*
|
|
|
|
* @param string $filename
|
2013-07-12 15:37:36 +02:00
|
|
|
* @throws Exception
|
2013-07-10 11:40:48 +02:00
|
|
|
*/
|
|
|
|
public function __construct($filename)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-10 11:40:48 +02:00
|
|
|
if (!@is_readable($filename)) {
|
2013-08-01 09:18:14 +02:00
|
|
|
throw new \Exception('Cannot read config file: ' . $filename);
|
2013-07-10 11:40:48 +02:00
|
|
|
};
|
|
|
|
$this->configFile = $filename;
|
2013-07-12 15:37:36 +02:00
|
|
|
$section = null;
|
|
|
|
$options = array(
|
|
|
|
'allowModifications' => true
|
|
|
|
);
|
|
|
|
parent::__construct($filename, $section, $options);
|
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
|
|
|
*
|
|
|
|
* @param string $configname
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function app($configname = 'config')
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-10 11:40:48 +02:00
|
|
|
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
|
|
|
}
|
2013-07-10 11:40:48 +02:00
|
|
|
return self::$app[$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 a module config instance
|
2013-07-10 11:40:48 +02:00
|
|
|
*
|
|
|
|
* @param string $modulename
|
|
|
|
* @param string $configname
|
|
|
|
* @return Config
|
|
|
|
*/
|
|
|
|
public static function module($modulename, $configname = 'config')
|
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];
|
|
|
|
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;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
public function getConfigFile()
|
|
|
|
{
|
|
|
|
return $this->configFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|