Skip to content

Commit 9d31039

Browse files
authored
Merge pull request #71 from SimonFrings/updates
Use new Socket API and improve documentation and examples
2 parents 72bd964 + 21b22f5 commit 9d31039

File tree

6 files changed

+31
-38
lines changed

6 files changed

+31
-38
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ Once [installed](#install), you can use the following code to access your local
8282
Asterisk instance and issue some simple commands via AMI:
8383

8484
```php
85+
<?php
86+
87+
require __DIR__ . '/vendor/autoload.php';
88+
8589
$factory = new Clue\React\Ami\Factory();
8690

8791
$factory->createClient('user:secret@localhost')->then(function (Clue\React\Ami\Client $client) {
@@ -118,7 +122,7 @@ proxy servers etc.), you can explicitly pass a custom instance of the
118122
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
119123

120124
```php
121-
$connector = new React\Socket\Connector(null, array(
125+
$connector = new React\Socket\Connector(array(
122126
'dns' => '127.0.0.1',
123127
'tcp' => array(
124128
'bindto' => '192.168.10.1:0'
@@ -546,7 +550,7 @@ This is a shortcut to get the value of the "Event" field.
546550

547551
## Install
548552

549-
The recommended way to install this library is [through Composer](https://getcomposer.org).
553+
The recommended way to install this library is [through Composer](https://getcomposer.org/).
550554
[New to Composer?](https://getcomposer.org/doc/00-intro.md)
551555

552556
This project follows [SemVer](https://semver.org/).
@@ -560,12 +564,12 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
560564

561565
This project aims to run on any platform and thus does not require any PHP
562566
extensions and supports running on legacy PHP 5.3 through current PHP 8+.
563-
It's *highly recommended to use PHP 7+* for this project.
567+
It's *highly recommended to use the latest supported PHP version* for this project.
564568

565569
## Tests
566570

567571
To run the test suite, you first need to clone this repo and then install all
568-
dependencies [through Composer](https://getcomposer.org):
572+
dependencies [through Composer](https://getcomposer.org/):
569573

570574
```bash
571575
$ composer install
@@ -574,7 +578,7 @@ $ composer install
574578
To run the test suite, go to the project root and run:
575579

576580
```bash
577-
$ php vendor/bin/phpunit
581+
$ vendor/bin/phpunit
578582
```
579583

580584
The test suite contains both unit tests and functional integration tests.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
1616
"react/event-loop": "^1.2",
1717
"react/promise": "^2.0 || ^1.1",
18-
"react/socket": "^1.8"
18+
"react/socket": "^1.9"
1919
},
2020
"require-dev": {
2121
"clue/block-react": "^1.2",

examples/commands.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
<?php
22

3-
use Clue\React\Ami\Factory;
4-
use Clue\React\Ami\Client;
5-
use Clue\React\Ami\ActionSender;
6-
use Clue\React\Ami\Protocol\Response;
73
use React\EventLoop\Loop;
84

95
require __DIR__ . '/../vendor/autoload.php';
106

11-
$factory = new Factory();
7+
$factory = new Clue\React\Ami\Factory();
128

139
$target = isset($argv[1]) ? $argv[1] : 'name:password@localhost';
1410

15-
$factory->createClient($target)->then(function (Client $client) {
11+
$factory->createClient($target)->then(function (Clue\React\Ami\Client $client) {
1612
echo 'Client connected. Use STDIN to send CLI commands via asterisk AMI.' . PHP_EOL;
17-
$sender = new ActionSender($client);
13+
$sender = new Clue\React\Ami\ActionSender($client);
1814

1915
$sender->events(false);
2016

21-
$sender->listCommands()->then(function (Response $response) {
17+
$sender->listCommands()->then(function (Clue\React\Ami\Protocol\Response $response) {
2218
echo 'Commands: ' . implode(', ', array_keys($response->getFields())) . PHP_EOL;
2319
});
2420

@@ -33,12 +29,14 @@
3329
echo '<' . $line . PHP_EOL;
3430

3531
$sender->command($line)->then(
36-
function (Response $response) {
32+
function (Clue\React\Ami\Protocol\Response $response) {
3733
echo $response->getCommandOutput() . PHP_EOL;
3834
},
3935
function (Exception $error) use ($line) {
4036
echo 'Error executing "' . $line . '": ' . $error->getMessage() . PHP_EOL;
4137
}
4238
);
4339
});
44-
}, 'var_dump');
40+
}, function (Exception $error) {
41+
echo 'Connection error: ' . $error;
42+
});

examples/events.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
<?php
22

3-
use Clue\React\Ami\Factory;
4-
use Clue\React\Ami\Client;
5-
use Clue\React\Ami\ActionSender;
6-
use Clue\React\Ami\Protocol\Response;
7-
use Clue\React\Ami\Protocol\Event;
8-
93
require __DIR__ . '/../vendor/autoload.php';
104

11-
$factory = new Factory();
5+
$factory = new Clue\React\Ami\Factory();
126

137
$target = isset($argv[1]) ? $argv[1] : 'name:password@localhost';
148

159
$factory->createClient($target)->then(
16-
function (Client $client) {
10+
function (Clue\React\Ami\Client $client) {
1711
echo 'Client connected ' . PHP_EOL;
1812

19-
$sender = new ActionSender($client);
13+
$sender = new Clue\React\Ami\ActionSender($client);
2014
$sender->events(true);
2115

2216
$client->on('close', function() {
2317
echo 'Connection closed' . PHP_EOL;
2418
});
2519

26-
$client->on('event', function (Event $event) {
20+
$client->on('event', function (Clue\React\Ami\Protocol\Event $event) {
2721
echo 'Event: ' . $event->getName() . ': ' . json_encode($event->getFields()) . PHP_EOL;
2822
});
2923
},

examples/peers.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
<?php
22

3-
use Clue\React\Ami\ActionSender;
4-
use Clue\React\Ami\Client;
5-
use Clue\React\Ami\Factory;
6-
use Clue\React\Ami\Protocol\Collection;
7-
83
require __DIR__ . '/../vendor/autoload.php';
94

10-
$factory = new Factory();
5+
$factory = new Clue\React\Ami\Factory();
116

127
$target = isset($argv[1]) ? $argv[1] : 'name:password@localhost';
138

14-
$factory->createClient($target)->then(function (Client $client) {
9+
$factory->createClient($target)->then(function (Clue\React\Ami\Client $client) {
1510
echo 'Successfully connected' . PHP_EOL;
1611

17-
$collector = new ActionSender($client);
12+
$collector = new Clue\React\Ami\ActionSender($client);
1813

19-
$collector->sipPeers()->then(function (Collection $collection) {
14+
$collector->sipPeers()->then(function (Clue\React\Ami\Protocol\Collection $collection) {
2015
var_dump('result', $collection);
2116
$peers = $collection->getEntryEvents();
2217

2318
echo 'found ' . count($peers) . ' peers' . PHP_EOL;
2419
});
25-
}, 'var_dump');
20+
}, function (Exception $error) {
21+
echo 'Connection error: ' . $error;
22+
});

src/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
2626
*
2727
* ```php
28-
* $connector = new React\Socket\Connector(null, array(
28+
* $connector = new React\Socket\Connector(array(
2929
* 'dns' => '127.0.0.1',
3030
* 'tcp' => array(
3131
* 'bindto' => '192.168.10.1:0'
@@ -46,7 +46,7 @@ class Factory
4646
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null)
4747
{
4848
if ($connector === null) {
49-
$connector = new Connector($loop);
49+
$connector = new Connector(array(), $loop);
5050
}
5151

5252
$this->connector = $connector;

0 commit comments

Comments
 (0)