mirror of
				https://github.com/Icinga/icingaweb2.git
				synced 2025-10-31 03:14:31 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			85 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			85 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace Icinga\Monitoring;
 | |
| 
 | |
| use Icinga\Application\Config;
 | |
| use Icinga\Web\Session;
 | |
| use Exception;
 | |
| 
 | |
| class Backend
 | |
| {
 | |
|     protected static $instances = array();
 | |
|     protected static $backendConfigs;
 | |
|     
 | |
|     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
 | |
|                 $name = Session::getInstance()->backend;
 | |
|             }
 | |
|             return self::getBackend($name);
 | |
|         }
 | |
|     }
 | |
| }
 |