2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga;
|
2013-06-27 13:04:47 +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-07 11:44:37 +02:00
|
|
|
class Backend
|
|
|
|
{
|
|
|
|
protected static $instances = array();
|
2013-06-27 13:04:47 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
protected function __construct() {}
|
|
|
|
|
|
|
|
public static function getInstance($name = null)
|
|
|
|
{
|
|
|
|
if (! array_key_exists($name, self::$instances)) {
|
2013-07-12 15:37:36 +02:00
|
|
|
$backends = IcingaConfig::app('backends');
|
2013-06-07 11:44:37 +02:00
|
|
|
if ($name === null) {
|
2013-06-27 13:04:47 +02:00
|
|
|
$name = AuthManager::getInstance()->getSession()->get('backend');
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
if ($name === null) {
|
2013-07-12 15:37:36 +02:00
|
|
|
$name = array_shift(array_keys($backends->toArray()));
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2013-07-12 15:37:36 +02:00
|
|
|
if (isset($backends->$name)) {
|
|
|
|
$config = $backends->$name;
|
2013-06-07 11:44:37 +02:00
|
|
|
$type = $config->type;
|
|
|
|
$type[0] = strtoupper($type[0]);
|
|
|
|
$class = '\\Icinga\\Backend\\' . $type;
|
|
|
|
self::$instances[$name] = new $class($config);
|
|
|
|
} else {
|
|
|
|
throw new \Exception(sprintf(
|
|
|
|
'Got no config for backend %s',
|
|
|
|
$name
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self::$instances[$name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|