N°4789 - make autoselect and dependencies work as well

This commit is contained in:
odain
2025-08-26 11:29:19 +02:00
parent 8af748bd3e
commit 812e24b402
2 changed files with 98 additions and 11 deletions

View File

@@ -325,7 +325,7 @@ class ModuleDiscoveryService {
* @return bool
* @throws ModuleDiscoveryServiceException
*/
public function ComputeBooleanExpression(string $sBooleanExpr) : bool
public function UnprotectedComputeBooleanExpression(string $sBooleanExpr) : bool
{
$bResult = false;
try{
@@ -343,20 +343,28 @@ class ModuleDiscoveryService {
* @return bool
* @throws ModuleDiscoveryServiceException
*/
public function ComputeBooleanExpression3(string $sBooleanExpr) : bool
public function ComputeBooleanExpression(string $sBooleanExpr, $bProtected=true) : bool
{
if (! $bProtected){
return $this->UnprotectedComputeBooleanExpression($sBooleanExpr);
}
$sPhpContent = <<<PHP
<?php
$sBooleanExpr;
PHP;
$aNodes = ModuleDiscoveryService::GetInstance()->parsePhpCode($sPhpContent);
$oExpr = $aNodes[0];
return $this->EvaluateBooleanExpression($oExpr->expr);
try{
$aNodes = ModuleDiscoveryService::GetInstance()->parsePhpCode($sPhpContent);
$oExpr = $aNodes[0];
return $this->EvaluateBooleanExpression($oExpr->expr);
} catch (ModuleDiscoveryServiceException $previous) {
throw new ModuleDiscoveryServiceException("Eval of '$sBooleanExpr' caused an error", 0, $previous);
}
}
private function EvaluateBooleanExpression(\PhpParser\Node\Expr $oCondExpression) : bool
{
//var_dump($oCondExpression);
#var_dump($oCondExpression);
if ($oCondExpression instanceof \PhpParser\Node\Expr\BinaryOp){
$sExpr = $this->GetMixedValueForBooleanOperatorEvaluation($oCondExpression->left)
@@ -364,7 +372,7 @@ PHP;
. $oCondExpression->getOperatorSigil()
. " "
. $this->GetMixedValueForBooleanOperatorEvaluation($oCondExpression->right);
return $this->ComputeBooleanExpression($sExpr);
return $this->ComputeBooleanExpression($sExpr, false);
}
if ($oCondExpression instanceof \PhpParser\Node\Expr\BooleanNot){
@@ -375,6 +383,10 @@ PHP;
return $this->CallFunction($oCondExpression);
}
if ($oCondExpression instanceof \PhpParser\Node\Expr\StaticCall){
return $this->StaticCallFunction($oCondExpression);
}
if ($oCondExpression instanceof \PhpParser\Node\Expr\ConstFetch){
return $this->EvaluateConstantExpression($oCondExpression);
}
@@ -400,6 +412,39 @@ PHP;
$oReflectionFunction = new ReflectionFunction($sFunction);
return (bool)$oReflectionFunction->invoke(...$aArgs);
}
/**
* @param \PhpParser\Node\Expr\StaticCall $oStaticCall
*
* @return bool
* @throws \ModuleDiscoveryServiceException
* @throws \ReflectionException
*/
private function StaticCallFunction(\PhpParser\Node\Expr\StaticCall $oStaticCall) : bool
{
var_dump($oStaticCall);
$sClassName = $oStaticCall->class->name;
$sMethodName = $oStaticCall->name->name;
$aWhiteList = ["SetupInfo::ModuleIsSelected"];
$sStaticCallDescription = "$sClassName::$sMethodName";
if (! in_array($sStaticCallDescription, $aWhiteList)){
throw new ModuleDiscoveryServiceException("StaticCall $sStaticCallDescription not supported");
}
$aArgs=[];
foreach ($oStaticCall->args as $arg){
/** @var \PhpParser\Node\Arg $arg */
$aArgs[]=$arg->value->value;
}
$class = new \ReflectionClass($sClassName);
$method = $class->getMethod($sMethodName);
if (! $method->isPublic()){
throw new ModuleDiscoveryServiceException("StaticCall $sStaticCallDescription not public");
}
return (bool) $method->invokeArgs(null, $aArgs);
}
}
class ModuleDiscoveryServiceException extends Exception

View File

@@ -31,10 +31,15 @@ class ModuleDiscoveryServiceTest extends ItopDataTestCase
/*public function testAllReadModuleFileConfiguration()
{
foreach (glob(__DIR__.'/resources/all/module.*.php') as $sModuleFilePath){
$aRes = ModuleDiscoveryService::GetInstance()->ReadModuleFileConfiguration($sModuleFilePath);
$aExpected = ModuleDiscoveryService::GetInstance()->ReadModuleFileConfigurationLegacy($sModuleFilePath);
$aRes = ModuleDiscoveryService::GetInstance()->ReadModuleFileConfiguration($sModuleFilePath);
$aExpected = ModuleDiscoveryService::GetInstance()->ReadModuleFileConfigurationLegacy($sModuleFilePath);
$this->assertEquals($aExpected, $aRes);
$this->assertEquals($aExpected, $aRes);
$aAutoselect = $aRes[2]['auto_select'] ?? "";
if (strlen($aAutoselect) >0){
var_dump($aAutoselect);
}
}
}*/
@@ -88,10 +93,47 @@ class ModuleDiscoveryServiceTest extends ItopDataTestCase
public function testComputeBooleanExpression_BrokenBooleanExpression(){
$this->expectException(\ModuleDiscoveryServiceException::class);
$this->expectExceptionMessage('Eval of \'(a || true)\' caused an error: Undefined constant "a"');
$this->expectExceptionMessage('Eval of \'(a || true)\' caused an error');
$this->assertTrue(ModuleDiscoveryService::GetInstance()->ComputeBooleanExpression("(a || true)"));
}
public static function ComputeBooleanExpressionAutoselectProvider()
{
$sSimpleCallToModuleIsSelected = "SetupInfo::ModuleIsSelected(\"itop-storage-mgmt\")";
$sSimpleCallToModuleIsSelected2 = "SetupInfo::ModuleIsSelected(\"itop-storage-mgmt-notselected\")";
$sCallToModuleIsSelectedCombinedWithAndOperator = "SetupInfo::ModuleIsSelected(\"itop-storage-mgmt\") || SetupInfo::ModuleIsSelected(\"itop-virtualization-mgmt\")";
$sCallToModuleIsSelectedCombinedWithAndOperator2 = "SetupInfo::ModuleIsSelected(\"itop-storage-mgmt-notselected\") || SetupInfo::ModuleIsSelected(\"itop-virtualization-mgmt\")";
return [
"simple call to SetupInfo::ModuleIsSelected SELECTED" => [
"expr" => $sSimpleCallToModuleIsSelected,
"expected" => true
],
"simple call to SetupInfo::ModuleIsSelected NOT SELECTED" => [
"expr" => $sSimpleCallToModuleIsSelected2,
"expected" => false
],
"call to SetupInfo::ModuleIsSelected + OR => SELECTED" => [
"expr" => $sCallToModuleIsSelectedCombinedWithAndOperator,
"expected" => true
],
"simple call to SetupInfo::ModuleIsSelected + OR => NOT SELECTED" => [
"expr" => $sCallToModuleIsSelectedCombinedWithAndOperator2,
"expected" => false
],
];
}
/**
* @dataProvider ComputeBooleanExpressionAutoselectProvider
*/
public function testComputeBooleanExpressionAutoselect(string $sBooleanExpression, bool $expected){
\SetupInfo::SetSelectedModules(["itop-storage-mgmt" => "123"]);
$this->assertEquals($expected, ModuleDiscoveryService::GetInstance()->ComputeBooleanExpression($sBooleanExpression), $sBooleanExpression);
}
public function testEvaluateConstantExpression()
{
$sPHP = <<<PHP