mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
N°2214 Add PHP check in CLI scripts
It is quite common that the PHP interpreter that is launched in CLI is different that the one used by the webserver. So iTop code launched by CLI could run in a context that doesn't meet iTop requirements !
This adds in the following scripts the same control that is done on the setup wizard first step :
* cron.php
* backup, check-backup
* export, exportv2
* bulk import
* synchro-exec, synchro-import
If the check throws at least one error then the script is stopped with an appropriate message, and a log is made (IssueLog, Error level, CLI channel)
(cherry picked from commit c768e18e2b : no risk taken for 2.7.1, so cherry picked for 2.8.0)
This commit is contained in:
@@ -39,6 +39,49 @@ class CheckResult
|
||||
$this->sLabel = $sLabel;
|
||||
$this->sDescription = $sDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @since 2.7.1 2.8.0 N°2214
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$sPrintDesc = (empty($this->sDescription)) ? '' : " ({$this->sDescription})";
|
||||
return "{$this->sLabel}$sPrintDesc";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \CheckResult[] $aResults
|
||||
*
|
||||
* @return \CheckResult[] only elements that are error (iSeverity===ERROR)
|
||||
*
|
||||
* @since 2.7.1 2.8.0 N°2214
|
||||
*/
|
||||
public static function KeepOnlyErrors($aResults)
|
||||
{
|
||||
return array_filter($aResults,
|
||||
static function ($v)
|
||||
{
|
||||
if ($v->iSeverity === CheckResult::ERROR) {
|
||||
return $v;
|
||||
}
|
||||
},
|
||||
ARRAY_FILTER_USE_BOTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \CheckResult[] $aResults
|
||||
* @return string[]
|
||||
* @uses \CheckResult::__toString
|
||||
*
|
||||
* @since 2.7.1 2.8.0 N°2214
|
||||
*/
|
||||
public static function FromObjetsToStrings($aResults)
|
||||
{
|
||||
return array_map(function($value) {
|
||||
return $value->__toString();
|
||||
}, $aResults);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -370,6 +413,37 @@ class SetupUtils
|
||||
return $aResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \CLIPage $oCliPage
|
||||
* @param int $iExitCode
|
||||
*
|
||||
* @since 2.7.1 2.8.0 N°2214
|
||||
*/
|
||||
public static function CheckPhpAndExtensionsForCli($oCliPage, $iExitCode = -1)
|
||||
{
|
||||
$aPhpCheckResults = self::CheckPhpAndExtensions();
|
||||
$aPhpCheckErrors = CheckResult::KeepOnlyErrors($aPhpCheckResults);
|
||||
if (empty($aPhpCheckErrors))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$sMessageTitle = 'Error: PHP minimum requirements are not met !';
|
||||
$oCliPage->p($sMessageTitle);
|
||||
$aPhpCheckErrorsForPrint = CheckResult::FromObjetsToStrings($aPhpCheckErrors);
|
||||
foreach ($aPhpCheckErrorsForPrint as $sError)
|
||||
{
|
||||
$oCliPage->p(' * '.$sError);
|
||||
}
|
||||
$oCliPage->output();
|
||||
|
||||
// some CLI scripts are launched automatically
|
||||
// we need a log so that we don't miss errors after migration !
|
||||
IssueLog::Error($oCliPage->s_title.' '.$sMessageTitle, 'CLI', $aPhpCheckErrorsForPrint);
|
||||
|
||||
exit($iExitCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CheckResult[] $aResult checks log
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user