Skip to content

Commit 52cec81

Browse files
PaulRotmannSimonFrings
authored andcommitted
Simplify usage by supporting new default loop
1 parent 2022052 commit 52cec81

File tree

7 files changed

+66
-36
lines changed

7 files changed

+66
-36
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,12 @@ as defined in [RFC 6763](http://tools.ietf.org/html/rfc6763).
3434
Once [installed](#install), you can use the following code to look up the address of a local domain name:
3535

3636
```php
37-
$loop = React\EventLoop\Factory::create();
38-
$factory = new Factory($loop);
37+
$factory = new Factory();
3938
$resolver = $factory->createResolver();
4039

4140
$resolver->lookup('hostname.local')->then(function ($ip) {
4241
echo 'Found: ' . $ip . PHP_EOL;
4342
});
44-
45-
$loop->run();
4643
```
4744

4845
See also the [examples](examples).
@@ -52,13 +49,17 @@ See also the [examples](examples).
5249
### Factory
5350

5451
The `Factory` is responsible for creating your [`Resolver`](#resolver) instance.
55-
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
5652

5753
```php
58-
$loop = React\EventLoop\Factory::create();
59-
$factory = new Factory($loop);
54+
$factory = new Factory();
6055
```
6156

57+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
58+
pass the event loop instance to use for this object. You can use a `null` value
59+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
60+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
61+
given event loop instance.
62+
6263
#### createResolver()
6364

6465
The `createResolver()` method can be used to create a mDNS resolver instance that sends multicast DNS queries and waits for incoming unicast DNS responses. It returns a [`Resolver`](#resolver) instance.
@@ -106,8 +107,7 @@ The resulting blocking code could look something like this:
106107
```php
107108
use Clue\React\Block;
108109

109-
$loop = React\EventLoop\Factory::create();
110-
$factory = new Factory($loop);
110+
$factory = new Factory();
111111
$resolver = $factory->createResolver();
112112

113113
$promise = $resolver->lookup('me.local');

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"php": ">=5.3",
2121
"clue/multicast-react": "^1.0 || ^0.2",
2222
"react/dns": "~0.4.0|~0.3.0",
23-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
23+
"react/event-loop": "^1.2",
2424
"react/promise": "^2.1 || ^1.2.1"
2525
},
2626
"require-dev": {

examples/lookup.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
$name = isset($argv[1]) ? $argv[1] : 'me.local';
66

7-
$loop = React\EventLoop\Factory::create();
8-
$factory = new Clue\React\Mdns\Factory($loop);
7+
$factory = new Clue\React\Mdns\Factory();
98
$mdns = $factory->createResolver();
109

1110
$mdns->resolve($name)->then('e', 'e');
1211

1312
function e($v) {
1413
echo $v . PHP_EOL;
1514
}
16-
17-
$loop->run();

src/Factory.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Clue\React\Mdns;
44

5+
use React\EventLoop\Loop;
56
use React\EventLoop\LoopInterface;
67
use React\Dns\Query\ExecutorInterface;
78
use React\Dns\Resolver\Resolver;
@@ -10,17 +11,20 @@ class Factory
1011
{
1112
const DNS = '224.0.0.251:5353';
1213

14+
/** @var LoopInterface */
1315
private $loop;
16+
17+
/** @var ExecutorInterface */
1418
private $executor;
1519

16-
public function __construct(LoopInterface $loop, ExecutorInterface $executor = null)
20+
/**
21+
* @param ?LoopInterface $loop
22+
* @param ?ExecutorInterface $executor
23+
*/
24+
public function __construct(LoopInterface $loop = null, ExecutorInterface $executor = null)
1725
{
18-
if ($executor === null) {
19-
$executor = new MulticastExecutor($loop);
20-
}
21-
22-
$this->loop = $loop;
23-
$this->executor = $executor;
26+
$this->loop = $loop ?: Loop::get();
27+
$this->executor = $executor ?: new MulticastExecutor($loop);
2428
}
2529

2630
public function createResolver()

src/MulticastExecutor.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use React\Dns\Model\Message;
77
use React\Dns\Protocol\Parser;
88
use React\Dns\Protocol\BinaryDumper;
9+
use React\EventLoop\Loop;
910
use React\EventLoop\LoopInterface;
1011
use React\Promise\Deferred;
1112
use Clue\React\Multicast\Factory as DatagramFactory;
@@ -21,29 +22,35 @@
2122
*/
2223
class MulticastExecutor implements ExecutorInterface
2324
{
25+
/** @var LoopInterface */
2426
private $loop;
27+
28+
/** @var Parser */
2529
private $parser;
30+
31+
/** @var BinaryDumper */
2632
private $dumper;
33+
34+
/** @var number */
2735
private $timeout;
36+
37+
/** @var DatagramFactory */
2838
private $factory;
2939

30-
public function __construct(LoopInterface $loop, Parser $parser = null, BinaryDumper $dumper = null, $timeout = 5, DatagramFactory $factory = null)
40+
/**
41+
* @param ?LoopInterface $loop
42+
* @param ?Parser $parser
43+
* @param ?BinaryDumper $dumper
44+
* @param number $timeout
45+
* @param ?DatagramFactory $factory
46+
*/
47+
public function __construct(LoopInterface $loop = null, Parser $parser = null, BinaryDumper $dumper = null, $timeout = 5, DatagramFactory $factory = null)
3148
{
32-
if ($parser === null) {
33-
$parser = new Parser();
34-
}
35-
if ($dumper === null) {
36-
$dumper = new BinaryDumper();
37-
}
38-
if ($factory === null) {
39-
$factory = new DatagramFactory($loop);
40-
}
41-
42-
$this->loop = $loop;
43-
$this->parser = $parser;
44-
$this->dumper = $dumper;
49+
$this->loop = $loop ?: Loop::get();
50+
$this->parser = $parser ?: new Parser();
51+
$this->dumper = $dumper ?: new BinaryDumper();
4552
$this->timeout = $timeout;
46-
$this->factory = $factory;
53+
$this->factory = $factory ?: new DatagramFactory($this->loop);
4754
}
4855

4956
public function query($nameserver, Query $query)

tests/FactoryTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,15 @@ public function testCreateResolver()
1515

1616
$this->assertInstanceOf('React\Dns\Resolver\Resolver', $resolver);
1717
}
18+
19+
public function testConstructWithoutLoopAssignsLoopAutomatically()
20+
{
21+
$factory = new Factory();
22+
23+
$ref = new \ReflectionProperty($factory, 'loop');
24+
$ref->setAccessible(true);
25+
$loop = $ref->getValue($factory);
26+
27+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
28+
}
1829
}

tests/MulticastExecutorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ public function testQueryWillReturnPromise()
3131
$this->assertInstanceOf('React\Promise\PromiseInterface', $ret);
3232
}
3333

34+
public function testConstructWithoutLoopAssignsLoopAutomatically()
35+
{
36+
$executor = new MulticastExecutor();
37+
38+
$ref = new \ReflectionProperty($executor, 'loop');
39+
$ref->setAccessible(true);
40+
$loop = $ref->getValue($executor);
41+
42+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
43+
}
44+
3445
public function testCancellingPromiseWillCloseSocketAndReject()
3546
{
3647
$nameserver = Factory::DNS;

0 commit comments

Comments
 (0)