mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
N°4789 - Parse datamodel module.xxx.php files instead of interpreting them (#746)
* N°4789 - Parse datamodel module.xxx.php files instead of interpreting them - refactoring all in a dedicated service first * N°4789 - fix broken setup + tests * N°4789 - replace legacy eval by module file parsing * N°4789 - handle constants and if conditional structures * N°4789 - compute boolean expressions * N°4789 - make autoselect and dependencies work as well * cleanup * N°4789 - fix BeforeWritingConfig calls during setup * N°4789 - refactor and split in ModuleDiscoveryEvaluationService + handle ModuleInstallerAPI methods calls during setup * N°4789 - PR review changes with Romain * PR review + code cleanup + added usecases and test cover * temp evaluation work * replace eval by iTop custom evaluation classes * move PhpParser/Evaluation classes in a specific namespave + composer dumpautoload * fix broken setup * fix broken setup * complete Evaluators list + autoload * cleanup useless testing resources * cleanup + replace last eval call in VariableEvaluator * fix few Evaluators code * enhance nikic evaluators + test with/without nikic lib * Evaluator fixes/enhancements + tests * bump to nikic fork temporarly * bump nikic-parser fork + use only nikic fork evaluation + cleanup itop redondant evaluators * review with Romain: use distinct whitelists in setup time/runtime + move ModuleFileParser internal logic into ModuleFileReader * PhpExpressionEvaluator used via constructor and not as a service * dumpautoload again after rebase
This commit is contained in:
56
sources/PhpParser/Evaluation/PhpExpressionEvaluator.php
Normal file
56
sources/PhpParser/Evaluation/PhpExpressionEvaluator.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Combodo\iTop\PhpParser\Evaluation;
|
||||
|
||||
use ModuleFileParser;
|
||||
use ModuleFileReaderException;
|
||||
use PhpParser\ConstExprEvaluator;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\ParserFactory;
|
||||
|
||||
/**
|
||||
* Used at runtime/setup time
|
||||
*/
|
||||
class PhpExpressionEvaluator {
|
||||
|
||||
/** @var ConstExprEvaluator $oConstExprEvaluator */
|
||||
private $oConstExprEvaluator;
|
||||
|
||||
public function __construct(array $functionsWhiteList=[], array $staticCallsWhitelist=[]) {
|
||||
$this->oConstExprEvaluator = new ConstExprEvaluator();
|
||||
$this->oConstExprEvaluator->setStaticcallsWhitelist($staticCallsWhitelist);
|
||||
$this->oConstExprEvaluator->setFunctionsWhitelist($functionsWhiteList);
|
||||
}
|
||||
|
||||
public function EvaluateExpression(Expr $oExpression) : mixed
|
||||
{
|
||||
return $this->oConstExprEvaluator->evaluateDirectly($oExpression);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sBooleanExpr
|
||||
*
|
||||
* @return bool
|
||||
* @throws \ModuleFileReaderException
|
||||
*/
|
||||
public function ParseAndEvaluateBooleanExpression(string $sBooleanExpr) : bool
|
||||
{
|
||||
return $this->ParseAndEvaluateExpression($sBooleanExpr);
|
||||
}
|
||||
|
||||
public function ParseAndEvaluateExpression(string $sExpr) : mixed
|
||||
{
|
||||
$sPhpContent = <<<PHP
|
||||
<?php
|
||||
$sExpr;
|
||||
PHP;
|
||||
try{
|
||||
$oParser = (new ParserFactory())->createForNewestSupportedVersion();
|
||||
$aNodes = $oParser->parse($sPhpContent);
|
||||
$oExpr = $aNodes[0];
|
||||
return $this->EvaluateExpression($oExpr->expr);
|
||||
} catch (\Throwable $t) {
|
||||
throw new ModuleFileReaderException("Eval of '$sExpr' caused an error:".$t->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user