N°4789 - PR review changes with Romain

This commit is contained in:
odain
2025-08-27 21:45:56 +02:00
parent 61c2b71f1f
commit 08c77f8106
12 changed files with 291 additions and 312 deletions

View File

@@ -1,227 +0,0 @@
<?php
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;
protected function __construct() {
}
final public static function GetInstance(): ModuleDiscoveryService {
if (!isset(static::$oInstance)) {
static::$oInstance = new static();
}
return static::$oInstance;
}
final public static function SetInstance(?ModuleDiscoveryService $oInstance): void {
static::$oInstance = $oInstance;
}
/**
* Read the information from a module file (module.xxx.php)
* Closely inspired (almost copied/pasted !!) from ModuleDiscovery::ListModuleFiles
* @param string $sModuleFile
* @return array
* @throws ModuleDiscoveryServiceException
*/
public function ReadModuleFileConfigurationLegacy(string $sModuleFilePath) : array
{
$aModuleInfo = []; // will be filled by the "eval" line below...
try
{
$aMatches = [];
$sModuleFileContents = file_get_contents($sModuleFilePath);
$sModuleFileContents = str_replace(['<?php', '?>'], '', $sModuleFileContents);
$sModuleFileContents = str_replace('__FILE__', "'".addslashes($sModuleFilePath)."'", $sModuleFileContents);
preg_match_all('/class ([A-Za-z0-9_]+) extends ([A-Za-z0-9_]+)/', $sModuleFileContents, $aMatches);
//print_r($aMatches);
$idx = 0;
foreach($aMatches[1] as $sClassName)
{
if (class_exists($sClassName))
{
// rename any class declaration inside the code to prevent a "duplicate class" declaration
// and change its parent class as well so that nobody will find it and try to execute it
// Note: don't use the same naming scheme as ModuleDiscovery otherwise you 'll have the duplicate class error again !!
$sModuleFileContents = str_replace($sClassName.' extends '.$aMatches[2][$idx], $sClassName.'_Ext_'.(ModuleDiscoveryService::$iDummyClassIndex++).' extends DummyHandler', $sModuleFileContents);
}
$idx++;
}
// Replace the main function call by an assignment to a variable, as an array...
$sModuleFileContents = str_replace(['SetupWebPage::AddModule', 'ModuleDiscovery::AddModule'], '$aModuleInfo = array', $sModuleFileContents);
eval($sModuleFileContents); // Assigns $aModuleInfo
if (count($aModuleInfo) === 0)
{
throw new ModuleDiscoveryServiceException("Eval of $sModuleFilePath did not return the expected information...");
}
$this->CompleteConfigWithModuleFilePath($aModuleInfo);
}
catch(ModuleDiscoveryServiceException $e)
{
// Continue...
throw $e;
}
catch(ParseError $e)
{
// Continue...
throw new ModuleDiscoveryServiceException("Eval of $sModuleFilePath caused a parse error: ".$e->getMessage()." at line ".$e->getLine());
}
catch(Exception $e)
{
// Continue...
throw new ModuleDiscoveryServiceException("Eval of $sModuleFilePath caused an exception: ".$e->getMessage(), 0, $e);
}
return $aModuleInfo;
}
/**
* Read the information from a module file (module.xxx.php)
* Closely inspired (almost copied/pasted !!) from ModuleDiscovery::ListModuleFiles
* @param string $sModuleFile
* @return array
* @throws ModuleDiscoveryServiceException
*/
public function ReadModuleFileConfiguration(string $sModuleFilePath) : array
{
try
{
$aNodes = ModuleDiscoveryEvaluationService::GetInstance()->ParsePhpCode(file_get_contents($sModuleFilePath));
}
catch (PhpParser\Error $e) {
throw new \ModuleDiscoveryServiceException($e->getMessage(), 0, $e, $sModuleFilePath);
}
try {
foreach ($aNodes as $sKey => $oNode) {
if ($oNode instanceof \PhpParser\Node\Stmt\Expression) {
$aModuleConfig = ModuleDiscoveryEvaluationService::GetInstance()->BrowseAddModuleCallAndReturnModuleConfiguration($sModuleFilePath, $oNode);
if (! is_null($aModuleConfig)){
$this->CompleteConfigWithModuleFilePath($aModuleConfig);
return $aModuleConfig;
}
}
if ($oNode instanceof PhpParser\Node\Stmt\If_) {
$aModuleConfig = ModuleDiscoveryEvaluationService::GetInstance()->BrowseIfStructure($sModuleFilePath, $oNode);
if (! is_null($aModuleConfig)){
$this->CompleteConfigWithModuleFilePath($aModuleConfig);
return $aModuleConfig;
}
}
}
} catch(ModuleDiscoveryServiceException $e) {
// Continue...
throw $e;
} catch(Exception $e) {
// Continue...
throw new ModuleDiscoveryServiceException("Eval of $sModuleFilePath caused an exception: ".$e->getMessage(), 0, $e, $sModuleFilePath);
}
throw new ModuleDiscoveryServiceException("No proper call to SetupWebPage::AddModule found in module file", 0, null, $sModuleFilePath);
}
/**
* 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 CompleteConfigWithModuleFilePath(array &$aModuleInfo)
{
if (count($aModuleInfo)==3) {
$aModuleInfo[2]['module_file_path'] = $aModuleInfo[0];
}
}
/**
*
* @param \Config $oConfig
* @param array $aModuleConfig
*
* @return void
* @throws \ModuleDiscoveryServiceException
*/
public function CallInstallerBeforeWritingConfigMethod(Config $oConfig, array $aModuleConfig)
{
$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;
}
}

