revert to previous legacy order + gather new module computation classes in a dedicated folder

This commit is contained in:
odain
2025-09-09 16:52:41 +02:00
parent 4207b25bde
commit b194a0b17c
7 changed files with 497 additions and 386 deletions

View File

@@ -0,0 +1,118 @@
<?php
/**
* Class that handles a module dependency
*/
class iTopCoreModuleDependency {
private array $aPotentialPrerequisites;
private array $aParamsPerModuleId;
private string $sDepString;
private bool $bAlwaysUnresolved=false;
public function __construct(string $sDepString)
{
$this->sDepString = $sDepString;
$this->aParamsPerModuleId = [];
$this->aPotentialPrerequisites = [];
if (preg_match_all('/([^\(\)&| ]+)/', $sDepString, $aMatches))
{
foreach($aMatches as $aMatch)
{
foreach($aMatch as $sModuleId)
{
if (! array_key_exists($sModuleId, $this->aParamsPerModuleId)) {
// $sModuleId in the dependency string is made of a <name>/<optional_operator><version>
// where the operator is < <= = > >= (by default >=)
$aModuleMatches = array();
if (preg_match('|^([^/]+)/(<?>?=?)([^><=]+)$|', $sModuleId, $aModuleMatches)) {
$sModuleName = $aModuleMatches[1];
$this->aPotentialPrerequisites[$sModuleName] = true;
$sOperator = $aModuleMatches[2];
if ($sOperator == '') {
$sOperator = '>=';
}
$sExpectedVersion = $aModuleMatches[3];
$this->aParamsPerModuleId[$sModuleId] = [$sModuleName, $sOperator, $sExpectedVersion];
}
}
}
}
} else {
$this->bAlwaysUnresolved=true;
}
}
/**
* Return module names potentially required by current dependency
* @return array
*/
public function GetPotentialPrerequisiteModuleNames() : array
{
return array_keys($this->aPotentialPrerequisites);
}
/**
* Check if dependency is resolved with current list of module versions
* @param array $aModuleVersions: versions by module names dict
* @param array $aSelectedModules: modules names dict
*
* @return bool
*/
public function IsDependencyResolved(array $aModuleVersions, array $aSelectedModules) : bool
{
if ($this->bAlwaysUnresolved){
return false;
}
$aReplacements=[];
foreach ($this->aParamsPerModuleId as $sModuleId => list($sModuleName, $sOperator, $sExpectedVersion)){
if (array_key_exists($sModuleName, $aModuleVersions))
{
// module is present, check the version
$sCurrentVersion = $aModuleVersions[$sModuleName];
if (version_compare($sCurrentVersion, $sExpectedVersion, $sOperator))
{
if (array_key_exists($sModuleName, $this->aPotentialPrerequisites)) {
unset($this->aPotentialPrerequisites[$sModuleName]);
}
$aReplacements[$sModuleId] = '(true)'; // Add parentheses to protect against invalid condition causing
// a function call that results in a runtime fatal error
}
else
{
$aReplacements[$sModuleId] = '(false)'; // Add parentheses to protect against invalid condition causing
// a function call that results in a runtime fatal error
}
}
else
{
// module is not present
$aReplacements[$sModuleId] = '(false)'; // Add parentheses to protect against invalid condition causing
// a function call that results in a runtime fatal error
}
}
foreach ($this->aPotentialPrerequisites as $sModuleName)
{
if (array_key_exists($sModuleName, $aSelectedModules))
{
// This module is actually a prerequisite
if (!array_key_exists($sModuleName, $aModuleVersions))
{
return false;
}
}
}
$bResult=false;
$sBooleanExpr = str_replace(array_keys($aReplacements), array_values($aReplacements), $this->sDepString);
$bOk = @eval('$bResult = '.$sBooleanExpr.'; return true;');
if ($bOk == false)
{
SetupLog::Warning("Eval of '$sBooleanExpr' returned false");
echo "Failed to parse the boolean Expression = '$sBooleanExpr'<br/>";
}
return $bResult;
}
}