Add additional functionality and documentation for easier use through developers

Add functions to get information about currently available resources and add
documentation about class usage for developers.
This commit is contained in:
Matthias Jentsch 2013-08-14 16:19:46 +02:00
parent 3ce44b72e1
commit f1040aad60
4 changed files with 120 additions and 10 deletions

View File

@ -21,12 +21,12 @@ bind_pw = "admin"
user_class = "inetOrgPerson"
user_name_attribute = "uid"
[users-mysql]
backend = "db"
target = "user"
resource = "icingaweb-mysql"
[users-pgsql]
backend = "db"
target = "user"
resource = "icingaweb-pgsql"
[users-mysql]
backend = "db"
target = "user"
resource = "icingaweb-mysql"

View File

@ -18,4 +18,60 @@ could be a value like *mysql* or *pgsql*. The other properties like *host*, *pas
*dbname* are the connection information for the resource.
## Factory Implementations
This section contains documentation documentation for the Icinga2-Web developers that want to
use resources defined in the *resources.ini*. Each supported resource type should have an own
factory class, that can be used to comfortably create instances of classes that provide access
to the data of the resources.
### DbAdapterFactory
The DbAdapterFactory can be used to retrieve instances of Zend_Db_Adapter_Abstract for accessing
the data of the SQL database.
Lets assume for the following examples, that we have an *resources.ini* that looks like this:
[resource1]
type = "db"
db = "mysql"
dbname = "resource1"
host = "host"
username = "username1"
password = "password1"
[resource2]
type = "db"
db = "pgsql"
dbname = "resource2"
host = "host"
username = "username2"
password = "password2"
[resource3]
type = "other"
foo = "foo"
bar = "bar"
In the most simple use-case you can create an Adapter by calling the
*getDbAdapter* function. The created adapter will be an instance of Zend_Db_Adapter_
$adapter = DbAdapterFactory::getDbAdapter('resource1');
If you specify a resource that does not exist or has the wrong type,
the factory will throw an ConfigurationException. You can make sure
a resource exists and has the right type, by calling the function *resourceExists*:
if (DbAdapterFactory::resourceExists('resource3')) {
$adapter = DbAdapterFactory::getDbAdapter('resource3');
} else {
// This returned false, because resource3 has a different type than "db"
echo 'resource does not exist, adapter could not be created...'
}
You can retrieve a list of all available resources by calling *getResources*. You will
get an array of all resources that have the type 'db'.

View File

@ -73,6 +73,9 @@ class DbAdapterFactory implements ConfigAwareFactory {
*/
public static function setConfig($config, array $options = null)
{
if (is_array($config)) {
$config = new Zend_Config($config);
}
self::$resources = $config;
if (isset($options['factory'])) {
self::$factoryClass = $options['factory'];
@ -90,6 +93,35 @@ class DbAdapterFactory implements ConfigAwareFactory {
unset(self::$factoryClass);
}
/**
* Get a list of all resources available to this factory
*
* @return array An array containing all resources compatible to this factory
*/
public static function getResources()
{
$resources = self::$resources->toArray();
foreach ($resources as $identifier => $resource) {
if ($resource['type'] !== 'db') {
unset($resources[$identifier]);
}
}
return $resources;
}
/**
* Return if a resource with the given identifier exists
*
* @param $identifier The name of the resource
*
* @return boolean If the resource exists and is compatible
*/
public static function resourceExists($identifier)
{
return isset(self::$resources->{$identifier})
&& (self::$resources->{$identifier}->type === 'db');
}
/**
* Get the resource with the given $identifier
*
@ -99,7 +131,7 @@ class DbAdapterFactory implements ConfigAwareFactory {
{
if (!isset(self::$resources)) {
$msg = 'Creation of resource ' . $identifier . ' not possible, because there is no configuration present.'
. ' Make shure this factory class was initiated correctly during the application bootstrap.';
. ' Make shure this factory class was initialised correctly during the application bootstrap.';
Logger::error($msg);
throw new ProgrammingError($msg);
}

View File

@ -36,6 +36,7 @@ require_once('Zend/Config.php');
require_once('../../library/Icinga/Application/Logger.php');
require_once('library/Icinga/Application/ZendDbMock.php');
require_once('../../library/Icinga/Exception/ConfigurationError.php');
require_once('../../library/Icinga/Exception/ProgrammingError.php');
require_once('../../library/Icinga/Application/ConfigAwareFactory.php');
require_once('../../library/Icinga/Application/DbAdapterFactory.php');
@ -58,7 +59,7 @@ class DbAdapterFactoryTest extends \PHPUnit_Framework_TestCase {
*/
public function setUp()
{
$resources = array(
$this->resources = array(
/*
* PostgreSQL databse
*/
@ -99,7 +100,6 @@ class DbAdapterFactoryTest extends \PHPUnit_Framework_TestCase {
'type' => 'ldap',
),
);
$this->resources = new \Zend_Config($resources);
DbAdapterFactory::setConfig(
$this->resources,
array(
@ -116,12 +116,34 @@ class DbAdapterFactoryTest extends \PHPUnit_Framework_TestCase {
ZendDbMock::getAdapter(),
'The db adapter name must be Pdo_Mysql.');
$this->assertEquals(
$this->getOptions($this->resources->{'resource2'}),
$this->getOptions($this->resources['resource2']),
ZendDbMock::getConfig(),
'The options must match the original config file content'
);
}
public function testResourceExists()
{
$this->assertTrue(DbAdapterFactory::resourceExists('resource2'),
'resourceExists() called with an existing resource should return true');
$this->assertFalse(DbAdapterFactory::resourceExists('not existing'),
'resourceExists() called with an existing resource should return false');
$this->assertFalse(DbAdapterFactory::resourceExists('resource4'),
'resourceExists() called with an incompatible resource should return false');
}
public function testGetResources()
{
$withoutIncompatible = array_merge(array(),$this->resources);
unset($withoutIncompatible['resource4']);
$this->assertEquals(
$withoutIncompatible,
DbAdapterFactory::getResources(),
'getResources should return an array of all existing resources that are compatible');
}
/**
* Test if an exception is thrown, when an invalid database is used.
*
@ -151,7 +173,7 @@ class DbAdapterFactoryTest extends \PHPUnit_Framework_TestCase {
*/
private function getOptions($config)
{
$options = array_merge(array(),$config->toArray());
$options = array_merge(array(),$config);
unset($options['type']);
unset($options['db']);
return $options;