move PhpParser/Evaluation classes in a specific namespave + composer dumpautoload

This commit is contained in:
odain
2025-09-02 19:19:56 +02:00
parent 1962cd7a88
commit ac2b787e09
33 changed files with 92 additions and 42 deletions

View File

@@ -1,11 +1,10 @@
<?php
use evaluation\expression\PhpExpressionEvaluator;
use PhpParser\Node\Expr;
use Combodo\iTop\PhpParser\Evaluation\PhpExpressionEvaluator;
use PhpParser\Node\Expr\Assign;
use PhpParser\ParserFactory;
require_once APPROOT . 'sources/PhpParser/Evaluation/PhpExpressionEvaluator.php';
require_once __DIR__ . "/evaluation/expression/PhpExpressionEvaluator.php";
class ModuleFileParser {
private static ModuleFileParser $oInstance;

View File

@@ -1,8 +1,5 @@
<?php
use PhpParser\ParserFactory;
use PhpParser\Node\Expr\Assign;
require_once __DIR__ . '/ModuleFileParser.php';
require_once __DIR__ . '/ModuleFileReaderException.php';

View File

@@ -1,28 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
class ArrayDimFetchEvaluator implements iExprEvaluator {
public function GetHandledExpressionType(): string {
return ArrayDimFetch::class;
}
public function Evaluate(Expr $oExpr): mixed {
/** @var ArrayDimFetch $oExpr */
$var = PhpExpressionEvaluator::GetInstance()->EvaluateExpression($oExpr->var);
if (is_null($var)){
return null;
}
$dim = PhpExpressionEvaluator::GetInstance()->EvaluateExpression($oExpr->dim);
if (is_null($var)){
return $dim;
}
return $var[$dim] ?? null;
}
}

View File

@@ -1,48 +0,0 @@
<?php
namespace evaluation\expression;
use ModuleFileReaderException;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
class ArrayEvaluator implements iExprEvaluator {
public function GetHandledExpressionType(): string {
return Array_::class;
}
public function Evaluate(Expr $oExpr): mixed {
/** @var Array_ $oExpr */
$iIndex=0;
$aModuleInformation=[];
/** @var \PhpParser\Node\Expr\ArrayItem $oValue */
foreach ($oExpr->items as $oArrayItem){
if ($oArrayItem->key instanceof Int_||$oArrayItem->key instanceof String_||$oArrayItem->key instanceof ConstFetch) {
//dictionnary
$sKey = PhpExpressionEvaluator::GetInstance()->EvaluateExpression($oArrayItem->key);
if (is_null($sKey)){
continue;
}
} else {
//array
$sKey = $iIndex++;
}
try {
$oValue = $oArrayItem->value;
$oEvaluatuedValue = PhpExpressionEvaluator::GetInstance()->EvaluateExpression($oValue);
$aModuleInformation[$sKey]=$oEvaluatuedValue;
} catch(ModuleFileReaderException $e){
//required to support legacy below dump dependency
//'dependencies' => ['itop-config-mgmt/2.0.0'||'itop-structure/3.0.0']
continue;
}
}
return $aModuleInformation;
}
}

View File

@@ -1,18 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
abstract class BinaryOpEvaluator implements iExprEvaluator {
abstract function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed;
public function Evaluate(Expr $oExpr): mixed {
/** @var BinaryOp $oExpr */
return $this->EvaluateBinaryOperation(
PhpExpressionEvaluator::GetInstance()->EvaluateExpression($oExpr->left),
PhpExpressionEvaluator::GetInstance()->EvaluateExpression($oExpr->right));
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\BitwiseAnd;
class BitwiseAndEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return BitwiseAnd::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left & $right;
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
class BitwiseOrEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return BitwiseOr::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left | $right;
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
class BooleanAndEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return BooleanAnd::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left && $right;
}
}

View File

@@ -1,18 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BooleanNot;
class BooleanNotEvaluator implements iExprEvaluator {
public function GetHandledExpressionType(): string {
return BooleanNot::class;
}
public function Evaluate(Expr $oExpr): mixed {
/** @var BooleanNot $oExpr */
return ! PhpExpressionEvaluator::GetInstance()->EvaluateExpression($oExpr->expr);
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
class BooleanOrEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return BooleanOr::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left || $right;
}
}

View File

@@ -1,35 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
class ClassConstFetchEvaluator implements iExprEvaluator {
public function GetHandledExpressionType(): string {
return ClassConstFetch::class;
}
public function Evaluate(Expr $oExpr): mixed {
/** @var ClassConstFetch $oExpr */
$sClassName = $oExpr->class->name;
$sProperty = $oExpr->name->name;
if (class_exists($sClassName)){
$class = new \ReflectionClass($sClassName);
if (array_key_exists($sProperty, $class->getConstants())) {
$oReflectionConstant = $class->getReflectionConstant($sProperty);
if ($oReflectionConstant->isPublic()){
return $class->getConstant($sProperty);
}
}
}
if ('class' === $sProperty){
return $sClassName;
}
return null;
}
}

View File

@@ -1,28 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\Concat;
class ConcatEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return Concat::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
if (is_null($left) && is_null($right)){
return null;
}
if (is_null($left)){
return $right;
}
if (is_null($right)){
return $left;
}
return "$left" . "$right";
}
}

