mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-21 09:38:48 +02:00
N°4789 - refactor and split in ModuleDiscoveryEvaluationService + handle ModuleInstallerAPI methods calls during setup
This commit is contained in:
@@ -391,7 +391,7 @@ class ModuleDiscovery
|
||||
{
|
||||
$sBooleanExpr = str_replace(array_keys($aReplacements), array_values($aReplacements), $sDepString);
|
||||
try{
|
||||
$bResult = ModuleDiscoveryService::GetInstance()->ComputeBooleanExpression($sBooleanExpr);
|
||||
$bResult = ModuleDiscoveryEvaluationService::GetInstance()->EvaluateBooleanExpression($sBooleanExpr);
|
||||
} catch(ModuleDiscoveryServiceException $e){
|
||||
//logged already
|
||||
echo "Failed to parse the boolean Expression = '$sBooleanExpr'<br/>";
|
||||
|
||||
343
setup/modulediscovery/ModuleDiscoveryEvaluationService.php
Normal file
343
setup/modulediscovery/ModuleDiscoveryEvaluationService.php
Normal file
@@ -0,0 +1,343 @@
|
||||
<?php
|
||||
|
||||
use PhpParser\ParserFactory;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
|
||||
class ModuleDiscoveryEvaluationService {
|
||||
private static ModuleDiscoveryEvaluationService $oInstance;
|
||||
|
||||
protected function __construct() {
|
||||
}
|
||||
|
||||
final public static function GetInstance(): ModuleDiscoveryEvaluationService {
|
||||
if (!isset(static::$oInstance)) {
|
||||
static::$oInstance = new static();
|
||||
}
|
||||
|
||||
return static::$oInstance;
|
||||
}
|
||||
|
||||
final public static function SetInstance(?ModuleDiscoveryEvaluationService $oInstance): void {
|
||||
static::$oInstance = $oInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sPhpContent
|
||||
*
|
||||
* @return \PhpParser\Node\Stmt[]|null
|
||||
*/
|
||||
public function ParsePhpCode(string $sPhpContent): ?array
|
||||
{
|
||||
$oParser = (new ParserFactory())->createForNewestSupportedVersion();
|
||||
return $oParser->parse($sPhpContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sModuleFilePath
|
||||
* @param \PhpParser\Node\Expr\Assign $oAssignation
|
||||
*
|
||||
* @return array|null
|
||||
* @throws \ModuleDiscoveryServiceException
|
||||
*/
|
||||
public function BrowseAddModuleCallAndReturnModuleConfiguration(string $sModuleFilePath, \PhpParser\Node\Stmt\Expression $oExpression) : ?array
|
||||
{
|
||||
/** @var Assign $oAssignation */
|
||||
$oAssignation = $oExpression->expr;
|
||||
if (false === ($oAssignation instanceof PhpParser\Node\Expr\StaticCall)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var PhpParser\Node\Expr\StaticCall $oAssignation */
|
||||
|
||||
if ("SetupWebPage" !== $oAssignation?->class?->name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ("AddModule" !== $oAssignation?->name?->name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$aArgs = $oAssignation?->args;
|
||||
if (count($aArgs) != 3) {
|
||||
throw new ModuleDiscoveryServiceException("Not enough parameters when calling SetupWebPage::AddModule", 0, null, $sModuleFilePath);
|
||||
}
|
||||
|
||||
$oModuleId = $aArgs[1];
|
||||
if (false === ($oModuleId instanceof PhpParser\Node\Arg)) {
|
||||
throw new ModuleDiscoveryServiceException("2nd parameter to SetupWebPage::AddModule call issue: " . get_class($oModuleId), 0, null, $sModuleFilePath);
|
||||
}
|
||||
|
||||
/** @var PhpParser\Node\Arg $oModuleId */
|
||||
if (false === ($oModuleId->value instanceof PhpParser\Node\Scalar\String_)) {
|
||||
throw new ModuleDiscoveryServiceException("2nd parameter to SetupWebPage::AddModule not a string: " . get_class($oModuleId->value), 0, null, $sModuleFilePath);
|
||||
}
|
||||
|
||||
/** @var PhpParser\Node\Scalar\String_ $sModuleIdStringObj */
|
||||
$sModuleIdStringObj = $oModuleId->value;
|
||||
$sModuleId = $sModuleIdStringObj->value;
|
||||
|
||||
$oModuleConfigInfo = $aArgs[2];
|
||||
if (false === ($oModuleConfigInfo instanceof PhpParser\Node\Arg)) {
|
||||
throw new ModuleDiscoveryServiceException("3rd parameter to SetupWebPage::AddModule call issue: " . get_class($oModuleConfigInfo), 0, null, $sModuleFilePath);
|
||||
}
|
||||
|
||||
/** @var PhpParser\Node\Arg $oModuleConfigInfo */
|
||||
if (false === ($oModuleConfigInfo->value instanceof PhpParser\Node\Expr\Array_)) {
|
||||
throw new ModuleDiscoveryServiceException("3rd parameter to SetupWebPage::AddModule not an array: " . get_class($oModuleConfigInfo->value), 0, null, $sModuleFilePath);
|
||||
}
|
||||
|
||||
$aModuleConfig=[];
|
||||
$this->BrowseArrayStructure($oModuleConfigInfo->value, $aModuleConfig);
|
||||
|
||||
if (! is_array($aModuleConfig)){
|
||||
throw new ModuleDiscoveryServiceException("3rd parameter to SetupWebPage::AddModule not an array: " . get_class($oModuleConfigInfo->value), 0, null, $sModuleFilePath);
|
||||
}
|
||||
return [
|
||||
$sModuleFilePath,
|
||||
$sModuleId,
|
||||
$aModuleConfig,
|
||||
];
|
||||
}
|
||||
|
||||
public function BrowseArrayStructure(PhpParser\Node\Expr\Array_ $oArray, array &$aModuleConfig) : void
|
||||
{
|
||||
$iIndex=0;
|
||||
/** @var \PhpParser\Node\Expr\ArrayItem $oValue */
|
||||
foreach ($oArray->items as $oArrayItem){
|
||||
if ($oArrayItem->key instanceof PhpParser\Node\Scalar\String_) {
|
||||
//dictionnary
|
||||
$sKey = $oArrayItem->key->value;
|
||||
} else if ($oArrayItem->key instanceof \PhpParser\Node\Expr\ConstFetch) {
|
||||
$sKey = $this->EvaluateConstantExpression($oArrayItem->key);
|
||||
if (is_null($sKey)){
|
||||
continue;
|
||||
}
|
||||
}else {
|
||||
$sKey = $iIndex++;
|
||||
}
|
||||
|
||||
$oValue = $oArrayItem->value;
|
||||
|
||||
if ($oValue instanceof PhpParser\Node\Expr\Array_) {
|
||||
$aSubConfig=[];
|
||||
$this->BrowseArrayStructure($oValue, $aSubConfig);
|
||||
$aModuleConfig[$sKey]=$aSubConfig;
|
||||
}
|
||||
|
||||
if ($oValue instanceof PhpParser\Node\Scalar\String_||$oValue instanceof PhpParser\Node\Scalar\Int_) {
|
||||
$aModuleConfig[$sKey]=$oValue->value;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($oValue instanceof \PhpParser\Node\Expr\ConstFetch) {
|
||||
$oEvaluatedConstant = $this->EvaluateConstantExpression($oValue);
|
||||
$aModuleConfig[$sKey]= $oEvaluatedConstant;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sModuleFilePath
|
||||
* @param \PhpParser\Node\Stmt\If_ $oNode
|
||||
*
|
||||
* @return array|null
|
||||
* @throws \ModuleDiscoveryServiceException
|
||||
*/
|
||||
public function BrowseIfStructure(string $sModuleFilePath, \PhpParser\Node\Stmt\If_ $oNode) : ?array
|
||||
{
|
||||
$bCondition = $this->EvaluateExpression($oNode->cond);
|
||||
if ($bCondition) {
|
||||
foreach ($oNode->stmts as $oSubNode) {
|
||||
if ($oSubNode instanceof \PhpParser\Node\Stmt\Expression) {
|
||||
$aModuleConfig = $this->BrowseAddModuleCallAndReturnModuleConfiguration($sModuleFilePath, $oSubNode);
|
||||
if (!is_null($aModuleConfig)) {
|
||||
return $aModuleConfig;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! is_null($oNode->elseifs)) {
|
||||
foreach ($oNode->elseifs as $oElseIfSubNode) {
|
||||
/** @var \PhpParser\Node\Stmt\ElseIf_ $oElseIfSubNode */
|
||||
$bCondition = $this->EvaluateExpression($oElseIfSubNode->cond);
|
||||
if ($bCondition) {
|
||||
$aModuleConfig = $this->BrowseStatementsAndReturnModuleConfiguration($sModuleFilePath, $oElseIfSubNode->stmts);
|
||||
if (!is_null($aModuleConfig)) {
|
||||
return $aModuleConfig;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (! is_null($oNode->else)) {
|
||||
$aModuleConfig = $this->BrowseStatementsAndReturnModuleConfiguration($sModuleFilePath, $oNode->else->stmts);
|
||||
|
||||
return $aModuleConfig;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function BrowseStatementsAndReturnModuleConfiguration(string $sModuleFilePath, array $aStmts) : ?array
|
||||
{
|
||||
foreach ($aStmts as $oSubNode) {
|
||||
if ($oSubNode instanceof \PhpParser\Node\Stmt\Expression) {
|
||||
$aModuleConfig = $this->BrowseAddModuleCallAndReturnModuleConfiguration($sModuleFilePath, $oSubNode);
|
||||
if (!is_null($aModuleConfig)) {
|
||||
return $aModuleConfig;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function EvaluateConstantExpression(\PhpParser\Node\Expr\ArrayItem|\PhpParser\Node\Expr\ConstFetch $oValue) : mixed
|
||||
{
|
||||
$bResult = false;
|
||||
try{
|
||||
@eval('$bResult = '.$oValue->name.';');
|
||||
} catch (Throwable $t) {
|
||||
throw new ModuleDiscoveryServiceException("Eval of ' . $oValue->name . ' caused an error: ".$t->getMessage());
|
||||
}
|
||||
|
||||
return $bResult;
|
||||
}
|
||||
|
||||
private function GetMixedValueForBooleanOperatorEvaluation(\PhpParser\Node\Expr $oExpr) : string
|
||||
{
|
||||
if ($oExpr instanceof \PhpParser\Node\Scalar\Int_ || $oExpr instanceof \PhpParser\Node\Scalar\Float_){
|
||||
return "" . $oExpr->value;
|
||||
}
|
||||
|
||||
return $this->EvaluateExpression($oExpr) ? "true" : "false";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sBooleanExpr
|
||||
*
|
||||
* @return bool
|
||||
* @throws ModuleDiscoveryServiceException
|
||||
*/
|
||||
private function UnprotectedComputeBooleanExpression(string $sBooleanExpr) : bool
|
||||
{
|
||||
$bResult = false;
|
||||
try{
|
||||
@eval('$bResult = '.$sBooleanExpr.';');
|
||||
} catch (Throwable $t) {
|
||||
throw new ModuleDiscoveryServiceException("Eval of '$sBooleanExpr' caused an error: ".$t->getMessage());
|
||||
}
|
||||
|
||||
return $bResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sBooleanExpr
|
||||
* @param bool $bSafe: when true, evaluation relies on unsafe eval() call
|
||||
*
|
||||
* @return bool
|
||||
* @throws ModuleDiscoveryServiceException
|
||||
*/
|
||||
public function EvaluateBooleanExpression(string $sBooleanExpr, $bSafe=true) : bool
|
||||
{
|
||||
if (! $bSafe){
|
||||
return $this->UnprotectedComputeBooleanExpression($sBooleanExpr);
|
||||
}
|
||||
|
||||
$sPhpContent = <<<PHP
|
||||
<?php
|
||||
$sBooleanExpr;
|
||||
PHP;
|
||||
try{
|
||||
$aNodes = $this->ParsePhpCode($sPhpContent);
|
||||
$oExpr = $aNodes[0];
|
||||
return $this->EvaluateExpression($oExpr->expr);
|
||||
} catch (Throwable $t) {
|
||||
throw new ModuleDiscoveryServiceException("Eval of '$sBooleanExpr' caused an error:".$t->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function EvaluateExpression(\PhpParser\Node\Expr $oCondExpression) : bool
|
||||
{
|
||||
if ($oCondExpression instanceof \PhpParser\Node\Expr\BinaryOp){
|
||||
$sExpr = $this->GetMixedValueForBooleanOperatorEvaluation($oCondExpression->left)
|
||||
. " "
|
||||
. $oCondExpression->getOperatorSigil()
|
||||
. " "
|
||||
. $this->GetMixedValueForBooleanOperatorEvaluation($oCondExpression->right);
|
||||
return $this->EvaluateBooleanExpression($sExpr, false);
|
||||
}
|
||||
|
||||
if ($oCondExpression instanceof \PhpParser\Node\Expr\BooleanNot){
|
||||
return ! $this->EvaluateExpression($oCondExpression->expr);
|
||||
}
|
||||
|
||||
if ($oCondExpression instanceof \PhpParser\Node\Expr\FuncCall){
|
||||
return $this->EvaluateCallFunction($oCondExpression);
|
||||
}
|
||||
|
||||
if ($oCondExpression instanceof \PhpParser\Node\Expr\StaticCall){
|
||||
return $this->EvaluateStaticCallFunction($oCondExpression);
|
||||
}
|
||||
|
||||
if ($oCondExpression instanceof \PhpParser\Node\Expr\ConstFetch){
|
||||
return $this->EvaluateConstantExpression($oCondExpression);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function EvaluateCallFunction(\PhpParser\Node\Expr\FuncCall $oFunct) : bool
|
||||
{
|
||||
$sFunction = $oFunct->name->name;
|
||||
$aWhiteList = ["function_exists"];
|
||||
if (! in_array($sFunction, $aWhiteList)){
|
||||
throw new ModuleDiscoveryServiceException("FuncCall $sFunction not supported");
|
||||
//return false;
|
||||
}
|
||||
|
||||
$aArgs=[];
|
||||
foreach ($oFunct->args as $arg){
|
||||
/** @var \PhpParser\Node\Arg $arg */
|
||||
$aArgs[]=$arg->value->value;
|
||||
}
|
||||
|
||||
$oReflectionFunction = new ReflectionFunction($sFunction);
|
||||
return (bool)$oReflectionFunction->invoke(...$aArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\StaticCall $oStaticCall
|
||||
*
|
||||
* @return bool
|
||||
* @throws \ModuleDiscoveryServiceException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private function EvaluateStaticCallFunction(\PhpParser\Node\Expr\StaticCall $oStaticCall) : bool
|
||||
{
|
||||
$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);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,9 @@
|
||||
use PhpParser\ParserFactory;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
|
||||
require_once __DIR__ . '/ModuleDiscoveryEvaluationService.php';
|
||||
require_once __DIR__ . '/ModuleDiscoveryServiceException.php';
|
||||
|
||||
class ModuleDiscoveryService {
|
||||
private static ModuleDiscoveryService $oInstance;
|
||||
private static int $iDummyClassIndex = 0;
|
||||
@@ -61,7 +64,7 @@ class ModuleDiscoveryService {
|
||||
throw new ModuleDiscoveryServiceException("Eval of $sModuleFilePath did not return the expected information...");
|
||||
}
|
||||
|
||||
$this->AddModuleFilePath($aModuleInfo);
|
||||
$this->CompleteConfigWithModuleFilePath($aModuleInfo);
|
||||
}
|
||||
catch(ModuleDiscoveryServiceException $e)
|
||||
{
|
||||
@@ -81,30 +84,6 @@ class ModuleDiscoveryService {
|
||||
return $aModuleInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* N°4789 - Parse datamodel module.xxx.php files instead of interpreting them
|
||||
* additional path added to handle ModuleInstallerAPI declaration during setup only
|
||||
* @param array &$aModuleInfo
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function AddModuleFilePath(array &$aModuleInfo)
|
||||
{
|
||||
if (count($aModuleInfo)==3) {
|
||||
$aModuleInfo[2]['module_file_path'] = $aModuleInfo[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sPhpContent
|
||||
*
|
||||
* @return \PhpParser\Node\Stmt[]|null
|
||||
*/
|
||||
public function parsePhpCode(string $sPhpContent): ?array
|
||||
{
|
||||
$oParser = (new ParserFactory())->createForNewestSupportedVersion();
|
||||
return $oParser->parse($sPhpContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the information from a module file (module.xxx.php)
|
||||
@@ -117,7 +96,7 @@ class ModuleDiscoveryService {
|
||||
{
|
||||
try
|
||||
{
|
||||
$aNodes = $this->parsePhpCode(file_get_contents($sModuleFilePath));
|
||||
$aNodes = ModuleDiscoveryEvaluationService::GetInstance()->ParsePhpCode(file_get_contents($sModuleFilePath));
|
||||
}
|
||||
catch (PhpParser\Error $e) {
|
||||
throw new \ModuleDiscoveryServiceException($e->getMessage(), 0, $e, $sModuleFilePath);
|
||||
@@ -126,17 +105,17 @@ class ModuleDiscoveryService {
|
||||
try {
|
||||
foreach ($aNodes as $sKey => $oNode) {
|
||||
if ($oNode instanceof \PhpParser\Node\Stmt\Expression) {
|
||||
$aModuleConfig = $this->ParseCallToAddModuleAndReturnModuleConfiguration($sModuleFilePath, $oNode);
|
||||
$aModuleConfig = ModuleDiscoveryEvaluationService::GetInstance()->BrowseAddModuleCallAndReturnModuleConfiguration($sModuleFilePath, $oNode);
|
||||
if (! is_null($aModuleConfig)){
|
||||
$this->AddModuleFilePath($aModuleConfig);
|
||||
$this->CompleteConfigWithModuleFilePath($aModuleConfig);
|
||||
return $aModuleConfig;
|
||||
}
|
||||
}
|
||||
|
||||
if ($oNode instanceof PhpParser\Node\Stmt\If_) {
|
||||
$aModuleConfig = $this->BrowseIfStructure($sModuleFilePath, $oNode);
|
||||
$aModuleConfig = ModuleDiscoveryEvaluationService::GetInstance()->BrowseIfStructure($sModuleFilePath, $oNode);
|
||||
if (! is_null($aModuleConfig)){
|
||||
$this->AddModuleFilePath($aModuleConfig);
|
||||
$this->CompleteConfigWithModuleFilePath($aModuleConfig);
|
||||
return $aModuleConfig;
|
||||
}
|
||||
}
|
||||
@@ -152,365 +131,97 @@ class ModuleDiscoveryService {
|
||||
throw new ModuleDiscoveryServiceException("No proper call to SetupWebPage::AddModule found in module file", 0, null, $sModuleFilePath);
|
||||
}
|
||||
|
||||
private function BrowseArrayStructure(PhpParser\Node\Expr\Array_ $oArray, array &$aModuleConfig) : void
|
||||
{
|
||||
$iIndex=0;
|
||||
/** @var \PhpParser\Node\Expr\ArrayItem $oValue */
|
||||
foreach ($oArray->items as $oArrayItem){
|
||||
if ($oArrayItem->key instanceof PhpParser\Node\Scalar\String_) {
|
||||
//dictionnary
|
||||
$sKey = $oArrayItem->key->value;
|
||||
} else if ($oArrayItem->key instanceof \PhpParser\Node\Expr\ConstFetch) {
|
||||
$sKey = $this->EvaluateConstantExpression($oArrayItem->key);
|
||||
if (is_null($sKey)){
|
||||
continue;
|
||||
}
|
||||
}else {
|
||||
$sKey = $iIndex++;
|
||||
}
|
||||
|
||||
$oValue = $oArrayItem->value;
|
||||
|
||||
if ($oValue instanceof PhpParser\Node\Expr\Array_) {
|
||||
$aSubConfig=[];
|
||||
$this->BrowseArrayStructure($oValue, $aSubConfig);
|
||||
$aModuleConfig[$sKey]=$aSubConfig;
|
||||
}
|
||||
|
||||
if ($oValue instanceof PhpParser\Node\Scalar\String_||$oValue instanceof PhpParser\Node\Scalar\Int_) {
|
||||
$aModuleConfig[$sKey]=$oValue->value;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($oValue instanceof \PhpParser\Node\Expr\ConstFetch) {
|
||||
$oEvaluatedConstant = $this->EvaluateConstantExpression($oValue);
|
||||
$aModuleConfig[$sKey]= $oEvaluatedConstant;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sModuleFilePath
|
||||
* @param \PhpParser\Node\Expr\Assign $oAssignation
|
||||
* N°4789 - Parse datamodel module.xxx.php files instead of interpreting them
|
||||
* additional path added to handle ModuleInstallerAPI declaration during setup only
|
||||
* @param array &$aModuleInfo
|
||||
*
|
||||
* @return array|null
|
||||
* @throws \ModuleDiscoveryServiceException
|
||||
* @return void
|
||||
*/
|
||||
private function ParseCallToAddModuleAndReturnModuleConfiguration(string $sModuleFilePath, \PhpParser\Node\Stmt\Expression $oExpression) : ?array
|
||||
private function CompleteConfigWithModuleFilePath(array &$aModuleInfo)
|
||||
{
|
||||
/** @var Assign $oAssignation */
|
||||
$oAssignation = $oExpression->expr;
|
||||
if (false === ($oAssignation instanceof PhpParser\Node\Expr\StaticCall)) {
|
||||
return null;
|
||||
if (count($aModuleInfo)==3) {
|
||||
$aModuleInfo[2]['module_file_path'] = $aModuleInfo[0];
|
||||
}
|
||||
|
||||
/** @var PhpParser\Node\Expr\StaticCall $oAssignation */
|
||||
|
||||
if ("SetupWebPage" !== $oAssignation?->class?->name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ("AddModule" !== $oAssignation?->name?->name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$aArgs = $oAssignation?->args;
|
||||
if (count($aArgs) != 3) {
|
||||
throw new ModuleDiscoveryServiceException("Not enough parameters when calling SetupWebPage::AddModule", 0, null, $sModuleFilePath);
|
||||
}
|
||||
|
||||
$oModuleId = $aArgs[1];
|
||||
if (false === ($oModuleId instanceof PhpParser\Node\Arg)) {
|
||||
throw new ModuleDiscoveryServiceException("2nd parameter to SetupWebPage::AddModule call issue: " . get_class($oModuleId), 0, null, $sModuleFilePath);
|
||||
}
|
||||
|
||||
/** @var PhpParser\Node\Arg $oModuleId */
|
||||
if (false === ($oModuleId->value instanceof PhpParser\Node\Scalar\String_)) {
|
||||
throw new ModuleDiscoveryServiceException("2nd parameter to SetupWebPage::AddModule not a string: " . get_class($oModuleId->value), 0, null, $sModuleFilePath);
|
||||
}
|
||||
|
||||
/** @var PhpParser\Node\Scalar\String_ $sModuleIdStringObj */
|
||||
$sModuleIdStringObj = $oModuleId->value;
|
||||
$sModuleId = $sModuleIdStringObj->value;
|
||||
|
||||
$oModuleConfigInfo = $aArgs[2];
|
||||
if (false === ($oModuleConfigInfo instanceof PhpParser\Node\Arg)) {
|
||||
throw new ModuleDiscoveryServiceException("3rd parameter to SetupWebPage::AddModule call issue: " . get_class($oModuleConfigInfo), 0, null, $sModuleFilePath);
|
||||
}
|
||||
|
||||
/** @var PhpParser\Node\Arg $oModuleConfigInfo */
|
||||
if (false === ($oModuleConfigInfo->value instanceof PhpParser\Node\Expr\Array_)) {
|
||||
throw new ModuleDiscoveryServiceException("3rd parameter to SetupWebPage::AddModule not an array: " . get_class($oModuleConfigInfo->value), 0, null, $sModuleFilePath);
|
||||
}
|
||||
|
||||
$aModuleConfig=[];
|
||||
$this->BrowseArrayStructure($oModuleConfigInfo->value, $aModuleConfig);
|
||||
|
||||
if (! is_array($aModuleConfig)){
|
||||
throw new ModuleDiscoveryServiceException("3rd parameter to SetupWebPage::AddModule not an array: " . get_class($oModuleConfigInfo->value), 0, null, $sModuleFilePath);
|
||||
}
|
||||
return [
|
||||
$sModuleFilePath,
|
||||
$sModuleId,
|
||||
$aModuleConfig,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sModuleFilePath
|
||||
* @param \PhpParser\Node\Stmt\If_ $oNode
|
||||
*
|
||||
* @return array|null
|
||||
* @throws \ModuleDiscoveryServiceException
|
||||
*/
|
||||
private function BrowseIfStructure(string $sModuleFilePath, \PhpParser\Node\Stmt\If_ $oNode) : ?array
|
||||
{
|
||||
$bCondition = $this->EvaluateBooleanExpression($oNode->cond);
|
||||
if ($bCondition) {
|
||||
foreach ($oNode->stmts as $oSubNode) {
|
||||
if ($oSubNode instanceof \PhpParser\Node\Stmt\Expression) {
|
||||
$aModuleConfig = $this->ParseCallToAddModuleAndReturnModuleConfiguration($sModuleFilePath, $oSubNode);
|
||||
if (!is_null($aModuleConfig)) {
|
||||
return $aModuleConfig;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! is_null($oNode->elseifs)) {
|
||||
foreach ($oNode->elseifs as $oElseIfSubNode) {
|
||||
/** @var \PhpParser\Node\Stmt\ElseIf_ $oElseIfSubNode */
|
||||
$bCondition = $this->EvaluateBooleanExpression($oElseIfSubNode->cond);
|
||||
if ($bCondition) {
|
||||
$aModuleConfig = $this->ParseStatementsAndReturnModuleConfiguration($sModuleFilePath, $oElseIfSubNode->stmts);
|
||||
if (!is_null($aModuleConfig)) {
|
||||
return $aModuleConfig;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (! is_null($oNode->else)) {
|
||||
$aModuleConfig = $this->ParseStatementsAndReturnModuleConfiguration($sModuleFilePath, $oNode->else->stmts);
|
||||
|
||||
return $aModuleConfig;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private function ParseStatementsAndReturnModuleConfiguration(string $sModuleFilePath, array $aStmts) : ?array
|
||||
{
|
||||
foreach ($aStmts as $oSubNode) {
|
||||
if ($oSubNode instanceof \PhpParser\Node\Stmt\Expression) {
|
||||
$aModuleConfig = $this->ParseCallToAddModuleAndReturnModuleConfiguration($sModuleFilePath, $oSubNode);
|
||||
if (!is_null($aModuleConfig)) {
|
||||
return $aModuleConfig;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function EvaluateConstantExpression(\PhpParser\Node\Expr\ArrayItem|\PhpParser\Node\Expr\ConstFetch $oValue) : mixed
|
||||
{
|
||||
$bResult = false;
|
||||
try{
|
||||
@eval('$bResult = '.$oValue->name.';');
|
||||
} catch (Throwable $t) {
|
||||
throw new ModuleDiscoveryServiceException("Eval of ' . $oValue->name . ' caused an error: ".$t->getMessage());
|
||||
}
|
||||
|
||||
return $bResult;
|
||||
}
|
||||
|
||||
private function GetMixedValueForBooleanOperatorEvaluation(\PhpParser\Node\Expr $oExpr) : string
|
||||
{
|
||||
if ($oExpr instanceof \PhpParser\Node\Scalar\Int_ || $oExpr instanceof \PhpParser\Node\Scalar\Float_){
|
||||
return "" . $oExpr->value;
|
||||
}
|
||||
|
||||
return $this->EvaluateBooleanExpression($oExpr) ? "true" : "false";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sBooleanExpr
|
||||
*
|
||||
* @return bool
|
||||
* @throws ModuleDiscoveryServiceException
|
||||
*/
|
||||
public function UnprotectedComputeBooleanExpression(string $sBooleanExpr) : bool
|
||||
{
|
||||
$bResult = false;
|
||||
try{
|
||||
@eval('$bResult = '.$sBooleanExpr.';');
|
||||
} catch (Throwable $t) {
|
||||
throw new ModuleDiscoveryServiceException("Eval of '$sBooleanExpr' caused an error: ".$t->getMessage());
|
||||
}
|
||||
|
||||
return $bResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sBooleanExpr
|
||||
*
|
||||
* @return bool
|
||||
* @throws ModuleDiscoveryServiceException
|
||||
*/
|
||||
public function ComputeBooleanExpression(string $sBooleanExpr, $bProtected=true) : bool
|
||||
{
|
||||
if (! $bProtected){
|
||||
return $this->UnprotectedComputeBooleanExpression($sBooleanExpr);
|
||||
}
|
||||
|
||||
$sPhpContent = <<<PHP
|
||||
<?php
|
||||
$sBooleanExpr;
|
||||
PHP;
|
||||
try{
|
||||
$aNodes = ModuleDiscoveryService::GetInstance()->parsePhpCode($sPhpContent);
|
||||
$oExpr = $aNodes[0];
|
||||
return $this->EvaluateBooleanExpression($oExpr->expr);
|
||||
} catch (Throwable $t) {
|
||||
throw new ModuleDiscoveryServiceException("Eval of '$sBooleanExpr' caused an error:".$t->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function EvaluateBooleanExpression(\PhpParser\Node\Expr $oCondExpression) : bool
|
||||
{
|
||||
if ($oCondExpression instanceof \PhpParser\Node\Expr\BinaryOp){
|
||||
$sExpr = $this->GetMixedValueForBooleanOperatorEvaluation($oCondExpression->left)
|
||||
. " "
|
||||
. $oCondExpression->getOperatorSigil()
|
||||
. " "
|
||||
. $this->GetMixedValueForBooleanOperatorEvaluation($oCondExpression->right);
|
||||
return $this->ComputeBooleanExpression($sExpr, false);
|
||||
}
|
||||
|
||||
if ($oCondExpression instanceof \PhpParser\Node\Expr\BooleanNot){
|
||||
return ! $this->EvaluateBooleanExpression($oCondExpression->expr);
|
||||
}
|
||||
|
||||
if ($oCondExpression instanceof \PhpParser\Node\Expr\FuncCall){
|
||||
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);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function CallFunction(\PhpParser\Node\Expr\FuncCall $oFunct) : bool
|
||||
{
|
||||
$sFunction = $oFunct->name->name;
|
||||
$aWhiteList = ["function_exists"];
|
||||
if (! in_array($sFunction, $aWhiteList)){
|
||||
throw new ModuleDiscoveryServiceException("FuncCall $sFunction not supported");
|
||||
//return false;
|
||||
}
|
||||
|
||||
$aArgs=[];
|
||||
foreach ($oFunct->args as $arg){
|
||||
/** @var \PhpParser\Node\Arg $arg */
|
||||
$aArgs[]=$arg->value->value;
|
||||
}
|
||||
|
||||
$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
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param \Config $oConfig
|
||||
* @param array $aModuleInfo
|
||||
* @param array $aModuleConfig
|
||||
*
|
||||
* @return void
|
||||
* @throws \ModuleDiscoveryServiceException
|
||||
*/
|
||||
public function CallInstallerBeforeWritingConfigMethod(Config $oConfig, array $aModuleInfo)
|
||||
public function CallInstallerBeforeWritingConfigMethod(Config $oConfig, array $aModuleConfig)
|
||||
{
|
||||
if (isset($aModuleInfo['installer']))
|
||||
{
|
||||
$sModuleInstallerClass = $aModuleInfo['installer'];
|
||||
if (!class_exists($sModuleInstallerClass)) {
|
||||
$sModuleFilePath = $aModuleInfo['module_file_path'];
|
||||
$this->ReadModuleFileConfigurationLegacy($sModuleFilePath);
|
||||
}
|
||||
|
||||
if (!class_exists($sModuleInstallerClass))
|
||||
{
|
||||
throw new Exception("Wrong installer class: '$sModuleInstallerClass' is not a PHP class - Module: ".$aModuleInfo['label']);
|
||||
}
|
||||
if (!is_subclass_of($sModuleInstallerClass, 'ModuleInstallerAPI'))
|
||||
{
|
||||
throw new Exception("Wrong installer class: '$sModuleInstallerClass' is not derived from 'ModuleInstallerAPI' - Module: ".$aModuleInfo['label']);
|
||||
}
|
||||
$aCallSpec = array($sModuleInstallerClass, 'BeforeWritingConfig');
|
||||
call_user_func_array($aCallSpec, array($oConfig));
|
||||
$sModuleInstallerClass = $this->DeclareModuleInstallerAPI($aModuleConfig);
|
||||
if (is_null($sModuleInstallerClass)){
|
||||
return;
|
||||
}
|
||||
|
||||
$aCallSpec = [$sModuleInstallerClass, 'BeforeWritingConfig'];
|
||||
call_user_func_array($aCallSpec, [$oConfig]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the given handler method for all selected modules having an installation handler
|
||||
*
|
||||
* @param Config $oConfig
|
||||
* @param array $aModuleConfig
|
||||
* @param array $aModule
|
||||
* @param string $sHandlerName
|
||||
*
|
||||
* @throws CoreException
|
||||
*/
|
||||
public function CallInstallerHandler(Config $oConfig, array $aModuleConfig, array $aModule, $sHandlerName)
|
||||
{
|
||||
$sModuleInstallerClass = $this->DeclareModuleInstallerAPI($aModuleConfig);
|
||||
if (is_null($sModuleInstallerClass)){
|
||||
return;
|
||||
}
|
||||
|
||||
SetupLog::Info("Calling Module Handler: $sModuleInstallerClass::$sHandlerName(oConfig, {$aModule['version_db']}, {$aModule['version_code']})");
|
||||
$aCallSpec = [$sModuleInstallerClass, $sHandlerName];
|
||||
if (is_callable($aCallSpec))
|
||||
{
|
||||
try {
|
||||
call_user_func_array($aCallSpec, [MetaModel::GetConfig(), $aModule['version_db'], $aModule['version_code']]);
|
||||
} catch (Exception $e) {
|
||||
$sErrorMessage = "Module $sModuleId : error when calling module installer class $sModuleInstallerClass for $sHandlerName handler";
|
||||
$aExceptionContextData = [
|
||||
'ModulelId' => $sModuleId,
|
||||
'ModuleInstallerClass' => $sModuleInstallerClass,
|
||||
'ModuleInstallerHandler' => $sHandlerName,
|
||||
'ExceptionClass' => get_class($e),
|
||||
'ExceptionMessage' => $e->getMessage(),
|
||||
];
|
||||
throw new CoreException($sErrorMessage, $aExceptionContextData, '', $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function DeclareModuleInstallerAPI($aModuleConfig) : ?string
|
||||
{
|
||||
if (! isset($aModuleConfig['installer'])){
|
||||
return null;
|
||||
}
|
||||
|
||||
$sModuleInstallerClass = $aModuleConfig['installer'];
|
||||
if (!class_exists($sModuleInstallerClass)) {
|
||||
$sModuleFilePath = $aModuleConfig['module_file_path'];
|
||||
$this->ReadModuleFileConfigurationLegacy($sModuleFilePath);
|
||||
}
|
||||
|
||||
if (!class_exists($sModuleInstallerClass))
|
||||
{
|
||||
throw new CoreException("Wrong installer class: '$sModuleInstallerClass' is not a PHP class - Module: ".$aModuleConfig['label']);
|
||||
}
|
||||
if (!is_subclass_of($sModuleInstallerClass, 'ModuleInstallerAPI'))
|
||||
{
|
||||
throw new CoreException("Wrong installer class: '$sModuleInstallerClass' is not derived from 'ModuleInstallerAPI' - Module: ".$aModuleConfig['label']);
|
||||
}
|
||||
|
||||
return $sModuleInstallerClass;
|
||||
}
|
||||
}
|
||||
|
||||
class ModuleDiscoveryServiceException extends Exception
|
||||
{
|
||||
/**
|
||||
* ModuleDiscoveryServiceException constructor.
|
||||
*
|
||||
* @param string $sMessage
|
||||
* @param int $iHttpCode
|
||||
* @param Exception|null $oPrevious
|
||||
*/
|
||||
public function __construct($sMessage, $iHttpCode = 0, Exception $oPrevious = null, $sModuleFile=null)
|
||||
{
|
||||
$e = new \Exception("");
|
||||
|
||||
$aContext = ['previous' => $oPrevious?->getMessage(), 'stack' => $e->getTraceAsString()];
|
||||
if (! is_null($sModuleFile)){
|
||||
$aContext['module_file'] = $sModuleFile;
|
||||
}
|
||||
SetupLog::Warning($sMessage, null, $aContext);
|
||||
parent::__construct($sMessage, $iHttpCode, $oPrevious);
|
||||
}
|
||||
}
|
||||
23
setup/modulediscovery/ModuleDiscoveryServiceException.php
Normal file
23
setup/modulediscovery/ModuleDiscoveryServiceException.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
class ModuleDiscoveryServiceException extends Exception
|
||||
{
|
||||
/**
|
||||
* ModuleDiscoveryServiceException constructor.
|
||||
*
|
||||
* @param string $sMessage
|
||||
* @param int $iHttpCode
|
||||
* @param Exception|null $oPrevious
|
||||
*/
|
||||
public function __construct($sMessage, $iHttpCode = 0, Exception $oPrevious = null, $sModuleFile = null)
|
||||
{
|
||||
$e = new \Exception("");
|
||||
|
||||
$aContext = ['previous' => $oPrevious?->getMessage(), 'stack' => $e->getTraceAsString()];
|
||||
if (!is_null($sModuleFile)) {
|
||||
$aContext['module_file'] = $sModuleFile;
|
||||
}
|
||||
SetupLog::Warning($sMessage, null, $aContext);
|
||||
parent::__construct($sMessage, $iHttpCode, $oPrevious);
|
||||
}
|
||||
}
|
||||
@@ -459,7 +459,7 @@ class RunTimeEnvironment
|
||||
{
|
||||
SetupInfo::SetSelectedModules($aRet);
|
||||
try{
|
||||
$bSelected = ModuleDiscoveryService::GetInstance()->ComputeBooleanExpression($oModule->GetAutoSelect());
|
||||
$bSelected = ModuleDiscoveryEvaluationService::GetInstance()->EvaluateBooleanExpression($oModule->GetAutoSelect());
|
||||
if ($bSelected)
|
||||
{
|
||||
$aRet[$oModule->GetName()] = $oModule; // store the Id of the selected module
|
||||
@@ -1087,28 +1087,9 @@ class RunTimeEnvironment
|
||||
{
|
||||
foreach($aAvailableModules as $sModuleId => $aModule)
|
||||
{
|
||||
if (($sModuleId != ROOT_MODULE) && in_array($sModuleId, $aSelectedModules) &&
|
||||
isset($aAvailableModules[$sModuleId]['installer']) )
|
||||
if (($sModuleId != ROOT_MODULE) && in_array($sModuleId, $aSelectedModules))
|
||||
{
|
||||
$sModuleInstallerClass = $aAvailableModules[$sModuleId]['installer'];
|
||||
SetupLog::Info("Calling Module Handler: $sModuleInstallerClass::$sHandlerName(oConfig, {$aModule['version_db']}, {$aModule['version_code']})");
|
||||
$aCallSpec = array($sModuleInstallerClass, $sHandlerName);
|
||||
if (is_callable($aCallSpec))
|
||||
{
|
||||
try {
|
||||
call_user_func_array($aCallSpec, array(MetaModel::GetConfig(), $aModule['version_db'], $aModule['version_code']));
|
||||
} catch (Exception $e) {
|
||||
$sErrorMessage = "Module $sModuleId : error when calling module installer class $sModuleInstallerClass for $sHandlerName handler";
|
||||
$aExceptionContextData = [
|
||||
'ModulelId' => $sModuleId,
|
||||
'ModuleInstallerClass' => $sModuleInstallerClass,
|
||||
'ModuleInstallerHandler' => $sHandlerName,
|
||||
'ExceptionClass' => get_class($e),
|
||||
'ExceptionMessage' => $e->getMessage(),
|
||||
];
|
||||
throw new CoreException($sErrorMessage, $aExceptionContextData, '', $e);
|
||||
}
|
||||
}
|
||||
ModuleDiscoveryService::GetInstance()->CallInstallerHandler(MetaModel::GetConfig(), $aAvailableModules[$sModuleId], $aModule, $sHandlerName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,7 +270,7 @@ class InstallationFileService {
|
||||
{
|
||||
try {
|
||||
SetupInfo::SetSelectedModules($this->aSelectedModules);
|
||||
$bSelected = ModuleDiscoveryService::GetInstance()->ComputeBooleanExpression($aModule['auto_select']);
|
||||
$bSelected = ModuleDiscoveryEvaluationService::GetInstance()->EvaluateBooleanExpression($aModule['auto_select']);
|
||||
if ($bSelected)
|
||||
{
|
||||
// Modules in data/production-modules/ are considered as mandatory and always installed
|
||||
|
||||
@@ -1787,7 +1787,7 @@ EOF
|
||||
// Check the module selection
|
||||
try {
|
||||
SetupInfo::SetSelectedModules($aModules);
|
||||
$bSelected = ModuleDiscoveryService::GetInstance()->ComputeBooleanExpression($aInfo['auto_select']);
|
||||
$bSelected = ModuleDiscoveryEvaluationService::GetInstance()->EvaluateBooleanExpression($aInfo['auto_select']);
|
||||
}
|
||||
catch (ModuleDiscoveryServiceException $e) {
|
||||
//logged already
|
||||
@@ -1865,7 +1865,7 @@ EOF
|
||||
try
|
||||
{
|
||||
SetupInfo::SetSelectedModules($aModules);
|
||||
$bSelected = ModuleDiscoveryService::GetInstance()->ComputeBooleanExpression($aModule['auto_select']);
|
||||
$bSelected = ModuleDiscoveryEvaluationService::GetInstance()->EvaluateBooleanExpression($aModule['auto_select']);
|
||||
if ($bSelected)
|
||||
{
|
||||
$aModules[$sModuleId] = true; // store the Id of the selected module
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
namespace Combodo\iTop\Test\UnitTest\Setup\ModuleDiscovery;
|
||||
|
||||
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
|
||||
use ModuleDiscoveryEvaluationService;
|
||||
use ModuleDiscoveryService;
|
||||
use PhpParser\ParserFactory;
|
||||
|
||||
class ModuleDiscoveryEvaluationServiceTest extends ItopDataTestCase
|
||||
{
|
||||
private string $sTempModuleFilePath;
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->RequireOnceItopFile('setup/modulediscovery/ModuleDiscoveryService.php');
|
||||
}
|
||||
|
||||
public static function EvaluateBooleanExpressionProvider()
|
||||
{
|
||||
return [
|
||||
"true" => [ "expr" => "true", "expected" => true],
|
||||
"(true)" => [ "expr" => "(true)", "expected" => true],
|
||||
"(false||true)" => [ "expr" => "(false||true)", "expected" => true],
|
||||
"false" => [ "expr" => "false", "expected" => false],
|
||||
"(false)" => [ "expr" => "(false)", "expected" => false],
|
||||
"(false&&true)" => [ "expr" => "(false&&true)", "expected" => false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider EvaluateBooleanExpressionProvider
|
||||
*/
|
||||
public function testEvaluateBooleanExpression(string $sBooleanExpression, bool $expected){
|
||||
$this->assertEquals($expected, ModuleDiscoveryEvaluationService::GetInstance()->EvaluateBooleanExpression($sBooleanExpression), $sBooleanExpression);
|
||||
}
|
||||
|
||||
public function testEvaluateBooleanExpression_BrokenBooleanExpression(){
|
||||
$this->expectException(\ModuleDiscoveryServiceException::class);
|
||||
$this->expectExceptionMessage('Eval of \'(a || true)\' caused an error');
|
||||
$this->assertTrue(ModuleDiscoveryEvaluationService::GetInstance()->EvaluateBooleanExpression("(a || true)"));
|
||||
}
|
||||
|
||||
|
||||
public static function EvaluateBooleanExpressionAutoselectProvider()
|
||||
{
|
||||
$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 EvaluateBooleanExpressionAutoselectProvider
|
||||
*/
|
||||
public function testEvaluateBooleanExpression_Autoselect(string $sBooleanExpression, bool $expected){
|
||||
\SetupInfo::SetSelectedModules(["itop-storage-mgmt" => "123"]);
|
||||
$this->assertEquals($expected, ModuleDiscoveryEvaluationService::GetInstance()->EvaluateBooleanExpression($sBooleanExpression), $sBooleanExpression);
|
||||
}
|
||||
|
||||
public function testEvaluateConstantExpression()
|
||||
{
|
||||
$sPHP = <<<PHP
|
||||
<?php
|
||||
APPROOT;
|
||||
PHP;
|
||||
$aNodes = ModuleDiscoveryEvaluationService::GetInstance()->ParsePhpCode($sPHP);
|
||||
/** @var \PhpParser\Node\Expr $oExpr */
|
||||
$oExpr = $aNodes[0];
|
||||
$val = $this->InvokeNonPublicMethod(ModuleDiscoveryEvaluationService::class, "EvaluateConstantExpression", ModuleDiscoveryEvaluationService::GetInstance(), [$oExpr->expr]);
|
||||
$this->assertEquals(APPROOT, $val);
|
||||
}
|
||||
|
||||
public static function EvaluateExpressionBooleanProvider() {
|
||||
$sTruePHP = <<<PHP
|
||||
<?php
|
||||
if (COND){
|
||||
echo "toto";
|
||||
}
|
||||
PHP;
|
||||
|
||||
return [
|
||||
"true" => [
|
||||
"code" => str_replace("COND", "true", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"false" => [
|
||||
"code" => str_replace("COND", "false", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"not ok" => [
|
||||
"code" => str_replace("COND", "! false", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"not ko" => [
|
||||
"code" => str_replace("COND", "! (true)", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"AND ko" => [
|
||||
"code" => str_replace("COND", "true && false", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"AND ok1" => [
|
||||
"code" => str_replace("COND", "true && true", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"AND ko2" => [
|
||||
"code" => str_replace("COND", "true && true && false", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"OR ko" => [
|
||||
"code" => str_replace("COND", "false || false", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"OR ok" => [
|
||||
"code" => str_replace("COND", "false ||true", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"OR ok2" => [
|
||||
"code" => str_replace("COND", "false ||false||true", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"function_exists('ldap_connect')" => [
|
||||
"code" => str_replace("COND", "function_exists('ldap_connect')", $sTruePHP),
|
||||
"bool_expected" => function_exists('ldap_connect'),
|
||||
|
||||
],
|
||||
"function_exists('gabuzomeushouldnotexist')" => [
|
||||
"code" => str_replace("COND", "function_exists('gabuzomeushouldnotexist')", $sTruePHP),
|
||||
"bool_expected" => function_exists('gabuzomeushouldnotexist'),
|
||||
|
||||
],
|
||||
"1 > 2" => [
|
||||
"code" => str_replace("COND", "1 > 2", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"1 == 1" => [
|
||||
"code" => str_replace("COND", "1 == 1", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"1 < 2" => [
|
||||
"code" => str_replace("COND", "1 < 2", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
],
|
||||
"PHP_VERSION_ID == PHP_VERSION_ID" => [
|
||||
"code" => str_replace("COND", "PHP_VERSION_ID == PHP_VERSION_ID", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
],
|
||||
"PHP_VERSION_ID != PHP_VERSION_ID" => [
|
||||
"code" => str_replace("COND", "PHP_VERSION_ID != PHP_VERSION_ID", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider EvaluateExpressionBooleanProvider
|
||||
*/
|
||||
public function testEvaluateExpression($sPHP, $bExpected)
|
||||
{
|
||||
$aNodes = ModuleDiscoveryEvaluationService::GetInstance()->ParsePhpCode($sPHP);
|
||||
/** @var \PhpParser\Node\Expr $oExpr */
|
||||
$oExpr = $aNodes[0];
|
||||
$val = $this->InvokeNonPublicMethod(ModuleDiscoveryEvaluationService::class, "EvaluateExpression", ModuleDiscoveryEvaluationService::GetInstance(), [$oExpr->cond]);
|
||||
$this->assertEquals($bExpected, $val);
|
||||
}
|
||||
}
|
||||
@@ -72,82 +72,10 @@ class ModuleDiscoveryServiceTest extends ItopDataTestCase
|
||||
}
|
||||
|
||||
|
||||
public static function ComputeBooleanExpressionProvider()
|
||||
{
|
||||
return [
|
||||
"true" => [ "expr" => "true", "expected" => true],
|
||||
"(true)" => [ "expr" => "(true)", "expected" => true],
|
||||
"(false||true)" => [ "expr" => "(false||true)", "expected" => true],
|
||||
"false" => [ "expr" => "false", "expected" => false],
|
||||
"(false)" => [ "expr" => "(false)", "expected" => false],
|
||||
"(false&&true)" => [ "expr" => "(false&&true)", "expected" => false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider ComputeBooleanExpressionProvider
|
||||
* local tool function
|
||||
*/
|
||||
public function testComputeBooleanExpression(string $sBooleanExpression, bool $expected){
|
||||
$this->assertEquals($expected, ModuleDiscoveryService::GetInstance()->ComputeBooleanExpression($sBooleanExpression), $sBooleanExpression);
|
||||
}
|
||||
|
||||
public function testComputeBooleanExpression_BrokenBooleanExpression(){
|
||||
$this->expectException(\ModuleDiscoveryServiceException::class);
|
||||
$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
|
||||
<?php
|
||||
APPROOT;
|
||||
PHP;
|
||||
$aNodes = ModuleDiscoveryService::GetInstance()->parsePhpCode($sPHP);
|
||||
/** @var \PhpParser\Node\Expr $oExpr */
|
||||
$oExpr = $aNodes[0];
|
||||
$val = $this->InvokeNonPublicMethod(ModuleDiscoveryService::class, "EvaluateConstantExpression", ModuleDiscoveryService::GetInstance(), [$oExpr->expr]);
|
||||
$this->assertEquals(APPROOT, $val);
|
||||
}
|
||||
|
||||
public function CallReadModuleFileConfiguration($sPHpCode)
|
||||
private function CallReadModuleFileConfiguration($sPHpCode)
|
||||
{
|
||||
$this->sTempModuleFilePath = tempnam(__DIR__, "test");
|
||||
file_put_contents($this->sTempModuleFilePath, $sPHpCode);
|
||||
@@ -274,112 +202,6 @@ PHP;
|
||||
$this->assertEquals([$this->sTempModuleFilePath, "elseif2", ["c" => "d", 'module_file_path' => $this->sTempModuleFilePath]], $val);
|
||||
}
|
||||
|
||||
public static function EvaluateExpressionBooleanProvider() {
|
||||
$sTruePHP = <<<PHP
|
||||
<?php
|
||||
if (COND){
|
||||
echo "toto";
|
||||
}
|
||||
PHP;
|
||||
|
||||
return [
|
||||
"true" => [
|
||||
"code" => str_replace("COND", "true", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"false" => [
|
||||
"code" => str_replace("COND", "false", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"not ok" => [
|
||||
"code" => str_replace("COND", "! false", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"not ko" => [
|
||||
"code" => str_replace("COND", "! (true)", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"AND ko" => [
|
||||
"code" => str_replace("COND", "true && false", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"AND ok1" => [
|
||||
"code" => str_replace("COND", "true && true", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"AND ko2" => [
|
||||
"code" => str_replace("COND", "true && true && false", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"OR ko" => [
|
||||
"code" => str_replace("COND", "false || false", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"OR ok" => [
|
||||
"code" => str_replace("COND", "false ||true", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"OR ok2" => [
|
||||
"code" => str_replace("COND", "false ||false||true", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"function_exists('ldap_connect')" => [
|
||||
"code" => str_replace("COND", "function_exists('ldap_connect')", $sTruePHP),
|
||||
"bool_expected" => function_exists('ldap_connect'),
|
||||
|
||||
],
|
||||
"function_exists('gabuzomeushouldnotexist')" => [
|
||||
"code" => str_replace("COND", "function_exists('gabuzomeushouldnotexist')", $sTruePHP),
|
||||
"bool_expected" => function_exists('gabuzomeushouldnotexist'),
|
||||
|
||||
],
|
||||
"1 > 2" => [
|
||||
"code" => str_replace("COND", "1 > 2", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
|
||||
],
|
||||
"1 == 1" => [
|
||||
"code" => str_replace("COND", "1 == 1", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
|
||||
],
|
||||
"1 < 2" => [
|
||||
"code" => str_replace("COND", "1 < 2", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
],
|
||||
"PHP_VERSION_ID == PHP_VERSION_ID" => [
|
||||
"code" => str_replace("COND", "PHP_VERSION_ID == PHP_VERSION_ID", $sTruePHP),
|
||||
"bool_expected" => true,
|
||||
],
|
||||
"PHP_VERSION_ID != PHP_VERSION_ID" => [
|
||||
"code" => str_replace("COND", "PHP_VERSION_ID != PHP_VERSION_ID", $sTruePHP),
|
||||
"bool_expected" => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider EvaluateExpressionBooleanProvider
|
||||
*/
|
||||
public function testEvaluateExpressionBoolean($sPHP, $bExpected)
|
||||
{
|
||||
$aNodes = ModuleDiscoveryService::GetInstance()->parsePhpCode($sPHP);
|
||||
/** @var \PhpParser\Node\Expr $oExpr */
|
||||
$oExpr = $aNodes[0];
|
||||
$val = $this->InvokeNonPublicMethod(ModuleDiscoveryService::class, "EvaluateBooleanExpression", ModuleDiscoveryService::GetInstance(), [$oExpr->cond]);
|
||||
$this->assertEquals($bExpected, $val);
|
||||
}
|
||||
|
||||
public function testCallDeclaredInstaller()
|
||||
{
|
||||
$sModuleInstallerClass = "TicketsInstaller" . uniqid();
|
||||
|
||||
Reference in New Issue
Block a user