From 89268ecb1afd171943932254c8e4f92077fadac0 Mon Sep 17 00:00:00 2001 From: Jeroen van den Enden Date: Thu, 25 Jan 2018 16:12:13 +0100 Subject: [PATCH] Throw exception when libsvm command fails to run (#200) * Throw exception when libsvm command fails to run * Update CS --- src/Phpml/Exception/LibsvmCommandException.php | 15 +++++++++++++++ .../SupportVectorMachine/SupportVectorMachine.php | 15 +++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/Phpml/Exception/LibsvmCommandException.php diff --git a/src/Phpml/Exception/LibsvmCommandException.php b/src/Phpml/Exception/LibsvmCommandException.php new file mode 100644 index 0000000..01d8079 --- /dev/null +++ b/src/Phpml/Exception/LibsvmCommandException.php @@ -0,0 +1,15 @@ +buildTrainCommand($trainingSetFileName, $modelFileName); $output = ''; - exec(escapeshellcmd($command), $output); + exec(escapeshellcmd($command), $output, $return); + + if ($return !== 0) { + throw LibsvmCommandException::failedToRun($command); + } $this->model = file_get_contents($modelFileName); @@ -168,6 +173,8 @@ class SupportVectorMachine /** * @return array|string + * + * @throws LibsvmCommandException */ public function predict(array $samples) { @@ -178,7 +185,11 @@ class SupportVectorMachine $command = sprintf('%ssvm-predict%s %s %s %s', $this->binPath, $this->getOSExtension(), $testSetFileName, $modelFileName, $outputFileName); $output = ''; - exec(escapeshellcmd($command), $output); + exec(escapeshellcmd($command), $output, $return); + + if ($return !== 0) { + throw LibsvmCommandException::failedToRun($command); + } $predictions = file_get_contents($outputFileName);