Skip to content

Commit 72bd964

Browse files
authored
Merge pull request #70 from clue-labs/default-loop
Simplify usage by supporting new default loop
2 parents 19cc3d2 + 4ec24a4 commit 72bd964

File tree

7 files changed

+37
-42
lines changed

7 files changed

+37
-42
lines changed

README.md

+13-12
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ 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-
$loop = React\EventLoop\Factory::create();
86-
$factory = new Clue\React\Ami\Factory($loop);
85+
$factory = new Clue\React\Ami\Factory();
8786

8887
$factory->createClient('user:secret@localhost')->then(function (Clue\React\Ami\Client $client) {
8988
echo 'Client connected' . PHP_EOL;
@@ -94,8 +93,6 @@ $factory->createClient('user:secret@localhost')->then(function (Clue\React\Ami\C
9493
var_dump($response);
9594
});
9695
});
97-
98-
$loop->run();
9996
```
10097

10198
See also the [examples](examples).
@@ -105,19 +102,23 @@ See also the [examples](examples).
105102
### Factory
106103

107104
The `Factory` is responsible for creating your [`Client`](#client) instance.
108-
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
109105

110106
```php
111-
$loop = React\EventLoop\Factory::create();
112-
$factory = new Clue\React\Ami\Factory($loop);
107+
$factory = new Clue\React\Ami\Factory();
113108
```
114109

110+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
111+
pass the event loop instance to use for this object. You can use a `null` value
112+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
113+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
114+
given event loop instance.
115+
115116
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
116117
proxy servers etc.), you can explicitly pass a custom instance of the
117118
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
118119

119120
```php
120-
$connector = new React\Socket\Connector($loop, array(
121+
$connector = new React\Socket\Connector(null, array(
121122
'dns' => '127.0.0.1',
122123
'tcp' => array(
123124
'bindto' => '192.168.10.1:0'
@@ -128,7 +129,7 @@ $connector = new React\Socket\Connector($loop, array(
128129
)
129130
));
130131

131-
$factory = new Clue\React\Ami\Factory($loop, $connector);
132+
$factory = new Clue\React\Ami\Factory(null, $connector);
132133
```
133134

134135
#### createClient()
@@ -377,11 +378,11 @@ The resulting blocking code could look something like this:
377378

378379
```php
379380
use Clue\React\Block;
381+
use React\EventLoop\Loop;
380382

381383
function getSipPeers()
382384
{
383-
$loop = React\EventLoop\Factory::create();
384-
$factory = new Clue\React\Ami\Factory($loop);
385+
$factory = new Clue\React\Ami\Factory();
385386

386387
$target = 'name:password@localhost';
387388
$promise = $factory->createClient($target)->then(function (Clue\React\Ami\Client $client) {
@@ -393,7 +394,7 @@ function getSipPeers()
393394
return $ret;
394395
});
395396

396-
return Block\await($promise, $loop, 5.0);
397+
return Block\await($promise, Loop::get(), 5.0);
397398
}
398399
```
399400

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
"require": {
1414
"php": ">=5.3",
1515
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
16-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
16+
"react/event-loop": "^1.2",
1717
"react/promise": "^2.0 || ^1.1",
18-
"react/socket": "^1.0 || ^0.8.2"
18+
"react/socket": "^1.8"
1919
},
2020
"require-dev": {
2121
"clue/block-react": "^1.2",

examples/commands.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
use Clue\React\Ami\Client;
55
use Clue\React\Ami\ActionSender;
66
use Clue\React\Ami\Protocol\Response;
7+
use React\EventLoop\Loop;
78

89
require __DIR__ . '/../vendor/autoload.php';
910

10-
$loop = React\EventLoop\Factory::create();
11-
$factory = new Factory($loop);
11+
$factory = new Factory();
1212

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

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

@@ -22,13 +22,13 @@
2222
echo 'Commands: ' . implode(', ', array_keys($response->getFields())) . PHP_EOL;
2323
});
2424

25-
$client->on('close', function() use ($loop) {
25+
$client->on('close', function() {
2626
echo 'Closed' . PHP_EOL;
2727

28-
$loop->removeReadStream(STDIN);
28+
Loop::removeReadStream(STDIN);
2929
});
3030

31-
$loop->addReadStream(STDIN, function () use ($sender) {
31+
Loop::addReadStream(STDIN, function () use ($sender) {
3232
$line = trim(fread(STDIN, 4096));
3333
echo '<' . $line . PHP_EOL;
3434

@@ -42,5 +42,3 @@ function (Exception $error) use ($line) {
4242
);
4343
});
4444
}, 'var_dump');
45-
46-
$loop->run();

examples/events.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88

99
require __DIR__ . '/../vendor/autoload.php';
1010

11-
$loop = React\EventLoop\Factory::create();
12-
$factory = new Factory($loop);
11+
$factory = new Factory();
1312

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

1615
$factory->createClient($target)->then(
17-
function (Client $client) use ($loop) {
16+
function (Client $client) {
1817
echo 'Client connected ' . PHP_EOL;
1918

2019
$sender = new ActionSender($client);
@@ -32,5 +31,3 @@ function (Exception $error) {
3231
echo 'Connection error: ' . $error;
3332
}
3433
);
35-
36-
$loop->run();

examples/peers.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
require __DIR__ . '/../vendor/autoload.php';
99

10-
$loop = React\EventLoop\Factory::create();
11-
$factory = new Factory($loop);
10+
$factory = new Factory();
1211

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

15-
$factory->createClient($target)->then(function (Client $client) use ($loop) {
14+
$factory->createClient($target)->then(function (Client $client) {
1615
echo 'Successfully connected' . PHP_EOL;
1716

1817
$collector = new ActionSender($client);
@@ -24,5 +23,3 @@
2423
echo 'found ' . count($peers) . ' peers' . PHP_EOL;
2524
});
2625
}, 'var_dump');
27-
28-
$loop->run();

src/Factory.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@
99

1010
/**
1111
* The `Factory` is responsible for creating your [`Client`](#client) instance.
12-
* It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
1312
*
1413
* ```php
15-
* $loop = React\EventLoop\Factory::create();
16-
* $factory = new Clue\React\Ami\Factory($loop);
14+
* $factory = new Clue\React\Ami\Factory();
1715
* ```
1816
*
17+
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
18+
* pass the event loop instance to use for this object. You can use a `null` value
19+
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
20+
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
21+
* given event loop instance.
22+
*
1923
* If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
2024
* proxy servers etc.), you can explicitly pass a custom instance of the
2125
* [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
2226
*
2327
* ```php
24-
* $connector = new React\Socket\Connector($loop, array(
28+
* $connector = new React\Socket\Connector(null, array(
2529
* 'dns' => '127.0.0.1',
2630
* 'tcp' => array(
2731
* 'bindto' => '192.168.10.1:0'
@@ -32,14 +36,14 @@
3236
* )
3337
* ));
3438
*
35-
* $factory = new Clue\React\Ami\Factory($loop, $connector);
39+
* $factory = new Clue\React\Ami\Factory(null, $connector);
3640
* ```
3741
*/
3842
class Factory
3943
{
4044
private $connector;
4145

42-
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null)
46+
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null)
4347
{
4448
if ($connector === null) {
4549
$connector = new Connector($loop);

tests/FactoryTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ class FactoryTest extends TestCase
1515
*/
1616
public function setUpFactory()
1717
{
18-
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
1918
$this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
2019

21-
$this->factory = new Factory($loop, $this->tcp);
20+
$this->factory = new Factory(null, $this->tcp);
2221
}
2322

2423
public function testDefaultCtorCreatesConnectorAutomatically()
2524
{
26-
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
27-
$this->factory = new Factory($loop);
25+
$this->factory = new Factory();
2826

2927
$ref = new \ReflectionProperty($this->factory, 'connector');
3028
$ref->setAccessible(true);

0 commit comments

Comments
 (0)