8000 Merge pull request #42 from clue-labs/default-loop · reactphp/datagram@70c5dc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 70c5dc0

Browse files
authored
Merge pull request #42 from clue-labs/default-loop
Simplify usage by supporting new default loop
2 parents 0410d08 + c85436d commit 70c5dc0

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

README.md

Lines changed: 1 addition & 4 deletions
< 8000 /tr>
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ Once [installed](#install), you can use the following code to connect to an UDP
1010
`localhost:1234` and send and receive UDP datagrams:
1111

1212
```php
13-
$loop = React\EventLoop\Factory::create();
14-
$factory = new React\Datagram\Factory($loop);
13+
$factory = new React\Datagram\Factory();
1514

1615
$factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $client) {
1716
$client->send('first');
@@ -20,8 +19,6 @@ $factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $
2019
echo 'received "' . $message . '" from ' . $serverAddress. PHP_EOL;
2120
});
2221
});
23-
24-
$loop->run();
2522
```
2623

2724
See also the [examples](examples).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"require": {
4040
"php": ">=5.3",
4141
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
42-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
42+
"react/event-loop": "^1.2",
4343
"react/dns": "^1.7",
4444
"react/promise": "~2.1|~1.2"
4545
},

examples/client.php

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

3+
use React\EventLoop\Loop;
4+
35
require_once __DIR__.'/../vendor/autoload.php';
46

5-
$loop = React\EventLoop\Factory::create();
6-
$factory = new React\Datagram\Factory($loop);
7+
$factory = new React\Datagram\Factory();
78

8-
$factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $client) use ($loop) {
9+
$factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $client) {
910
$client->send('first');
1011

1112
$client->on('message', function($message, $serverAddress, $client) {
@@ -17,24 +18,22 @@
1718
});
1819

1920
$n = 0;
20-
$tid = $loop->addPeriodicTimer(2.0, function() use ($client, &$n) {
21+
$tid = Loop::addPeriodicTimer(2.0, function() use ($client, &$n) {
2122
$client->send('tick' . ++$n);
2223
});
2324

2425
// read input from STDIN and forward everything to server
25-
$loop->addReadStream(STDIN, function () use ($client, $loop, $tid) {
26+
Loop::addReadStream(STDIN, function () use ($client, $tid) {
2627
$msg = fgets(STDIN, 2000);
2728
if ($msg === false) {
2829
// EOF => flush client and stop perodic sending and waiting for input
2930
$client->end();
30-
$loop->cancelTimer($tid);
31-
$loop->removeReadStream(STDIN);
31+
Loop::cancelTimer($tid);
32+
Loop::removeReadStream(STDIN);
3233
} else {
3334
$client->send(trim($msg));
3435
}
3536
});
3637
}, function($error) {
3738
echo 'ERROR: ' . $error->getMessage() . PHP_EOL;
3839
});
39-
40-
$loop->run();

examples/server.php

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

33
require_once __DIR__.'/../vendor/autoload.php';
44

5-
$loop = React\EventLoop\Factory::create();
6-
$factory = new React\Datagram\Factory($loop);
5+
$factory = new React\Datagram\Factory();
76

87
$factory->createServer('localhost:1234')->then(function (React\Datagram\Socket $server) {
98
$server->on('message', function($message, $address, $server) {
@@ -12,5 +11,3 @@
1211
echo 'client ' . $address . ': ' . $message . PHP_EOL;
1312
});
1413
});
15-
16-
$loop->run();

src/Factory.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use React\Dns\Config\Config as DnsConfig;
77
use React\Dns\Resolver\Factory as DnsFactory;
88
use React\Dns\Resolver\ResolverInterface;
9+
use React\EventLoop\Loop;
910
use React\EventLoop\LoopInterface;
1011
use React\Promise;
1112
use React\Promise\CancellablePromiseInterface;
@@ -18,13 +19,20 @@ class Factory
1819

1920
/**
2021
*
21-
* @param LoopInterface $loop
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
2229
* @param ?ResolverInterface $resolver Resolver instance to use. Will otherwise
2330
* try to load the system default DNS config or fall back to using
2431
* Google's public DNS 8.8.8.8
2532
*/
26-
public function __construct(LoopInterface $loop, ResolverInterface $resolver = null)
33+
public function __construct(LoopInterface $loop = null, ResolverInterface $resolver = null)
2734
{
35+
$loop = $loop ?: Loop::get();
2836
if ($resolver === null) {
2937
// try to load nameservers from system config or default to Google's public DNS
3038
$config = DnsConfig::loadSystemConfigBlocking();

tests/FactoryTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace React\Tests\Datagram;
44

5-
use React\Datagram\Socket;
6-
use React\Datagram\Factory;
75
use Clue\React\Block;
6+
use React\Datagram\Factory;
7+
use React\Datagram\Socket;
88
use React\Promise;
99

1010
class FactoryTest extends TestCase
@@ -23,6 +23,17 @@ public function setUpFactory()
2323
$this->factory = new Factory($this->loop, $this->resolver);
2424
}
2525

26+
public function testConstructWithoutLoopAssignsLoopAutomatically()
27+
{
28+
$factory = new Factory();
29+
30+
$ref = new \ReflectionProperty($factory, 'loop');
31+
$ref->setAccessible(true);
32+
$loop = $ref->getValue($factory);
33+
34+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
35+
}
36+
2637
public function testCreateClient()
2738
{
2839
$this->resolver->expects($this->never())->method('resolve');

0 commit comments

Comments
 (0)
0