getSection($resourceName); if ($resourceConfig->isEmpty()) { throw new ConfigurationError( 'Cannot load resource config "%s". Resource does not exist', $resourceName ); } return $resourceConfig; } /** * Return the configuration of all existing resources, or get all resources of a given type. * * @return Config The configuration containing all resources */ public static function getResourceConfigs() { self::assertResourcesExist(); return self::$resources; } /** * Check if the existing resources are set. If not, throw an error. * * @throws ConfigurationError */ private static function assertResourcesExist() { if (self::$resources === null) { throw new ConfigurationError( 'Resources not set up. Please contact your Icinga Web administrator' ); } } /** * Create and return a resource based on the given configuration * * @param ConfigObject $config The configuration of the resource to create * * @return Selectable The resource * @throws ConfigurationError In case of an unsupported type or invalid configuration */ public static function createResource(ConfigObject $config) { switch (strtolower($config->type)) { case 'db': $resource = new DbConnection($config); break; case 'ldap': if (empty($config->root_dn)) { throw new ConfigurationError('LDAP root DN missing'); } $resource = new LdapConnection($config); break; case 'livestatus': $resource = new LivestatusConnection($config->socket); break; case 'file': $resource = new FileReader($config); break; case 'ini': $resource = Config::fromIni($config->ini); break; default: throw new ConfigurationError( 'Unsupported resource type "%s"', $config->type ); } return $resource; } /** * Create a resource from name * * @param string $resourceName * @return DbConnection|LdapConnection|LivestatusConnection */ public static function create($resourceName) { return self::createResource(self::getResourceConfig($resourceName)); } }