Skip to content

Commit 7f26ec8

Browse files
committed
Simplify usage by supporting new default loop
1 parent 5921932 commit 7f26ec8

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ as defined in [RFC 6763](http://tools.ietf.org/html/rfc6763).
3131
Once [installed](#install), you can use the following code to search all available UPnP devices in your network:
3232

3333
```php
34-
$loop = React\EventLoop\Factory::create();
35-
$client = new Client($loop);
34+
$client = new Clue\React\Ssdp\Client();
3635

3736
$client->search()->then(
3837
function () {
@@ -47,8 +46,6 @@ $client->search()->then(
4746
echo PHP_EOL;
4847
}
4948
);
50-
51-
$loop->run();
5249
```
5350

5451
See also the [examples](examples).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"require": {
2020
"php": ">=5.3",
2121
"clue/multicast-react": "^1.0 || ^0.2",
22-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
22+
"react/event-loop": "^1.2",
2323
"react/promise": "^2.0 || ^1.0"
2424
},
2525
"require-dev": {

examples/demo.php

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

55
require __DIR__ . '/../vendor/autoload.php';
66

7-
$loop = React\EventLoop\Factory::create();
8-
$client = new Client($loop);
7+
$client = new Client();
98

109
$client->search()->then(
1110
function () {
@@ -20,5 +19,3 @@ function ($progress) {
2019
echo PHP_EOL;
2120
}
2221
);
23-
24-
$loop->run();

src/Client.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,36 @@
22

33
namespace Clue\React\Ssdp;
44

5-
use React\EventLoop\LoopInterface;
65
use Clue\React\Multicast\Factory as MulticastFactory;
6+
use React\EventLoop\Loop;
7+
use React\EventLoop\LoopInterface;
78
use React\Promise\Deferred;
89
use RuntimeException;
910

1011
class Client
1112
{
1213
const ADDRESS = '239.255.255.250:1900';
1314

15+
/** @var LoopInterface */
1416
private $loop;
17+
18+
/** @var MulticastFactory */
1519
private $multicast;
1620

17-
public function __construct(LoopInterface $loop, MulticastFactory $multicast = null)
21+
/**
22+
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
23+
* pass the event loop instance to use for this object. You can use a `null` value
24+
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
25+
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
26+
* given event loop instance.
27+
*
28+
* @param ?LoopInterface $loop
29+
* @param ?MulticastFactory $multicast
30+
*/
31+
public function __construct(LoopInterface $loop = null, MulticastFactory $multicast = null)
1832
{
19-
if ($multicast === null) {
20-
$multicast = new MulticastFactory($loop);
21-
}
22-
$this->loop = $loop;
23-
$this->multicast = $multicast;
33+
$this->loop = $loop ?: Loop::get();
34+
$this->multicast = $multicast ?: new MulticastFactory($this->loop);
2435
}
2536

2637
public function search($searchTarget = 'ssdp:all', $mx = 2)

tests/ClientTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33
namespace Clue\Tests\React\Ssdp;
44

55
use Clue\React\Ssdp\Client;
6-
use React\EventLoop\Factory;
6+
use React\EventLoop\Loop;
77

88
class ClientTest extends TestCase
99
{
10+
public function testConstructWithoutLoopAssignsLoopAutomatically()
11+
{
12+
$client = new Client();
13+
14+
$ref = new \ReflectionProperty($client, 'loop');
15+
$ref->setAccessible(true);
16+
$loop = $ref->getValue($client);
17+
18+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
19+
}
20+
1021
/**
1122
* @doesNotPerformAssertions
1223
*/
@@ -52,12 +63,12 @@ interface_exists('React\EventLoop\TimerInterface') ? 'React\EventLoop\TimerInter
5263

5364
public function testSearchTimeout()
5465
{
55-
$loop = Factory::create();
66+
$loop = Loop::get();
5667
$client = new Client($loop);
5768

5869
$promise = $client->search('ssdp:all', 0.01);
5970

60-
$loop->run();
71+
Loop::run();
6172

6273
$promise->then($this->expectCallableOnce(), $this->expectCallableNever(), $this->expectCallableNever());
6374
}

0 commit comments

Comments
 (0)