View File

@@ -3,13 +3,13 @@
use PhpParser\ParserFactory;
use PhpParser\Node\Expr\Assign;
class ModuleDiscoveryEvaluationService {
private static ModuleDiscoveryEvaluationService $oInstance;
class ModuleFileParser {
private static ModuleFileParser $oInstance;
protected function __construct() {
}
final public static function GetInstance(): ModuleDiscoveryEvaluationService {
final public static function GetInstance(): ModuleFileParser {
if (!isset(static::$oInstance)) {
static::$oInstance = new static();
}
@@ -17,7 +17,7 @@ class ModuleDiscoveryEvaluationService {
return static::$oInstance;
}
final public static function SetInstance(?ModuleDiscoveryEvaluationService $oInstance): void {
final public static function SetInstance(?ModuleFileParser $oInstance): void {
static::$oInstance = $oInstance;
}
@@ -37,9 +37,9 @@ class ModuleDiscoveryEvaluationService {
* @param \PhpParser\Node\Expr\Assign $oAssignation
*
* @return array|null
* @throws \ModuleDiscoveryServiceException
* @throws \ModuleFileReaderException
*/
public function BrowseAddModuleCallAndReturnModuleConfiguration(string $sModuleFilePath, \PhpParser\Node\Stmt\Expression $oExpression) : ?array
public function GetModuleInformationFromAddModuleCall(string $sModuleFilePath, \PhpParser\Node\Stmt\Expression $oExpression) : ?array
{
/** @var Assign $oAssignation */
$oAssignation = $oExpression->expr;
@@ -59,17 +59,17 @@ class ModuleDiscoveryEvaluationService {
$aArgs = $oAssignation?->args;
if (count($aArgs) != 3) {
throw new ModuleDiscoveryServiceException("Not enough parameters when calling SetupWebPage::AddModule", 0, null, $sModuleFilePath);
throw new ModuleFileReaderException("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);
throw new ModuleFileReaderException("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);
throw new ModuleFileReaderException("2nd parameter to SetupWebPage::AddModule not a string: " . get_class($oModuleId->value), 0, null, $sModuleFilePath);
}
/** @var PhpParser\Node\Scalar\String_ $sModuleIdStringObj */
@@ -78,19 +78,19 @@ class ModuleDiscoveryEvaluationService {
$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);
throw new ModuleFileReaderException("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);
throw new ModuleFileReaderException("3rd parameter to SetupWebPage::AddModule not an array: " . get_class($oModuleConfigInfo->value), 0, null, $sModuleFilePath);
}
$aModuleConfig=[];
$this->BrowseArrayStructure($oModuleConfigInfo->value, $aModuleConfig);
$this->FillModuleInformationFromArray($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);
throw new ModuleFileReaderException("3rd parameter to SetupWebPage::AddModule not an array: " . get_class($oModuleConfigInfo->value), 0, null, $sModuleFilePath);
}
return [
$sModuleFilePath,
@@ -99,7 +99,7 @@ class ModuleDiscoveryEvaluationService {
];
}
public function BrowseArrayStructure(PhpParser\Node\Expr\Array_ $oArray, array &$aModuleConfig) : void
public function FillModuleInformationFromArray(PhpParser\Node\Expr\Array_ $oArray, array &$aModuleInformation) : void
{
$iIndex=0;
/** @var \PhpParser\Node\Expr\ArrayItem $oValue */
@@ -120,18 +120,18 @@ class ModuleDiscoveryEvaluationService {
if ($oValue instanceof PhpParser\Node\Expr\Array_) {
$aSubConfig=[];
$this->BrowseArrayStructure($oValue, $aSubConfig);
$aModuleConfig[$sKey]=$aSubConfig;
$this->FillModuleInformationFromArray($oValue, $aSubConfig);
$aModuleInformation[$sKey]=$aSubConfig;
}
if ($oValue instanceof PhpParser\Node\Scalar\String_||$oValue instanceof PhpParser\Node\Scalar\Int_) {
$aModuleConfig[$sKey]=$oValue->value;
$aModuleInformation[$sKey]=$oValue->value;
continue;
}
if ($oValue instanceof \PhpParser\Node\Expr\ConstFetch) {
$oEvaluatedConstant = $this->EvaluateConstantExpression($oValue);
$aModuleConfig[$sKey]= $oEvaluatedConstant;
$aModuleInformation[$sKey]= $oEvaluatedConstant;
}
}
}
@@ -141,15 +141,15 @@ class ModuleDiscoveryEvaluationService {
* @param \PhpParser\Node\Stmt\If_ $oNode
*
* @return array|null
* @throws \ModuleDiscoveryServiceException
* @throws \ModuleFileReaderException
*/
public function BrowseIfStructure(string $sModuleFilePath, \PhpParser\Node\Stmt\If_ $oNode) : ?array
public function GetModuleInformationFromIf(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);
$aModuleConfig = $this->GetModuleInformationFromAddModuleCall($sModuleFilePath, $oSubNode);
if (!is_null($aModuleConfig)) {
return $aModuleConfig;
}
@@ -163,7 +163,7 @@ class ModuleDiscoveryEvaluationService {
/** @var \PhpParser\Node\Stmt\ElseIf_ $oElseIfSubNode */
$bCondition = $this->EvaluateExpression($oElseIfSubNode->cond);
if ($bCondition) {
$aModuleConfig = $this->BrowseStatementsAndReturnModuleConfiguration($sModuleFilePath, $oElseIfSubNode->stmts);
$aModuleConfig = $this->GetModuleConfigurationFromStatement($sModuleFilePath, $oElseIfSubNode->stmts);
if (!is_null($aModuleConfig)) {
return $aModuleConfig;
}
@@ -173,7 +173,7 @@ class ModuleDiscoveryEvaluationService {
}
if (! is_null($oNode->else)) {
$aModuleConfig = $this->BrowseStatementsAndReturnModuleConfiguration($sModuleFilePath, $oNode->else->stmts);
$aModuleConfig = $this->GetModuleConfigurationFromStatement($sModuleFilePath, $oNode->else->stmts);
return $aModuleConfig;
}
@@ -181,11 +181,11 @@ class ModuleDiscoveryEvaluationService {
return null;
}
public function BrowseStatementsAndReturnModuleConfiguration(string $sModuleFilePath, array $aStmts) : ?array
public function GetModuleConfigurationFromStatement(string $sModuleFilePath, array $aStmts) : ?array
{
foreach ($aStmts as $oSubNode) {
if ($oSubNode instanceof \PhpParser\Node\Stmt\Expression) {
$aModuleConfig = $this->BrowseAddModuleCallAndReturnModuleConfiguration($sModuleFilePath, $oSubNode);
$aModuleConfig = $this->GetModuleInformationFromAddModuleCall($sModuleFilePath, $oSubNode);
if (!is_null($aModuleConfig)) {
return $aModuleConfig;
}
@@ -195,13 +195,14 @@ class ModuleDiscoveryEvaluationService {
return null;
}
//TODO replace eval
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());
throw new ModuleFileReaderException("Eval of ' . $oValue->name . ' caused an error: ".$t->getMessage());
}
return $bResult;
@@ -220,7 +221,7 @@ class ModuleDiscoveryEvaluationService {
* @param string $sBooleanExpr
*
* @return bool
* @throws ModuleDiscoveryServiceException
* @throws ModuleFileReaderException
*/
private function UnprotectedComputeBooleanExpression(string $sBooleanExpr) : bool
{
@@ -228,7 +229,7 @@ class ModuleDiscoveryEvaluationService {
try{
@eval('$bResult = '.$sBooleanExpr.';');
} catch (Throwable $t) {
throw new ModuleDiscoveryServiceException("Eval of '$sBooleanExpr' caused an error: ".$t->getMessage());
throw new ModuleFileReaderException("Eval of '$sBooleanExpr' caused an error: ".$t->getMessage());
}
return $bResult;
@@ -239,7 +240,7 @@ class ModuleDiscoveryEvaluationService {
* @param bool $bSafe: when true, evaluation relies on unsafe eval() call
*
* @return bool
* @throws ModuleDiscoveryServiceException
* @throws ModuleFileReaderException
*/
public function EvaluateBooleanExpression(string $sBooleanExpr, $bSafe=true) : bool
{
@@ -256,7 +257,7 @@ PHP;
$oExpr = $aNodes[0];
return $this->EvaluateExpression($oExpr->expr);
} catch (Throwable $t) {
throw new ModuleDiscoveryServiceException("Eval of '$sBooleanExpr' caused an error:".$t->getMessage());
throw new ModuleFileReaderException("Eval of '$sBooleanExpr' caused an error:".$t->getMessage());
}
}
@@ -293,10 +294,9 @@ PHP;
private function EvaluateCallFunction(\PhpParser\Node\Expr\FuncCall $oFunct) : bool
{
$sFunction = $oFunct->name->name;
$aWhiteList = ["function_exists"];
$aWhiteList = ["function_exists", "class_exists", "method_exists"];
if (! in_array($sFunction, $aWhiteList)){
throw new ModuleDiscoveryServiceException("FuncCall $sFunction not supported");
//return false;
throw new ModuleFileReaderException("FuncCall $sFunction not supported");
}
$aArgs=[];
@@ -313,7 +313,7 @@ PHP;
* @param \PhpParser\Node\Expr\StaticCall $oStaticCall
*
* @return bool
* @throws \ModuleDiscoveryServiceException
* @throws \ModuleFileReaderException
* @throws \ReflectionException
*/
private function EvaluateStaticCallFunction(\PhpParser\Node\Expr\StaticCall $oStaticCall) : bool
@@ -323,7 +323,7 @@ PHP;
$aWhiteList = ["SetupInfo::ModuleIsSelected"];
$sStaticCallDescription = "$sClassName::$sMethodName";
if (! in_array($sStaticCallDescription, $aWhiteList)){
throw new ModuleDiscoveryServiceException("StaticCall $sStaticCallDescription not supported");
throw new ModuleFileReaderException("StaticCall $sStaticCallDescription not supported");
}
$aArgs=[];
@@ -335,7 +335,7 @@ PHP;
$class = new \ReflectionClass($sClassName);
$method = $class->getMethod($sMethodName);
if (! $method->isPublic()){
throw new ModuleDiscoveryServiceException("StaticCall $sStaticCallDescription not public");
throw new ModuleFileReaderException("StaticCall $sStaticCallDescription not public");
}
return (bool) $method->invokeArgs(null, $aArgs);

View File

@@ -0,0 +1,170 @@
<?php
use PhpParser\ParserFactory;
use PhpParser\Node\Expr\Assign;
require_once __DIR__ . '/ModuleFileParser.php';
require_once __DIR__ . '/ModuleFileReaderException.php';
class ModuleFileReader {
private static ModuleFileReader $oInstance;
private static int $iDummyClassIndex = 0;
protected function __construct() {
}
final public static function GetInstance(): ModuleFileReader {
if (!isset(static::$oInstance)) {
static::$oInstance = new static();
}
return static::$oInstance;
}
final public static function SetInstance(?ModuleFileReader $oInstance): void {
static::$oInstance = $oInstance;
}
/**
* Read the information from a module file (module.xxx.php)
* Use this method to load the ModuleInstallerAPI
* @param string $sModuleFile
* @return array
* @throws ModuleFileReaderException
*/
public function ReadModuleFileConfigurationUnsafe(string $sModuleFilePath) : array
{
$aModuleInfo = []; // will be filled by the "eval" line below...
try
{
$aMatches = [];
$sModuleFileContents = file_get_contents($sModuleFilePath);
$sModuleFileContents = str_replace(['<?php', '?>'], '', $sModuleFileContents);
$sModuleFileContents = str_replace('__FILE__', "'".addslashes($sModuleFilePath)."'", $sModuleFileContents);
preg_match_all('/class ([A-Za-z0-9_]+) extends ([A-Za-z0-9_]+)/', $sModuleFileContents, $aMatches);
//print_r($aMatches);
$idx = 0;
foreach($aMatches[1] as $sClassName)
{
if (class_exists($sClassName))
{
// rename any class declaration inside the code to prevent a "duplicate class" declaration
// and change its parent class as well so that nobody will find it and try to execute it
// Note: don't use the same naming scheme as ModuleDiscovery otherwise you 'll have the duplicate class error again !!
$sModuleFileContents = str_replace($sClassName.' extends '.$aMatches[2][$idx], $sClassName.'_Ext_'.(ModuleFileReader::$iDummyClassIndex++).' extends DummyHandler', $sModuleFileContents);
}
$idx++;
}
// Replace the main function call by an assignment to a variable, as an array...
$sModuleFileContents = str_replace(['SetupWebPage::AddModule', 'ModuleDiscovery::AddModule'], '$aModuleInfo = array', $sModuleFileContents);
eval($sModuleFileContents); // Assigns $aModuleInfo
if (count($aModuleInfo) === 0)
{
throw new ModuleFileReaderException("Eval of $sModuleFilePath did not return the expected information...");
}
$this->CompleteModuleInfoWithFilePath($aModuleInfo);
}
catch(ModuleFileReaderException $e)
{
// Continue...
throw $e;
}
catch(ParseError $e)
{
// Continue...
throw new ModuleFileReaderException("Eval of $sModuleFilePath caused a parse error: ".$e->getMessage()." at line ".$e->getLine());
}
catch(Exception $e)
{
// Continue...
throw new ModuleFileReaderException("Eval of $sModuleFilePath caused an exception: ".$e->getMessage(), 0, $e);
}
return $aModuleInfo;
}
/**
* Read the information from a module file (module.xxx.php)
* @param string $sModuleFile
* @return array
* @throws ModuleFileReaderException
*/
public function ReadModuleFileConfiguration(string $sModuleFilePath) : array
{
try
{
$aNodes = ModuleFileParser::GetInstance()->ParsePhpCode(file_get_contents($sModuleFilePath));
}
catch (PhpParser\Error $e) {
throw new \ModuleFileReaderException($e->getMessage(), 0, $e, $sModuleFilePath);
}
try {
foreach ($aNodes as $sKey => $oNode) {
if ($oNode instanceof \PhpParser\Node\Stmt\Expression) {
$aModuleInfo = ModuleFileParser::GetInstance()->GetModuleInformationFromAddModuleCall($sModuleFilePath, $oNode);
if (! is_null($aModuleInfo)){
$this->CompleteModuleInfoWithFilePath($aModuleInfo);
return $aModuleInfo;
}
}
if ($oNode instanceof PhpParser\Node\Stmt\If_) {
$aModuleInfo = ModuleFileParser::GetInstance()->GetModuleInformationFromIf($sModuleFilePath, $oNode);
if (! is_null($aModuleInfo)){
$this->CompleteModuleInfoWithFilePath($aModuleInfo);
return $aModuleInfo;
}
}
}
} catch(ModuleFileReaderException $e) {
// Continue...
throw $e;
} catch(Exception $e) {
// Continue...
throw new ModuleFileReaderException("Eval of $sModuleFilePath caused an exception: ".$e->getMessage(), 0, $e, $sModuleFilePath);
}
throw new ModuleFileReaderException("No proper call to SetupWebPage::AddModule found in module file", 0, null, $sModuleFilePath);
}
/**
*
* Internal trick: additional path is added into the module info structure to handle ModuleInstallerAPI execution during setup
* @param array &$aModuleInfo
*
* @return void
*/
private function CompleteModuleInfoWithFilePath(array &$aModuleInfo)
{
if (count($aModuleInfo)==3) {
$aModuleInfo[2]['module_file_path'] = $aModuleInfo[0];
}
}
public function GetAndCheckModuleInstallerClass($aModuleInfo) : ?string
{
if (! isset($aModuleInfo['installer'])){
return null;
}
$sModuleInstallerClass = $aModuleInfo['installer'];
if (!class_exists($sModuleInstallerClass)) {
$sModuleFilePath = $aModuleInfo['module_file_path'];
$this->ReadModuleFileConfigurationUnsafe($sModuleFilePath);
}
if (!class_exists($sModuleInstallerClass))
{
throw new CoreException("Wrong installer class: '$sModuleInstallerClass' is not a PHP class - Module: ".$aModuleInfo['label']);
}
if (!is_subclass_of($sModuleInstallerClass, 'ModuleInstallerAPI'))
{
throw new CoreException("Wrong installer class: '$sModuleInstallerClass' is not derived from 'ModuleInstallerAPI' - Module: ".$aModuleInfo['label']);
}
return $sModuleInstallerClass;
}
}

View File

@@ -1,9 +1,9 @@
<?php
class ModuleDiscoveryServiceException extends Exception
class ModuleFileReaderException extends Exception
{
/**
* ModuleDiscoveryServiceException constructor.
* ModuleFileReaderException constructor.
*
* @param string $sMessage
* @param int $iHttpCode