View File

@@ -1,21 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ConstFetch;
class ConstFetchEvaluator implements iExprEvaluator {
public function GetHandledExpressionType(): string {
return ConstFetch::class;
}
public function Evaluate(Expr $oExpr): mixed {
/** @var ConstFetch $oExpr */
if (defined($oExpr->name)){
return constant($oExpr->name);
}
return null;
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\Equal;
class EqualEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return Equal::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left == $right;
}
}

View File

@@ -1,33 +0,0 @@
<?php
namespace evaluation\expression;
use ModuleFileReaderException;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use ReflectionFunction;
class FuncCallEvaluator implements iExprEvaluator {
public function GetHandledExpressionType(): string {
return FuncCall::class;
}
public function Evaluate(Expr $oExpr): mixed {
/** @var FuncCall $oExpr */
$sFunction = $oExpr->name->name;
$aWhiteList = ["function_exists", "class_exists", "method_exists"];
if (! in_array($sFunction, $aWhiteList)){
throw new ModuleFileReaderException("FuncCall $sFunction not supported");
}
$aArgs=[];
foreach ($oExpr->args as $arg){
/** @var \PhpParser\Node\Arg $arg */
$aArgs[]=$arg->value->value;
}
$oReflectionFunction = new ReflectionFunction($sFunction);
return $oReflectionFunction->invoke(...$aArgs);
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\Greater;
class GreaterEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return Greater::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left > $right;
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
class GreaterOrEqualEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return GreaterOrEqual::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left >= $right;
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\Mul;
class MulEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return Mul::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left * $right;
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
class NotEqualEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return NotEqual::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left != $right;
}
}

View File

