From 1c147024e5d741bdd7e7e9816887e37020d84583 Mon Sep 17 00:00:00 2001 From: cmp167 Date: Thu, 21 Mar 2024 17:43:16 +0300 Subject: [PATCH] Add try/catch call_user_func --- cmpUtil.php | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/cmpUtil.php b/cmpUtil.php index db15858..0cf1dd5 100644 --- a/cmpUtil.php +++ b/cmpUtil.php @@ -351,15 +351,47 @@ if(!isset($opt["silent"]) || !$opt["silent"]) $this->d($prs["streamCurrent"] . ": " . $a[$i]); + $callName = ""; + if($prs["streamCurrent"] == "stdout") { - if(isset($opt["eventStringStdOut"]) && $opt["eventStringStdOut"]) { - ($opt["eventStringStdOut"]["call"])($a[$i], $prs); - } + $callName = "eventStringStdOut"; } else if($prs["streamCurrent"] == "stderr") { - if(isset($opt["eventStringStdErr"]) && $opt["eventStringStdErr"]) { - ($opt["eventStringStdErr"]["call"])($a[$i], $prs); - } + $callName = "eventStringStdErr"; + } + + if(!isset($opt["eventStringStdOut"])) + continue; + + if(!$opt["eventStringStdOut"]) + continue; + + if(!isset($opt[$callName]["call"])) { + $this->d("Unset $callName.call"); + continue; + } + + if(!is_callable($opt[$callName]["call"])) { + $this->d("Uncallable $callName.call"); + continue; + } + + $parm = null; + + if(isset($opt[$callName]["parm"])) + $parm = $opt[$callName]["parm"]; + + try { + $ret = call_user_func( + $opt[$callName]["call"], + array( + "string" => $a[$i], + "parm" => $parm, + "prs" => $prs + ) + ); + } catch(Exception | Error $e) { + $this->e($e); } } @@ -651,7 +683,10 @@ $this->cmpSysExec($a, array( "silent" => false, "eventString" => true, - "eventStringStdOut" => array("call" => [$this, "d"]) + "eventStringStdOut" => array( + "call" => [$this, "d"], + "parm" => "testparm" + ) )); }