2013-06-27 10:14:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Monitoring;
|
|
|
|
|
|
|
|
use Icinga\Application\Config;
|
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) {
|
|
|
|
$config = Config::getInstance()->backends;
|
|
|
|
$backends = $config->backends;
|
|
|
|
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]);
|
|
|
|
$class = '\\Icinga\\Monitoring\\Backend\\' . $type;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|