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']) file_exists($config_ini['logging']['target'])
) )
) { ) {
$config = ResourceFactory::getResourceConfig('logfile'); $resource = ResourceFactory::create('logfile');
$resource = ResourceFactory::createResource($config);
$this->view->logData = $resource->select()->order('DESC')->paginate(); $this->view->logData = $resource->select()->order('DESC')->paginate();
} else { } else {
$this->view->logData = null; $this->view->logData = null;

View File

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

View File

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

View File

@ -5,6 +5,7 @@
namespace Icinga\Form\Config; namespace Icinga\Form\Config;
use Exception; use Exception;
use Icinga\Application\Platform;
use Zend_Config; use Zend_Config;
use Zend_Form_Element_Checkbox; use Zend_Form_Element_Checkbox;
use Icinga\Web\Form; 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 * 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. * uninstalled extension, an error should be displayed.
*/ */
if ($config->db === 'mysql' && !ResourceFactory::mysqlAvailable()) { if ($config->db === 'mysql' && ! Platform::extensionLoaded('mysql')) {
$this->addErrorMessage( $this->addErrorMessage(
t('You need to install the php extension "mysql" and the ' . t('You need to install the php extension "mysql" and the ' .
'Zend_Pdo_Mysql classes to use MySQL database resources.') 'Zend_Pdo_Mysql classes to use MySQL database resources.')
); );
return false; return false;
} }
if ($config->db === 'pgsql' && !ResourceFactory::pgsqlAvailable()) { if ($config->db === 'pgsql' && ! Platform::extensionLoaded('pgsql')) {
$this->addErrorMessage( $this->addErrorMessage(
t('You need to install the php extension "pgsql" and the ' . t('You need to install the php extension "pgsql" and the ' .
'Zend_Pdo_Pgsql classes to use PostgreSQL database resources.') 'Zend_Pdo_Pgsql classes to use PostgreSQL database resources.')

View File

@ -4,22 +4,57 @@
namespace Icinga\Application; namespace Icinga\Application;
/**
* Platform tests for icingaweb
*/
class Platform class Platform
{ {
/**
* Domain name
*
* @var string
*/
protected static $domain; protected static $domain;
/**
* Host name
*
* @var string
*/
protected static $hostname; protected static $hostname;
/**
* Fully qualified domain name
*
* @var string
*/
protected static $fqdn; protected static $fqdn;
/**
* Test of windows
*
* @return bool
*/
public static function isWindows() public static function isWindows()
{ {
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
} }
/**
* Test of linux
*
* @return bool
*/
public static function isLinux() public static function isLinux()
{ {
return strtoupper(substr(PHP_OS, 0, 5)) === 'LINUX'; return strtoupper(substr(PHP_OS, 0, 5)) === 'LINUX';
} }
/**
* Test of CLI environment
*
* @return bool
*/
public static function isCli() public static function isCli()
{ {
if (PHP_SAPI == 'cli') { if (PHP_SAPI == 'cli') {
@ -31,6 +66,11 @@ class Platform
return false; return false;
} }
/**
* Get the hostname
*
* @return string
*/
public static function getHostname() public static function getHostname()
{ {
if (self::$hostname === null) { if (self::$hostname === null) {
@ -39,6 +79,11 @@ class Platform
return self::$hostname; return self::$hostname;
} }
/**
* Get the domain name
*
* @return string
*/
public static function getDomain() public static function getDomain()
{ {
if (self::$domain === null) { if (self::$domain === null) {
@ -47,6 +92,11 @@ class Platform
return self::$domain; return self::$domain;
} }
/**
* Get the fully qualified domain name
*
* @return string
*/
public static function getFqdn() public static function getFqdn()
{ {
if (self::$fqdn === null) { if (self::$fqdn === null) {
@ -55,6 +105,9 @@ class Platform
return self::$fqdn; return self::$fqdn;
} }
/**
* Initialize domain and host strings
*/
protected static function discoverHostname() protected static function discoverHostname()
{ {
self::$hostname = gethostname(); self::$hostname = gethostname();
@ -66,4 +119,16 @@ class Platform
self::$domain = array_shift(preg_split('~\.~', self::$hostname, 2)); 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\Ldap\Connection as LdapConnection;
use Icinga\Protocol\File\Reader as FileReader; use Icinga\Protocol\File\Reader as FileReader;
/**
* Create resources from names or resource configuration
*/
class ResourceFactory implements ConfigAwareFactory class ResourceFactory implements ConfigAwareFactory
{ {
/** /**
* Resource configuration
*
* @var Zend_Config * @var Zend_Config
*/ */
private static $resources; private static $resources;
/**
* Set resource configurations
*
* @param Zend_Config $config
*/
public static function setConfig($config) public static function setConfig($config)
{ {
self::$resources = $config; self::$resources = $config;
@ -29,10 +39,11 @@ class ResourceFactory implements ConfigAwareFactory
/** /**
* Get the configuration for a specific resource * Get the configuration for a specific resource
* *
* @param $resourceName String The resource's name * @param $resourceName String The resource's name
* *
* @return Zend_Config The configuration of the resource * @return Zend_Config The configuration of the resource
* @throws \Icinga\Exception\ConfigurationError *
* @throws ConfigurationError
*/ */
public static function getResourceConfig($resourceName) 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. * Check if the existing resources are set. If not, throw an error.
* *
* @throws \Icinga\Exception\ProgrammingError * @throws ProgrammingError
*/ */
private static function assertResourcesExist() 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 * @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'. * 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) public static function createResource(Zend_Config $config)
{ {
@ -122,18 +133,14 @@ class ResourceFactory implements ConfigAwareFactory
return $resource; 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'); return self::createResource(self::getResourceConfig($resourceName));
}
public static function pgsqlAvailable()
{
return extension_loaded('pgsql');
}
public static function mysqlAvailable()
{
return extension_loaded('mysql');
} }
} }

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') { if ($backendConfig->type === 'ido' && $resource->getDbType() !== 'oracle') {
// TODO(el): The resource should set the table prefix // TODO(el): The resource should set the table prefix
$resource->setTablePrefix('icinga_'); $resource->setTablePrefix('icinga_');

View File

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

View File

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