Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jul 26, 2022
1 parent 62d7ca8 commit 20208a4
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 3 deletions.
3 changes: 3 additions & 0 deletions modules/setup/application/forms/ModulePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Icinga\Application\Icinga;
use Icinga\Application\Modules\Module;
use Icinga\Module\Setup\ModuleDependencyWizard;
use Icinga\Web\Form;

class ModulePage extends Form
Expand Down Expand Up @@ -100,6 +101,8 @@ public function getModuleWizards()
foreach ($checked as $name => $module) {
if ($module->providesSetupWizard()) {
$wizards[$name] = $module->getSetupWizard();
} elseif (! empty($module->getRequiredModules())) {
$wizards[$name] = new ModuleDependencyWizard($module, array_keys($checked));
}
}

Expand Down
114 changes: 114 additions & 0 deletions modules/setup/library/Setup/ModuleDependencyWizard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Icinga\Module\Setup;

use Icinga\Application\Modules\Module;
use Icinga\Module\Setup\Requirement\ModuleMissingRequirement;
use Icinga\Module\Setup\Requirement\SetRequirement;
use Icinga\Module\Setup\Requirement\WebModuleRequirement;

class ModuleDependencyWizard
{
protected $module;

protected $checkedModules;

public function __construct(Module $module, array $checkedModules)
{
$this->module = $module;
$this->checkedModules = $checkedModules;
}

public function getRequirements()
{
$icingadbAndMonitoring = [];
$set = new RequirementSet();

foreach ($this->module->getRequiredModules() as $name => $requiredVersion) {
if ($name === 'monitoring' || $name === 'icingadb') {
$icingadbAndMonitoring[$name] = $requiredVersion;

continue;
}

if (! in_array($name, $this->checkedModules)) {
$set->add(
(new ModuleMissingRequirement(
[
'title' => $name,
'alias' => $name,
'description' => sprintf(
t('Module %s (%s) is required.'),
$name,
$requiredVersion
)
]
))
);
} else {
$set->add(
(new WebModuleRequirement(
[
'title' => $name,
'alias' => $name,
'condition' => [$name, $requiredVersion],
'description' => sprintf(
t('Module %s (%s) is required.'),
$name,
$requiredVersion
)
]
))
);
}
}

if (! empty($icingadbAndMonitoring)) {
$icingadbOrmonitoring = new RequirementSet(false, RequirementSet::MODE_OR);
foreach ($icingadbAndMonitoring as $name => $requiredVersion) {
if (! in_array($name, $this->checkedModules)) {
$icingadbOrmonitoring->add(
(new ModuleMissingRequirement(
[
'title' => $name,
'alias' => $name,
'optional' => true,
'description' => sprintf(
t('Module %s (%s) is required.'),
$name,
$requiredVersion
)
]
))
);
} else {
$icingadbOrmonitoring->add(new WebModuleRequirement([
'title' => $name,
'alias' => $name,
'optional' => true,
'condition' => [$name, $requiredVersion],
'description' => sprintf(
t('Module %s (%s) is required.'),
$name,
$requiredVersion
)
]));
}
}

$set->merge($icingadbOrmonitoring);

$requirement = (new SetRequirement([
'title' =>'Base Module',
'alias' => 'Monitoring OR Icingadb',
'optional' => false,
'condition' => $icingadbOrmonitoring,
'description' => t('Module Monitoring OR Icingadb is required.')
]));

$set->add($requirement);
}

return $set;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Icinga\Module\Setup\Requirement;

use Icinga\Module\Setup\Requirement;

class ModuleMissingRequirement extends Requirement
{
protected function evaluate()
{
$this->setStateText(sprintf(
mt('setup', 'Module %s is not enabled.'),
$this->getAlias()
));

return false;
}

public function equals(Requirement $requirement)
{
return false;
}
}
14 changes: 12 additions & 2 deletions modules/setup/library/Setup/Requirement/WebModuleRequirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ class WebModuleRequirement extends Requirement
{
protected function evaluate()
{
list($name, $op, $version) = $this->getCondition();
if (count($this->getCondition()) === 2) {
list($name, $version) = $this->getCondition();
$op = '=';
if (is_string($version)
&& preg_match('/^([<>=]{1,2})\s*v?((?:[\d.]+)(?:.+)?)$/', $version, $match)) {
$op = $match[1];
$version = $match[2];
}
} else {
list($name, $op, $version) = $this->getCondition();
}

$mm = Icinga::app()->getModuleManager();
if (! $mm->hasInstalled($name)) {
Expand All @@ -26,6 +36,6 @@ protected function evaluate()
}

$this->setStateText(sprintf(mt('setup', '%s version: %s'), $this->getAlias(), $moduleVersion));
return version_compare($moduleVersion, $version, $op);
return ($version === true || $version === null) ? true : version_compare($moduleVersion, $version, $op);
}
}
4 changes: 3 additions & 1 deletion modules/setup/library/Setup/WebWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ protected function init()
if (($modulePageData = $this->getPageData('setup_modules')) !== null) {
$modulePage = $this->getPage('setup_modules')->populate($modulePageData);
foreach ($modulePage->getModuleWizards() as $moduleWizard) {
$this->addPage($moduleWizard);
if (! $moduleWizard instanceof ModuleDependencyWizard) {
$this->addPage($moduleWizard);
}
}
}
}
Expand Down

0 comments on commit 20208a4

Please sign in to comment.