From 2d133d4e109ec4a73436b72c686918cbff5889c8 Mon Sep 17 00:00:00 2001 From: Nikolaj Klyashtornyj Date: Tue, 3 Sep 2019 09:40:25 +0300 Subject: [PATCH 1/3] Way to pass allowTimeout to waitResponse function to prevent loopping --- README.md | 4 ++++ src/Ami.php | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 79d9642..ce8d31c 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ After installing, you can now use it in your code: throw new \RuntimeException('Could not connect to Asterisk Management Interface.'); } + // // if you have a looping of command function + // // set allowTimeout flag to true + // $ami->allowTimeout(); + // $result contains the output from the command $result = $ami->command('core show channels'); diff --git a/src/Ami.php b/src/Ami.php index 7ca571e..cf6e84e 100644 --- a/src/Ami.php +++ b/src/Ami.php @@ -93,6 +93,11 @@ class Ami */ public $port; + /** + * @var int Used in waitResponse function to prevent looping when true + */ + private $allowTimeout; + /** * Event Handlers * @@ -186,6 +191,17 @@ public function sendRequest($action, array $parameters = []) return $response; }//end sendRequest() + /** + * Set global allowTimeout flag + * it will prevent looping waitResponse function + * + * @param boolean $value + * @return void + */ + public function allowTimeout($value = true) + { + $this->allowTimeout = boolval($value); + }//end allowTimeout() /** * Wait for a response @@ -203,6 +219,8 @@ public function waitResponse($allowTimeout = false) return []; } + $allowTimeout = $this->allowTimeout ?: $allowTimeout; + // make sure we haven't already timed out $info = stream_get_meta_data($this->socket); if (feof($this->socket) === true || $info['timed_out'] === true) { From 474b014fa83fdceeccd6477d3a824c9bc3352121 Mon Sep 17 00:00:00 2001 From: Nikolaj Klyashtornyj Date: Tue, 3 Sep 2019 09:42:53 +0300 Subject: [PATCH 2/3] fix Indention in waitResponse function --- src/Ami.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Ami.php b/src/Ami.php index cf6e84e..2eb6965 100644 --- a/src/Ami.php +++ b/src/Ami.php @@ -215,10 +215,10 @@ public function allowTimeout($value = true) */ public function waitResponse($allowTimeout = false) { - if (!is_resource($this->socket)) { - return []; - } - + if (!is_resource($this->socket)) { + return []; + } + $allowTimeout = $this->allowTimeout ?: $allowTimeout; // make sure we haven't already timed out From 7da4d7ae81ca0016ae6cef38a9d6c79e1c4acb62 Mon Sep 17 00:00:00 2001 From: Nikolaj Klyashtornyj Date: Tue, 3 Sep 2019 10:19:51 +0300 Subject: [PATCH 3/3] fix indention in connect function --- src/Ami.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Ami.php b/src/Ami.php index 2eb6965..312217a 100644 --- a/src/Ami.php +++ b/src/Ami.php @@ -399,8 +399,8 @@ public function disconnect() $this->logoff(); } - if (is_resource($this->socket)) { - fclose($this->socket); + if (is_resource($this->socket)) { + fclose($this->socket); } }//end disconnect()