From e27dcbfb2d302d59d78c8cfffd8c81753ee852a9 Mon Sep 17 00:00:00 2001 From: odain Date: Wed, 1 Jul 2026 17:04:56 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B09675=20-=20raise=20PHP=20CLI=20version?= =?UTF-8?q?=20issue=20as=20well?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ModelReflectionSerializer.php | 39 +++++++++++++++---- .../ModelSerializationTest.php | 21 ++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/setup/feature_removal/ModelReflectionSerializer.php b/setup/feature_removal/ModelReflectionSerializer.php index 3570e36721..bca18bb1b9 100644 --- a/setup/feature_removal/ModelReflectionSerializer.php +++ b/setup/feature_removal/ModelReflectionSerializer.php @@ -31,6 +31,8 @@ class ModelReflectionSerializer self::$oInstance = $oInstance; } + public const ERROR_LABEL = "Data consistency check failed: %s"; + public function GetModelFromEnvironment(string $sEnv): array { IssueLog::Debug(__METHOD__, null, ['env' => $sEnv]); @@ -38,20 +40,28 @@ class ModelReflectionSerializer $sPHPExec = trim(utils::GetConfig()->Get('php_path')); $aOutput = null; $iRes = 0; + exec("$sPHPExec --version", $aOutput, $iRes); + if ($iRes != 0) { + $sError = sprintf(self::ERROR_LABEL, "Cannot check CLI/PHP version ($sPHPExec)"); + $this->LogSetupError($sError, null, ['env' => $sEnv, 'code' => $iRes, "output" => $aOutput, 'php_path' => $sPHPExec]); + throw new CoreException($sError); + } - $sErrorLabel = "Data consistency check failed: %s"; + $this->CheckCliPhpVersionFromOutput(PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION, $sPHPExec, $aOutput); + $aOutput = null; + $iRes = 0; //preliminary check $sEnvDir = APPROOT."env-$sEnv"; if (! is_dir($sEnvDir)) { - $sMsg = sprintf($sErrorLabel, "Missing environment ($sEnvDir)"); + $sMsg = sprintf(self::ERROR_LABEL, "Missing environment ($sEnvDir)"); $this->LogSetupError($sMsg); throw new CoreException($sMsg); } $sConfigFile = APPROOT."conf/$sEnv/config-itop.php"; if (! is_file($sConfigFile)) { - $sMsg = sprintf($sErrorLabel, "Missing configuration ($sConfigFile)"); + $sMsg = sprintf(self::ERROR_LABEL, "Missing configuration ($sConfigFile)"); $this->LogSetupError($sMsg); throw new CoreException($sMsg); } @@ -60,26 +70,39 @@ class ModelReflectionSerializer exec($sCommandLine, $aOutput, $iRes); if ($iRes != 0) { $sError = $aOutput[0] ?? 'Invalid output when serializing model'; - $this->LogSetupError(sprintf($sErrorLabel, '(cli error) '.$sError), null, ['env' => $sEnv, 'code' => $iRes, "output" => $aOutput, 'cmd' => $sCommandLine]); - throw new CoreException(sprintf($sErrorLabel, $sError)); + $this->LogSetupError(sprintf(self::ERROR_LABEL, '(cli error) '.$sError), null, ['env' => $sEnv, 'code' => $iRes, "output" => $aOutput, 'cmd' => $sCommandLine]); + throw new CoreException(sprintf(self::ERROR_LABEL, $sError)); } $aClasses = json_decode($aOutput[0] ?? null, true); if (false === $aClasses) { - $sMsg = sprintf($sErrorLabel, 'Invalid JSON'); + $sMsg = sprintf(self::ERROR_LABEL, 'Invalid JSON'); $this->LogSetupError($sMsg, null, ['env' => $sEnv, "output" => $aOutput]); throw new CoreException($sMsg); } if (!is_array($aClasses)) { $sError = $aOutput[0] ?? 'Invalid json array when serializing model'; - $this->LogSetupError(sprintf($sErrorLabel, '(JSON output not an array) '.$sError), null, ['env' => $sEnv, "classes" => $aClasses, "output" => $aOutput]); - throw new CoreException(sprintf($sErrorLabel, $sError)); + $this->LogSetupError(sprintf(self::ERROR_LABEL, '(JSON output not an array) '.$sError), null, ['env' => $sEnv, "classes" => $aClasses, "output" => $aOutput]); + throw new CoreException(sprintf(self::ERROR_LABEL, $sError)); } return $aClasses; } + public function CheckCliPhpVersionFromOutput(string $sUIPhpVersion, string $sPHPExec, $aOutput): void + { + $sFoundVersion = trim($aOutput[0] ?? ""); + if (preg_match('/^.* (\d\.\d)\.\d/', $sFoundVersion, $aMatches)) { + $sFoundVersion = $aMatches[1]; + } + if ($sFoundVersion != $sUIPhpVersion) { + $sError = sprintf(self::ERROR_LABEL, "Invalid PHP versions (CLI: $sFoundVersion/ UI: $sUIPhpVersion)"); + $this->LogSetupError($sError, null, ["output" => $aOutput, 'php_path' => $sPHPExec]); + throw new CoreException($sError); + } + } + //could be shared with others in log APIs ? private function LogSetupError($sMessage, $sChannel = null, $aContext = []): void { diff --git a/tests/php-unit-tests/unitary-tests/setup/feature_removal/ModelSerializationTest.php b/tests/php-unit-tests/unitary-tests/setup/feature_removal/ModelSerializationTest.php index cfa57dccc6..dfd1616556 100644 --- a/tests/php-unit-tests/unitary-tests/setup/feature_removal/ModelSerializationTest.php +++ b/tests/php-unit-tests/unitary-tests/setup/feature_removal/ModelSerializationTest.php @@ -21,6 +21,27 @@ class ModelSerializationTest extends ItopDataTestCase $this->assertEqualsCanonicalizing(MetaModel::GetClasses(), $aModel); } + public function testCheckFail() + { + $sOuput = <<expectException(\CoreException::class); + $this->expectExceptionMessage("Data consistency check failed: Invalid PHP versions (CLI: 7.4/ UI: 6.6)"); + ModelReflectionSerializer::GetInstance()->CheckCliPhpVersionFromOutput('6.6', 'sPHPExec', [$sOuput]); + } + + public function testCheckOK() + { + $sOuput = <<CheckCliPhpVersionFromOutput('7.4', 'sPHPExec', [$sOuput]); + $this->assertTrue(true); + } + public function testGetModelFromEnvironmentFailure_NoEnvt() { $this->expectException(\CoreException::class);