Skip to content

Commit

Permalink
Implement all known requirements as object
Browse files Browse the repository at this point in the history
refs #8508
  • Loading branch information
Johannes Meyer committed Feb 25, 2015
1 parent 24d0999 commit 04630a2
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 151 deletions.
16 changes: 6 additions & 10 deletions modules/monitoring/library/Monitoring/MonitoringWizard.php
Expand Up @@ -3,7 +3,6 @@

namespace Icinga\Module\Monitoring;

use Icinga\Application\Platform;
use Icinga\Web\Form;
use Icinga\Web\Wizard;
use Icinga\Web\Request;
Expand All @@ -17,6 +16,7 @@
use Icinga\Module\Monitoring\Forms\Setup\SecurityPage;
use Icinga\Module\Monitoring\Forms\Setup\IdoResourcePage;
use Icinga\Module\Monitoring\Forms\Setup\LivestatusResourcePage;
use Icinga\Module\Setup\Requirement\PhpModuleRequirement;

/**
* Monitoring Module Setup Wizard
Expand Down Expand Up @@ -137,19 +137,15 @@ public function getRequirements()
{
$requirements = new Requirements();

$requirements->addOptional(
'existing_php_mod_sockets',
mt('monitoring', 'PHP Module: Sockets'),
mt(
$requirements->add(new PhpModuleRequirement(array(
'optional' => true,
'condition' => 'Sockets',
'description' => mt(
'monitoring',
'In case it\'s desired that a TCP connection is being used by Icinga Web 2 to'
. ' access a Livestatus interface, the Sockets module for PHP is required.'
),
Platform::extensionLoaded('sockets'),
Platform::extensionLoaded('sockets') ? mt('monitoring', 'The PHP Module sockets is available.') : (
mt('monitoring', 'The PHP Module sockets is not available.')
)
);
)));

return $requirements;
}
Expand Down
28 changes: 28 additions & 0 deletions modules/setup/library/Setup/Requirement/ClassRequirement.php
@@ -0,0 +1,28 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Requirement;

use Icinga\Application\Platform;
use Icinga\Module\Setup\Requirement;

class ClassRequirement extends Requirement
{
protected function evaluate()
{
$classNameOrPath = $this->getCondition();
if (Platform::classExists($classNameOrPath)) {
$this->setStateText(sprintf(
mt('setup', 'The %s is available.', 'setup.requirement.class'),
$this->getAlias() ?: $classNameOrPath . ' ' . mt('setup', 'class', 'setup.requirement.class')
));
return true;
} else {
$this->setStateText(sprintf(
mt('setup', 'The %s is missing.', 'setup.requirement.class'),
$this->getAlias() ?: $classNameOrPath . ' ' . mt('setup', 'class', 'setup.requirement.class')
));
return false;
}
}
}
@@ -0,0 +1,42 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Requirement;

use Icinga\Module\Setup\Requirement;

class ConfigDirectoryRequirement extends Requirement
{
public function getTitle()
{
$title = parent::getTitle();
if ($title === null) {
return mt('setup', 'Read- and writable configuration directory');
}

return $title;
}

protected function evaluate()
{
$path = $this->getCondition();
if (file_exists($path)) {
$readable = is_readable($path);
if ($readable && is_writable($path)) {
$this->setStateText(sprintf(mt('setup', 'The directory %s is read- and writable.'), $path));
return true;
} else {
$this->setStateText(sprintf(
$readable
? mt('setup', 'The directory %s is not writable.')
: mt('setup', 'The directory %s is not readable.'),
$path
));
return false;
}
} else {
$this->setStateText(sprintf(mt('setup', 'The directory %s does not exist.'), $path));
return false;
}
}
}
27 changes: 27 additions & 0 deletions modules/setup/library/Setup/Requirement/OSRequirement.php
@@ -0,0 +1,27 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Requirement;

use Icinga\Application\Platform;
use Icinga\Module\Setup\Requirement;

class OSRequirement extends Requirement
{
public function getTitle()
{
$title = parent::getTitle();
if ($title === null) {
return sprintf(mt('setup', '%s Platform'), ucfirst($this->getCondition()));
}

return $title;
}

protected function evaluate()
{
$phpOS = Platform::getOperatingSystemName();
$this->setStateText(sprintf(mt('setup', 'You are running PHP on a %s system.'), ucfirst($phpOS)));
return strtolower($phpOS) === strtolower($this->getCondition());
}
}
22 changes: 22 additions & 0 deletions modules/setup/library/Setup/Requirement/PhpConfigRequirement.php
@@ -0,0 +1,22 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Requirement;

use Icinga\Application\Platform;
use Icinga\Module\Setup\Requirement;

class PhpConfigRequirement extends Requirement
{
protected function evaluate()
{
list($configDirective, $value) = $this->getCondition();
$configValue = Platform::getPhpConfig($configDirective);
$this->setStateText(
$configValue
? sprintf(mt('setup', 'The PHP config `%s\' is set to "%s".'), $configDirective, $configValue)
: sprintf(mt('setup', 'The PHP config `%s\' is not defined.'), $configDirective)
);
return is_bool($value) ? $configValue == $value : $configValue === $value;
}
}
42 changes: 42 additions & 0 deletions modules/setup/library/Setup/Requirement/PhpModuleRequirement.php
@@ -0,0 +1,42 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Requirement;

use Icinga\Application\Platform;
use Icinga\Module\Setup\Requirement;

class PhpModuleRequirement extends Requirement
{
public function getTitle()
{
$title = parent::getTitle();
if ($title === $this->getAlias()) {
if ($title === null) {
$title = $this->getCondition();
}

return sprintf(mt('setup', 'PHP Module: %s'), $title);
}

return $title;
}

protected function evaluate()
{
$moduleName = $this->getCondition();
if (Platform::extensionLoaded($moduleName)) {
$this->setStateText(sprintf(
mt('setup', 'The PHP module %s is available.'),
$this->getAlias() ?: $moduleName
));
return true;
} else {
$this->setStateText(sprintf(
mt('setup', 'The PHP module %s is missing.'),
$this->getAlias() ?: $moduleName
));
return false;
}
}
}
28 changes: 28 additions & 0 deletions modules/setup/library/Setup/Requirement/PhpVersionRequirement.php
@@ -0,0 +1,28 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Requirement;

use Icinga\Application\Platform;
use Icinga\Module\Setup\Requirement;

class PhpVersionRequirement extends Requirement
{
public function getTitle()
{
$title = parent::getTitle();
if ($title === null) {
return mt('setup', 'PHP Version');
}

return $title;
}

protected function evaluate()
{
$phpVersion = Platform::getPhpVersion();
$this->setStateText(sprintf(mt('setup', 'You are running PHP version %s.'), $phpVersion));
list($operator, $requiredVersion) = $this->getCondition();
return version_compare($phpVersion, $requiredVersion, $operator);
}
}

0 comments on commit 04630a2

Please sign in to comment.