@@ -1,80 +0,0 @@
<?php
#Combodo\\iTop\\
namespace evaluation\expression;
use PhpParser\Node\Expr;
require_once __DIR__ . '/iExprEvaluator.php';
class PhpExpressionEvaluator {
private static PhpExpressionEvaluator $oInstance;
/** @var iExprEvaluator[] $aPhpParserEvaluators */
private static array $aPhpParserEvaluators;
protected function __construct() {
}
final public static function GetInstance(): PhpExpressionEvaluator {
if (!isset(static::$oInstance)) {
static::$oInstance = new static();
static::$aPhpParserEvaluators=[];
foreach (glob(__DIR__ . "/**Evaluator.php") as $sFile){
require_once $sFile;
$sNamespace = 'evaluation\\expression\\';
$sClass = $sNamespace. str_replace(".php", "", basename($sFile));
$oReflectionClass = new \ReflectionClass($sClass);
if ($oReflectionClass->isInstantiable()
&& $oReflectionClass->implementsInterface("{$sNamespace}iExprEvaluator")){
$oClass = new $sClass;
static::$aPhpParserEvaluators[$oClass->GetHandledExpressionType()] = $oClass;
}
}
}
return static::$oInstance;
}
final public static function SetInstance(?PhpExpressionEvaluator $oInstance): void {
static::$oInstance = $oInstance;
}
public function EvaluateExpression(Expr $oExpression) : mixed
{
$sClass = get_class($oExpression);
$oPhpParserEvaluator = static::$aPhpParserEvaluators[$sClass] ?? null;
if (is_null($oPhpParserEvaluator)){
return $oExpression->value;
}
return $oPhpParserEvaluator->Evaluate($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{
$aNodes = \ModuleFileParser::GetInstance()->ParsePhpCode($sPhpContent);
$oExpr = $aNodes[0];
return $this->EvaluateExpression($oExpr->expr);
} catch (\Throwable $t) {
throw new \ModuleFileReaderException("Eval of '$sExpr' caused an error:".$t->getMessage());
}
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\Smaller;
class SmallerEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return Smaller::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left < $right;
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
class SmallerOrEqualEvaluator extends BinaryOpEvaluator {
public function GetHandledExpressionType(): string {
return SmallerOrEqual::class;
}
function EvaluateBinaryOperation(mixed $left, mixed $right) : mixed
{
return $left <= $right;
}
}

View File

@@ -1,41 +0,0 @@
<?php
namespace evaluation\expression;
use ModuleFileReaderException;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\StaticCall;
use ReflectionFunction;
class StaticCallEvaluator implements iExprEvaluator {
public function GetHandledExpressionType(): string {
return StaticCall::class;
}
public function Evaluate(Expr $oExpr): mixed {
/** @var StaticCall $oExpr */
$sClassName = $oExpr->class->name;
$sMethodName = $oExpr->name->name;
$aWhiteList = ["SetupInfo::ModuleIsSelected", "utils::GetItopVersionWikiSyntax"];
$sStaticCallDescription = "$sClassName::$sMethodName";
if (! in_array($sStaticCallDescription, $aWhiteList)){
throw new ModuleFileReaderException("StaticCall $sStaticCallDescription not supported");
}
$aArgs=[];
foreach ($oExpr->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 ModuleFileReaderException("StaticCall $sStaticCallDescription not public");
}
return $method->invokeArgs(null, $aArgs);
}
}

View File

@@ -1,31 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\StaticPropertyFetch;
class StaticPropertyFetchEvaluator implements iExprEvaluator {
public function GetHandledExpressionType(): string {
return StaticPropertyFetch::class;
}
public function Evaluate(Expr $oExpr): mixed {
/** @var StaticPropertyFetch $oExpr */
$sClassName = $oExpr->class->name;
$sProperty = $oExpr->name->name;
if (class_exists($sClassName)){
$class = new \ReflectionClass($sClassName);
if (array_key_exists($sProperty, $class->getStaticProperties())) {
$oReflectionProperty = $class->getProperty($sProperty);
if ($oReflectionProperty->isPublic()){
return $class->getStaticPropertyValue($sProperty);
}
}
}
return null;
}
}

View File

@@ -1,19 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\UnaryMinus;
class UnaryMinusEvaluator implements iExprEvaluator {
public function GetHandledExpressionType(): string {
return UnaryMinus::class;
}
public function Evaluate(Expr $oExpr): mixed {
/** @var UnaryMinus $oExpr */
return - PhpExpressionEvaluator::GetInstance()->EvaluateExpression($oExpr->expr);
}
}

View File

@@ -1,32 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Variable;
class VariableEvaluator implements iExprEvaluator {
public function GetHandledExpressionType(): string {
return Variable::class;
}
public function Evaluate(Expr $oExpr): mixed {
/** @var Variable $oExpr */
if (is_null($oExpr->name)){
return null;
}
if (! isset($oExpr->name)) {
return null;
}
$sVarname=$oExpr->name;
$bResult = null;
@eval('$bResult = $'.$sVarname.';');
return $bResult;
}
}

View File

@@ -1,11 +0,0 @@
<?php
namespace evaluation\expression;
use PhpParser\Node\Expr;
interface iExprEvaluator {
public function GetHandledExpressionType(): string;
public function Evaluate(Expr $oExpr) : mixed;
}