Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ Once [installed](#install), you can use the following code to access your local
Asterisk Telephony instance and issue some simple commands via AMI:

```php
$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);

$factory->createClient('user:secret@localhost')->then(function (Client $client) {
echo 'Client connected' . PHP_EOL;

$api = new Api($client);
$api->listCommands()->then(function (Response $response) {
$sender = new ActionSender($client);
$sender->listCommands()->then(function (Response $response) {
echo 'Available commands:' . PHP_EOL;
var_dump($response);
});
Expand Down Expand Up @@ -103,7 +104,7 @@ The `close()` method can be used to force-close the AMI connection and reject al
The `end()` method can be used to soft-close the AMI connection once all pending actions are completed.

> Advanced: Creating [`Action`](#action) objects, sending them via AMI and waiting for incoming
> [`Response`](#response) objects is usually hidden behind the [`Api`](#api) interface.
> [`Response`](#response) objects is usually hidden behind the [`ActionSender`](#actionsender) interface.
>
> If you happen to need a custom or otherwise unsupported action, you can also do so manually
> as follows. Consider filing a PR though :)
Expand All @@ -114,29 +115,29 @@ The `end()` method can be used to soft-close the AMI connection once all pending
> The `request(Action $action)` method can be used to queue the given messages to be sent via AMI
> and wait for a [`Response`](#response) object that matches the value of its "ActionID" field.

### Api
### ActionSender

The `Api` wraps a given [`Client`](#client) instance to provide a simple way to execute common actions.
The `ActionSender` wraps a given [`Client`](#client) instance to provide a simple way to execute common actions.
This class represents the main interface to execute actions and wait for the corresponding responses.

```php
$api = new Api($client);
$sender = new ActionSender($client);

$api->ping()->then(function (Response $response) {
$sender->ping()->then(function (Response $response) {
// response received for ping action
});
```

All public methods resemble their respective AMI actions.
Listing all available actions is out of scope here, please refer to the [class outline](src/Api.php).
Listing all available actions is out of scope here, please refer to the [class outline](src/ActionSender.php).

Sending actions is async (non-blocking), so you can actually send multiple action requests in parallel.
The AMI will respond to each action with a [`Response`](#response) object. The order is not guaranteed.
Sending actions uses a Promise-based interface that makes it easy to react to when an action is *fulfilled*
(i.e. either successfully resolved or rejected with an error):

```php
$api->ping()->then(
$sender->ping()->then(
function (Response $response) {
// response received for ping action
},
Expand All @@ -153,11 +154,11 @@ $api->ping()->then(
});
```

> Advanced: Using the `Api` is not strictly necessary, but is the recommended way to execute common actions.
> Advanced: Using the `ActionSender` is not strictly necessary, but is the recommended way to execute common actions.
>
> If you happen to need a new or otherwise unsupported action, or additional arguments,
> you can also do so manually. See the advanced [`Client`](#client) usage above for details.
> A PR that updates the `Api` is very much appreciated :)
> A PR that updates the `ActionSender` is very much appreciated :)

### Message

Expand Down
12 changes: 6 additions & 6 deletions examples/commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Clue\React\Ami\Factory;
use Clue\React\Ami\Client;
use Clue\React\Ami\Api;
use Clue\React\Ami\ActionSender;
use Clue\React\Ami\Protocol\Response;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -14,11 +14,11 @@

$factory->createClient($target)->then(function (Client $client) use ($loop) {
echo 'Client connected. Use STDIN to send CLI commands via asterisk AMI.' . PHP_EOL;
$api = new Api($client);
$sender = new ActionSender($client);

$api->events(false);
$sender->events(false);

$api->listCommands()->then(function (Response $response) {
$sender->listCommands()->then(function (Response $response) {
echo 'Commands: ' . implode(', ', array_keys($response->getFields())) . PHP_EOL;
});

Expand All @@ -28,11 +28,11 @@
$loop->removeReadStream(STDIN);
});

$loop->addReadStream(STDIN, function () use ($api) {
$loop->addReadStream(STDIN, function () use ($sender) {
$line = trim(fread(STDIN, 4096));
echo '<' . $line . PHP_EOL;

$api->command($line)->then(
$sender->command($line)->then(
function (Response $response) {
echo $response->getFieldValue('_') . PHP_EOL;
},
Expand Down
6 changes: 3 additions & 3 deletions examples/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Clue\React\Ami\Factory;
use Clue\React\Ami\Client;
use Clue\React\Ami\Api;
use Clue\React\Ami\ActionSender;
use Clue\React\Ami\Protocol\Response;
use Clue\React\Ami\Protocol\Event;

Expand All @@ -17,8 +17,8 @@
function (Client $client) use ($loop) {
echo 'Client connected ' . PHP_EOL;

$api = new Api($client);
$api->events(true);
$sender = new ActionSender($client);
$sender->events(true);

$client->on('close', function() {
echo 'Connection closed' . PHP_EOL;
Expand Down
2 changes: 1 addition & 1 deletion examples/peers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Clue\React\Ami\Factory;
use Clue\React\Ami\Client;
use Clue\React\Ami\Api;
use Clue\React\Ami\ActionSender;
use Clue\React\Ami\Collector;
use Clue\React\Ami\Protocol\Collection;

Expand Down
2 changes: 1 addition & 1 deletion src/Api.php → src/ActionSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use UnexpectedValueException;
use Clue\React\Ami\Protocol\Event;

class Api
class ActionSender
{
private $client;

Expand Down
4 changes: 2 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public function createClient($address = null)

if (isset($parts['user'])) {
$promise = $promise->then(function (Client $client) use ($parts, $secure) {
$api = new Api($client);
$sender = new ActionSender($client);

return $api->login($parts['user'], $parts['pass'])->then(
return $sender->login($parts['user'], $parts['pass'])->then(
function ($response) use ($client) {
return $client;
},
Expand Down
14 changes: 7 additions & 7 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Clue\React\Ami\Factory;
use React\Promise\PromiseInterface;
use Clue\React\Ami\Client;
use Clue\React\Ami\Api;
use Clue\React\Ami\ActionSender;
use Clue\React\Ami\Protocol\Response;

class FunctionalTest extends TestCase
Expand Down Expand Up @@ -42,9 +42,9 @@ public function testConnection()
*/
public function testPing(Client $client)
{
$api = new Api($client);
$sender = new ActionSender($client);

$pong = $this->waitFor($api->ping());
$pong = $this->waitFor($sender->ping());
/* @var $pong Response */
}

Expand All @@ -62,11 +62,11 @@ public function testInvalidCommandGetsRejected(Client $client)
* @depends testConnection
* @param Client $client
*/
public function testApiLogoffDisconnects(Client $client)
public function testActionSenderLogoffDisconnects(Client $client)
{
$api = new Api($client);
$sender = new ActionSender($client);

$ret = $this->waitFor($api->logoff());
$ret = $this->waitFor($sender->logoff());
/* @var $ret Response */

$this->assertFalse($client->isBusy());
Expand All @@ -79,7 +79,7 @@ public function testApiLogoffDisconnects(Client $client)
}

/**
* @depends testApiLogoffDisconnects
* @depends testActionSenderLogoffDisconnects
* @param Client $client
* @expectedException Exception
*/
Expand Down