mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-26 20:18:52 +02:00
N°4789 - fix broken setup + tests
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
class ModuleDiscoveryService {
|
||||
private static ModuleDiscoveryService $oInstance;
|
||||
private static int $iDummyClassIndex = 0;
|
||||
|
||||
protected function __construct() {
|
||||
}
|
||||
@@ -23,13 +24,14 @@ class ModuleDiscoveryService {
|
||||
* Closely inspired (almost copied/pasted !!) from ModuleDiscovery::ListModuleFiles
|
||||
* @param string $sModuleFile
|
||||
* @return array
|
||||
* @throws ModuleDiscoveryServiceException
|
||||
*/
|
||||
public function ReadModuleFileConfiguration(string $sModuleFilePath) : array
|
||||
{
|
||||
static $iDummyClassIndex = 0;
|
||||
$aModuleInfo = []; // will be filled by the "eval" line below...
|
||||
$aModuleInfo = array(); // will be filled by the "eval" line below...
|
||||
try
|
||||
{
|
||||
$aMatches = array();
|
||||
$sModuleFileContents = file_get_contents($sModuleFilePath);
|
||||
$sModuleFileContents = str_replace(array('<?php', '?>'), '', $sModuleFileContents);
|
||||
$sModuleFileContents = str_replace('__FILE__', "'".addslashes($sModuleFilePath)."'", $sModuleFileContents);
|
||||
@@ -40,47 +42,73 @@ class ModuleDiscoveryService {
|
||||
{
|
||||
if (class_exists($sClassName))
|
||||
{
|
||||
// rename the class inside the code to prevent a "duplicate class" declaration
|
||||
// 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
|
||||
$sModuleFileContents = str_replace($sClassName.' extends '.$aMatches[2][$idx], $sClassName.'_'.($iDummyClassIndex++).' extends DummyHandler', $sModuleFileContents);
|
||||
// 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(array('SetupWebPage::AddModule', 'ModuleDiscovery::AddModule'), '$aModuleInfo = array', $sModuleFileContents);
|
||||
|
||||
eval($sModuleFileContents); // Assigns $aModuleInfo
|
||||
|
||||
if (count($aModuleInfo) === 0)
|
||||
{
|
||||
SetupLog::Warning("Eval of $sModuleFilePath did not return the expected information...");
|
||||
throw new ModuleDiscoveryServiceException("Eval of $sModuleFilePath did not return the expected information...");
|
||||
}
|
||||
|
||||
//echo "<p>Done.</p>\n";
|
||||
}
|
||||
catch(ModuleDiscoveryServiceException $e)
|
||||
{
|
||||
// Continue...
|
||||
throw $e;
|
||||
}
|
||||
catch(ParseError $e)
|
||||
{
|
||||
// PHP 7
|
||||
SetupLog::Warning("Eval of $sModuleFilePath caused a parse exception: ".$e->getMessage()." at line ".$e->getLine());
|
||||
// Continue...
|
||||
throw new ModuleDiscoveryServiceException("Eval of $sModuleFilePath caused a parse error: ".$e->getMessage()." at line ".$e->getLine());
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
// Continue...
|
||||
SetupLog::Warning("Eval of $sModuleFilePath caused an exception: ".$e->getMessage());
|
||||
throw new ModuleDiscoveryServiceException("Eval of $sModuleFilePath caused an exception: ".$e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
return $aModuleInfo;
|
||||
}
|
||||
|
||||
public function ComputeDependencyExpression(string $sBooleanExpr) : bool
|
||||
/**
|
||||
* @param string $sBooleanExpr
|
||||
*
|
||||
* @return bool
|
||||
* @throws ModuleDiscoveryServiceException
|
||||
*/
|
||||
public function ComputeBooleanExpression(string $sBooleanExpr) : bool
|
||||
{
|
||||
return @eval('$bResult = '.$sBooleanExpr.'; return $bResult;');
|
||||
$bResult = false;
|
||||
try{
|
||||
@eval('$bResult = '.$sBooleanExpr.';');
|
||||
} catch (Throwable $t) {
|
||||
throw new ModuleDiscoveryServiceException("Eval of '$sBooleanExpr' caused an error: ".$t->getMessage());
|
||||
}
|
||||
|
||||
return $bResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function ComputeAutoSelectExpression(string $sBooleanExpr) : bool
|
||||
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)
|
||||
{
|
||||
return eval('$bSelected = ('.$sBooleanExpr.'); return $bSelected');
|
||||
$e = new \Exception("");
|
||||
|
||||
SetupLog::Warning($sMessage, null, ['previous' => $oPrevious?->getMessage(), 'stack' => $e->getTraceAsString()]);
|
||||
parent::__construct($sMessage, $iHttpCode, $oPrevious);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user