Merge branch 'bugfix/ConfigAwareFactory-5514'

fixes #5514
This commit is contained in:
Marius Hein 2014-08-28 12:15:31 +02:00
commit dea7caa633
9 changed files with 99 additions and 32 deletions

View File

@ -45,8 +45,7 @@ class ListController extends Controller
file_exists($config_ini['logging']['target'])
)
) {
$config = ResourceFactory::getResourceConfig('logfile');
$resource = ResourceFactory::createResource($config);
$resource = ResourceFactory::create('logfile');
$this->view->logData = $resource->select()->order('DESC')->paginate();
} else {
$this->view->logData = null;

View File

@ -111,10 +111,9 @@ class DbBackendForm extends BaseBackendForm
public function isValidAuthenticationBackend()
{
try {
$testConnection = ResourceFactory::createResource(ResourceFactory::getResourceConfig(
$dbUserBackend = new DbUserBackend(ResourceFactory::create(
$this->getValue('backend_' . $this->filterName($this->getBackendName()) . '_resource')
));
$dbUserBackend = new DbUserBackend($testConnection);
if ($dbUserBackend->count() < 1) {
$this->addErrorMessage(t("No users found under the specified database backend"));
return false;

View File

@ -5,6 +5,7 @@
namespace Icinga\Form\Config\Authentication;
use Exception;
use Icinga\Application\Platform;
use Zend_Config;
use Icinga\Web\Form;
use Icinga\Data\ResourceFactory;
@ -135,7 +136,7 @@ class LdapBackendForm extends BaseBackendForm
*/
public function isValidAuthenticationBackend()
{
if (! ResourceFactory::ldapAvailable()) {
if (! Platform::extensionLoaded('ldap')) {
/*
* It should be possible to run icingaweb without the php ldap extension, when
* no ldap backends are needed. When the user tries to create an ldap backend
@ -148,7 +149,7 @@ class LdapBackendForm extends BaseBackendForm
$cfg = $this->getConfig();
$backendName = 'backend_' . $this->filterName($this->getBackendName()) . '_name';
$backendConfig = new Zend_Config($cfg[$this->getValue($backendName)]);
$backend = ResourceFactory::createResource(ResourceFactory::getResourceConfig($backendConfig->resource));
$backend = ResourceFactory::create($backendConfig->resource);
$testConn = new LdapUserBackend(
$backend,
$backendConfig->user_class,

View File

@ -5,6 +5,7 @@
namespace Icinga\Form\Config;
use Exception;
use Icinga\Application\Platform;
use Zend_Config;
use Zend_Form_Element_Checkbox;
use Icinga\Web\Form;
@ -405,14 +406,14 @@ class ResourceForm extends Form
* in case they aren't actually used. When the user tries to create a resource that depends on an
* uninstalled extension, an error should be displayed.
*/
if ($config->db === 'mysql' && !ResourceFactory::mysqlAvailable()) {
if ($config->db === 'mysql' && ! Platform::extensionLoaded('mysql')) {
$this->addErrorMessage(
t('You need to install the php extension "mysql" and the ' .
'Zend_Pdo_Mysql classes to use MySQL database resources.')
);
return false;
}
if ($config->db === 'pgsql' && !ResourceFactory::pgsqlAvailable()) {
if ($config->db === 'pgsql' && ! Platform::extensionLoaded('pgsql')) {
$this->addErrorMessage(
t('You need to install the php extension "pgsql" and the ' .
'Zend_Pdo_Pgsql classes to use PostgreSQL database resources.')

View File

@ -4,22 +4,57 @@
namespace Icinga\Application;
/**
* Platform tests for icingaweb
*/
class Platform
{
/**
* Domain name
*
* @var string
*/
protected static $domain;
/**
* Host name
*
* @var string
*/
protected static $hostname;
/**
* Fully qualified domain name
*
* @var string
*/
protected static $fqdn;
/**
* Test of windows
*
* @return bool
*/
public static function isWindows()
{
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}
/**
* Test of linux
*
* @return bool
*/
public static function isLinux()
{
return strtoupper(substr(PHP_OS, 0, 5)) === 'LINUX';
}
/**
* Test of CLI environment
*
* @return bool
*/
public static function isCli()
{
if (PHP_SAPI == 'cli') {
@ -31,6 +66,11 @@ class Platform
return false;
}
/**
* Get the hostname
*
* @return string
*/
public static function getHostname()
{
if (self::$hostname === null) {
@ -39,6 +79,11 @@ class Platform
return self::$hostname;
}
/**
* Get the domain name
*
* @return string
*/
public static function getDomain()
{
if (self::$domain === null) {
@ -47,6 +92,11 @@ class Platform
return self::$domain;
}
/**
* Get the fully qualified domain name
*
* @return string
*/
public static function getFqdn()
{
if (self::$fqdn === null) {
@ -55,6 +105,9 @@ class Platform
return self::$fqdn;
}
/**
* Initialize domain and host strings
*/
protected static function discoverHostname()
{
self::$hostname = gethostname();
@ -66,4 +119,16 @@ class Platform
self::$domain = array_shift(preg_split('~\.~', self::$hostname, 2));
}
}
/**
* Test for php extension
*
* @param string $extensionName E.g. mysql, ldap
*
* @return bool
*/
public static function extensionLoaded($extensionName)
{
return extension_loaded($extensionName);
}
}

View File

@ -14,13 +14,23 @@ use Icinga\Protocol\Statusdat\Reader as StatusdatReader;
use Icinga\Protocol\Ldap\Connection as LdapConnection;
use Icinga\Protocol\File\Reader as FileReader;
/**
* Create resources from names or resource configuration
*/
class ResourceFactory implements ConfigAwareFactory
{
/**
* Resource configuration
*
* @var Zend_Config
*/
private static $resources;
/**
* Set resource configurations
*
* @param Zend_Config $config
*/
public static function setConfig($config)
{
self::$resources = $config;
@ -32,7 +42,8 @@ class ResourceFactory implements ConfigAwareFactory
* @param $resourceName String The resource's name
*
* @return Zend_Config The configuration of the resource
* @throws \Icinga\Exception\ConfigurationError
*
* @throws ConfigurationError
*/
public static function getResourceConfig($resourceName)
{
@ -72,7 +83,7 @@ class ResourceFactory implements ConfigAwareFactory
/**
* Check if the existing resources are set. If not, throw an error.
*
* @throws \Icinga\Exception\ProgrammingError
* @throws ProgrammingError
*/
private static function assertResourcesExist()
{
@ -93,7 +104,7 @@ class ResourceFactory implements ConfigAwareFactory
*
* @return DbConnection|LdapConnection|LivestatusConnection|StatusdatReader An objects that can be used to access
* the given resource. The returned class depends on the configuration property 'type'.
* @throws \Icinga\Exception\ConfigurationError When an unsupported type is given
* @throws ConfigurationError When an unsupported type is given
*/
public static function createResource(Zend_Config $config)
{
@ -122,18 +133,14 @@ class ResourceFactory implements ConfigAwareFactory
return $resource;
}
public static function ldapAvailable()
/**
* Create a resource from name
*
* @param string $resourceName
* @return DbConnection|LdapConnection|LivestatusConnection|StatusdatReader
*/
public static function create($resourceName)
{
return extension_loaded('ldap');
}
public static function pgsqlAvailable()
{
return extension_loaded('pgsql');
}
public static function mysqlAvailable()
{
return extension_loaded('mysql');
return self::createResource(self::getResourceConfig($resourceName));
}
}

View File

@ -97,7 +97,7 @@ class Backend implements Selectable, Queryable, ConnectionInterface
);
}
}
$resource = ResourceFactory::createResource(ResourceFactory::getResourceConfig($backendConfig->resource));
$resource = ResourceFactory::create($backendConfig->resource);
if ($backendConfig->type === 'ido' && $resource->getDbType() !== 'oracle') {
// TODO(el): The resource should set the table prefix
$resource->setTablePrefix('icinga_');

View File

@ -68,12 +68,9 @@ class LdapBackendFormTest extends BaseTestCase
protected function setUpResourceFactoryMock()
{
Mockery::mock('alias:Icinga\Data\ResourceFactory')
->shouldReceive('ldapAvailable')
->andReturn(true)
->shouldReceive('getResourceConfig')
->andReturn(new \Zend_Config(array()))
->shouldReceive('createResource')
->with(Mockery::type('\Zend_Config'))
->shouldReceive('create')
->andReturn(Mockery::mock('Icinga\Protocol\Ldap\Connection'));
}
}

View File

@ -261,8 +261,6 @@ class ResourceFormTest extends BaseTestCase
protected function setUpResourceFactoryMock($resourceMock)
{
Mockery::mock('alias:Icinga\Data\ResourceFactory')
->shouldReceive('mysqlAvailable')
->andReturn(true)
->shouldReceive('createResource')
->with(Mockery::type('\Zend_Config'))
->andReturn($resourceMock);