Skip to content

Commit bf93b90

Browse files
authored
Merge pull request #7 from russelomua/master
solution for way to pass $allowTimeout into waitResponse
2 parents 0d46709 + 7da4d7a commit bf93b90

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ After installing, you can now use it in your code:
4141
throw new \RuntimeException('Could not connect to Asterisk Management Interface.');
4242
}
4343

44+
// // if you have a looping of command function
45+
// // set allowTimeout flag to true
46+
// $ami->allowTimeout();
47+
4448
// $result contains the output from the command
4549
$result = $ami->command('core show channels');
4650

src/Ami.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ class Ami
9393
*/
9494
public $port;
9595

96+
/**
97+
* @var int Used in waitResponse function to prevent looping when true
98+
*/
99+
private $allowTimeout;
100+
96101
/**
97102
* Event Handlers
98103
*
@@ -186,6 +191,17 @@ public function sendRequest($action, array $parameters = [])
186191
return $response;
187192
}//end sendRequest()
188193

194+
/**
195+
* Set global allowTimeout flag
196+
* it will prevent looping waitResponse function
197+
*
198+
* @param boolean $value
199+
* @return void
200+
*/
201+
public function allowTimeout($value = true)
202+
{
203+
$this->allowTimeout = boolval($value);
204+
}//end allowTimeout()
189205

190206
/**
191207
* Wait for a response
@@ -199,10 +215,12 @@ public function sendRequest($action, array $parameters = [])
199215
*/
200216
public function waitResponse($allowTimeout = false)
201217
{
202-
if (!is_resource($this->socket)) {
203-
return [];
204-
}
205-
218+
if (!is_resource($this->socket)) {
219+
return [];
220+
}
221+
222+
$allowTimeout = $this->allowTimeout ?: $allowTimeout;
223+
206224
// make sure we haven't already timed out
207225
$info = stream_get_meta_data($this->socket);
208226
if (feof($this->socket) === true || $info['timed_out'] === true) {
@@ -381,8 +399,8 @@ public function disconnect()
381399
$this->logoff();
382400
}
383401

384-
if (is_resource($this->socket)) {
385-
fclose($this->socket);
402+
if (is_resource($this->socket)) {
403+
fclose($this->socket);
386404
}
387405
}//end disconnect()
388406

0 commit comments

Comments
 (0)