2013-06-27 10:14:41 +02:00
|
|
|
<?php
|
|
|
|
|
2013-07-15 12:16:14 +02:00
|
|
|
namespace Monitoring;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2013-07-12 15:37:36 +02:00
|
|
|
use Icinga\Application\Config as IcingaConfig;
|
2013-06-27 13:04:47 +02:00
|
|
|
use Icinga\Authentication\Manager as AuthManager;
|
2013-06-27 10:14:41 +02:00
|
|
|
use Exception;
|
|
|
|
|
|
|
|
class Backend
|
|
|
|
{
|
|
|
|
protected static $instances = array();
|
|
|
|
protected static $backendConfigs;
|
2013-06-27 13:04:47 +02:00
|
|
|
|
2013-06-27 10:14:41 +02:00
|
|
|
final protected function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function exists($name)
|
|
|
|
{
|
|
|
|
$configs = self::getBackendConfigs();
|
|
|
|
return array_key_exists($name, $configs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDefaultName()
|
|
|
|
{
|
|
|
|
$configs = self::getBackendConfigs();
|
|
|
|
if (empty($configs)) {
|
|
|
|
throw new Exception(
|
|
|
|
'Cannot get default backend as no backend has been configured'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
reset($configs);
|
|
|
|
return key($configs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBackendConfigs()
|
|
|
|
{
|
|
|
|
if (self::$backendConfigs === null) {
|
2013-07-12 15:37:36 +02:00
|
|
|
$backends = IcingaConfig::app('backends');
|
2013-06-27 10:14:41 +02:00
|
|
|
foreach ($backends as $name => $config) {
|
|
|
|
// TODO: Check if access to this backend is allowed
|
|
|
|
self::$backendConfigs[$name] = $config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self::$backendConfigs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBackend($name = null)
|
|
|
|
{
|
|
|
|
if (! array_key_exists($name, self::$instances)) {
|
|
|
|
if ($name === null) {
|
|
|
|
$name = self::getDefaultName();
|
|
|
|
} else {
|
|
|
|
if (! self::exists($name)) {
|
|
|
|
throw new Exception(sprintf(
|
|
|
|
'There is no such backend: "%s"',
|
|
|
|
$name
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = self::$backendConfigs[$name];
|
|
|
|
$type = $config->type;
|
|
|
|
$type[0] = strtoupper($type[0]);
|
2013-07-15 12:16:14 +02:00
|
|
|
$class = '\\Monitoring\\Backend\\' . $type;
|
2013-06-27 10:14:41 +02:00
|
|
|
self::$instances[$name] = new $class($config);
|
|
|
|
}
|
|
|
|
return self::$instances[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getInstance($name = null)
|
|
|
|
{
|
|
|
|
if (array_key_exists($name, self::$instances)) {
|
|
|
|
return self::$instances[$name];
|
|
|
|
} else {
|
|
|
|
if ($name === null) {
|
|
|
|
// TODO: Remove this, will be chosen by Environment
|
2013-06-27 13:04:47 +02:00
|
|
|
$name = AuthManager::getInstance()->getSession()->get('backend');
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
return self::getBackend($name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|