* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team * */ // {{{ICINGA_LICENSE_HEADER}}} namespace Icinga\Data; use Icinga\Exception\ProgrammingError; use Zend_Config; use Icinga\Util\ConfigAwareFactory; use Icinga\Exception\ConfigurationError; use Icinga\Data\Db\Connection as DbConnection; use Icinga\Protocol\Livestatus\Connection as LivestatusConnection; 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; } 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) { throw new ConfigurationError('Resource "' . $resourceName . '" couldn\'t be retrieved'); } return $resourceConfig; } public static function createResource(Zend_Config $config) { switch (strtolower($config->type)) { case 'db': $resource = new DbConnection($config); break; case 'ldap': $resource = new LdapConnection($config); break; case 'statusdat': $resource = new StatusdatReader($config); break; case 'livestatus': $resource = new LivestatusConnection($config->socket); break; default: throw new ConfigurationError('Unsupported resource type "' . $config->type . '"'); } return $resource; } }