icingaweb2/library/Icinga/Data/ResourceFactory.php

88 lines
2.9 KiB
PHP
Raw Normal View History

<?php
// {{{ICINGA_LICENSE_HEADER}}}
2013-10-23 15:10:33 +02:00
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Data;
use Icinga\Exception\ProgrammingError;
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-22 22:21:03 +02:00
use Icinga\Protocol\Livestatus\Connection as LivestatusConnection;
2013-10-07 16:46:20 +02:00
use Icinga\Protocol\Statusdat\Reader as StatusdatReader;
use Icinga\Protocol\Ldap\Connection as LdapConnection;
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 (!isset(self::$resources)) {
throw new ProgrammingError(
"The ResourceFactory must be initialised by setting a config, before it can be used"
);
}
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 'ldap':
$resource = new LdapConnection($config);
break;
2013-10-07 16:46:20 +02:00
case 'statusdat':
$resource = new StatusdatReader($config);
break;
2013-10-22 22:21:03 +02:00
case 'livestatus':
$resource = new LivestatusConnection($config->socket);
break;
default:
throw new ConfigurationError('Unsupported resource type "' . $config->type . '"');
}
return $resource;
}
}