icingaweb2/library/Icinga/Data/ResourceFactory.php

49 lines
1.3 KiB
PHP
Raw Normal View History

<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Data;
2013-10-07 16:46:20 +02:00
use Zend_Config;
use Icinga\Util\ConfigAwareFactory;
use Icinga\Exception\ConfigurationError;
use Icinga\Data\Db\Connection as DbConnection;
2013-10-07 16:46:20 +02:00
use Icinga\Protocol\Statusdat\Reader as StatusdatReader;
class ResourceFactory implements ConfigAwareFactory
{
/**
* @var Zend_Config
*/
private static $resources;
public static function setConfig($config)
{
self::$resources = $config;
}
2013-10-07 16:46:20 +02:00
public static function getResourceConfig($resourceName)
{
if (($resourceConfig = self::$resources->get($resourceName)) === null) {
2013-10-19 20:09:17 +02:00
throw new ConfigurationError('Resource "' . $resourceName . '" couldn\'t be retrieved');
}
2013-10-07 16:46:20 +02:00
return $resourceConfig;
}
public static function createResource(Zend_Config $config)
{
switch (strtolower($config->type)) {
case 'db':
2013-10-07 16:46:20 +02:00
$resource = new DbConnection($config);
break;
case 'statusdat':
$resource = new StatusdatReader($config);
break;
default:
throw new ConfigurationError('Unsupported resource type "' . $config->type . '"');
}
return $resource;
}
}