8000 Merge pull request #114 from SimonFrings/eventloop · clue/reactphp-redis@d3e55bc · GitHub
[go: up one dir, main page]

Skip to content

Commit d3e55bc

Browse files
authored
Merge pull request #114 from SimonFrings/eventloop
Simplify usage by supporting new default loop
2 parents c4a8cbb + bd94553 commit d3e55bc

File tree

9 files changed

+64
-54
lines changed

9 files changed

+64
-54
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ Once [installed](#install), you can use the following code to connect to your
6060
local Redis server and send some requests:
6161

6262
```php
63-
$loop = React\EventLoop\Factory::create();
64-
$factory = new Clue\React\Redis\Factory($loop);
63+
$factory = new Clue\React\Redis\Factory();
6564

6665
$client = $factory->createLazyClient('localhost');
6766
$client->set('greeting', 'Hello world');
@@ -78,8 +77,6 @@ $client->incr('invocation')->then(function ($n) {
7877

7978
// end connection once all pending requests have been resolved
8079
$client->end();
81-
82-
$loop->run();
8380
```
8481

8582
See also the [examples](examples).
@@ -89,18 +86,22 @@ See also the [examples](examples).
8986
### Factory
9087

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

9490
```php
95-
$loop = \React\EventLoop\Factory::create();
96-
$factory = new \Clue\React\Redis\Factory($loop);
91+
$factory = new Clue\React\Redis\Factory();
9792
```
9893

94+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
95+
pass the event loop instance to use for this object. You can use a `null` value
96+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
97+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
98+
given event loop instance.
99+
99100
If you need custom DNS, proxy or TLS settings, you can explicitly pass a
100101
custom instance of the [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
101102

102103
```php
103-
$connector = new \React\Socket\Connector($loop, array(
104+
$connector = new React\Socket\Connector(null, array(
104105
'dns' => '127.0.0.1',
105106
'tcp' => array(
106107
'bindto' => '192.168.10.1:0'
@@ -111,7 +112,7 @@ $connector = new \React\Socket\Connector($loop, array(
111112
)
112113
));
113114

114-
$factory = new Factory($loop, $connector);
115+
$factory = new Clue\React\Redis\Factory(null, $connector);
115116
```
116117

117118
#### createClient()
@@ -146,7 +147,7 @@ connection attempt and/or Redis authentication.
146147
```php
147148
$promise = $factory->createClient($redisUri);
148149

149-
$loop->addTimer(3.0, function () use ($promise) {
150+
Loop::addTimer(3.0, function () use ($promise) {
150151
$promise->cancel();
151152
});
152153
```
@@ -466,7 +467,7 @@ respectively:
466467
```php
467468
$client->subscribe('user');
468469

469-
$loop->addTimer(60.0, function () use ($client) {
470+
Loop::addTimer(60.0, function () use ($client) {
470471
$client->unsubscribe('user');
471472
});
472473
```

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
"php": ">=5.3",
1515
"clue/redis-protocol": "0.3.*",
1616
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
17-
"react/event-loop": "^1.0 || ^0.5",
17+
"react/event-loop": "^1.2",
1818
"react/promise": "^2.0 || ^1.1",
1919
"react/promise-timer": "^1.5",
20-
"react/socket": "^1.1"
20+
"react/socket": "^1.8"
2121
},
2222
"require-dev": {
2323
"clue/block-react": "^1.1",

examples/cli.php

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

33
use Clue\React\Redis\Client;
44
use Clue\React\Redis\Factory;
5+
use React\EventLoop\Loop;
56
use React\Promise\PromiseInterface;
67

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

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

1212
echo '# connecting to redis...' . PHP_EOL;
1313

14-
$factory->createClient('localhost')->then(function (Client $client) use ($loop) {
14+
$factory->createClient('localhost')->then(function (Client $client) {
1515
echo '# connected! Entering interactive mode, hit CTRL-D to quit' . PHP_EOL;
1616

17-
$loop->addReadStream(STDIN, function () use ($client, $loop) {
17+
Loop::addReadStream(STDIN, function () use ($client) {
1818
$line = fgets(STDIN);
1919
if ($line === false || $line === '') {
2020
echo '# CTRL-D -> Ending connection...' . PHP_EOL;
21-
$loop->removeReadStream(STDIN);
21+
Loop::removeReadStream(STDIN);
2222
return $client->end();
2323
}
2424

@@ -43,10 +43,10 @@
4343
});
4444
});
4545

46-
$client->on('close', function() use ($loop) {
46+
$client->on('close', function() {
4747
echo '## DISCONNECTED' . PHP_EOL;
4848

49-
$loop->removeReadStream(STDIN);
49+
Loop::removeReadStream(STDIN);
5050
});
5151
}, function (Exception $error) {
5252
echo 'CONNECTION ERROR: ' . $error->getMessage() . PHP_EOL;
@@ -55,5 +55,3 @@
5555
}
5656
exit(1);
5757
});
58-
59-
$loop->run();

examples/incr.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-
$factory = new Factory($loop);
7+
$factory = new Factory();
98

109
$client = $factory->createLazyClient('localhost');
1110
$client->incr('test');
@@ -21,5 +20,3 @@
2120
});
2221

2322
$client->end();
24-
25-
$loop->run();

examples/publish.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-
$factory = new Factory($loop);
7+
$factory = new Factory();
98

109
$channel = isset($argv[1]) ? $argv[1] : 'channel';
1110
$message = isset($argv[2]) ? $argv[2] : 'message';
@@ -22,5 +21,3 @@
2221
});
2322

2423
$client->end();
25-
26-
$loop->run();

examples/subscribe.php

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

33
use Clue\React\Redis\Factory;
4+
use React\EventLoop\Loop;
45

56
require __DIR__ . '/../vendor/autoload.php';
67

7-
$loop = React\EventLoop\Factory::create();
8-
$factory = new Factory($loop);
8+
$factory = new Factory();
99

1010
$channel = isset($argv[1]) ? $argv[1] : 'channel';
1111

@@ -22,17 +22,15 @@
2222
});
2323

2424
// automatically re-subscribe to channel on connection issues
25-
$client->on('unsubscribe', function ($channel) use ($client, $loop) {
25+
$client->on('unsubscribe', function ($channel) use ($client) {
2626
echo 'Unsubscribed from ' . $channel . PHP_EOL;
2727

28-
$loop->addPeriodicTimer(2.0, function ($timer) use ($client, $channel, $loop){
29-
$client->subscribe($channel)->then(function () use ($timer, $loop) {
28+
Loop::addPeriodicTimer(2.0, function ($timer) use ($client, $channel){
29+
$client->subscribe($channel)->then(function () use ($timer) {
3030
echo 'Now subscribed again' . PHP_EOL;
31-
$loop->cancelTimer($timer);
31+
Loop::cancelTimer($timer);
3232
}, function (Exception $e) {
3333
echo 'Unable to subscribe again: ' . $e->getMessage() . PHP_EOL;
3434
});
3535
});
3636
});
37-
38-
$loop->run();

src/Factory.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Clue\React\Redis;
44

55
use Clue\Redis\Protocol\Factory as ProtocolFactory;
6+
use React\EventLoop\Loop;
67
use React\EventLoop\LoopInterface;
78
use React\Promise\Deferred;
89
use React\Promise\Timer\TimeoutException;
@@ -13,29 +14,25 @@
1314

1415
class Factory
1516
{
17+
/** @var LoopInterface */
1618
private $loop;
19+
20+
/** @var ConnectorInterface */
1721
private $connector;
22+
23+
/** @var ProtocolFactory */
1824
private $protocol;
1925

2026
/**
21-
* @param LoopInterface $loop
22-
* @param ConnectorInterface|null $connector [optional] Connector to use.
23-
* Should be `null` in order to use default Connector.
24-
* @param ProtocolFactory|null $protocol
27+
* @param ?LoopInterface $loop
28+
* @param ?ConnectorInterface $connector
29+
* @param ?ProtocolFactory $protocol
2530
*/
26-
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, ProtocolFactory $protocol = null)
31+
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, ProtocolFactory $protocol = null)
2732
{
28-
if ($connector === null) {
29-
$connector = new Connector($loop);
30-
}
31-
32-
if ($protocol === null) {
33-
$protocol = new ProtocolFactory();
34-
}
35-
36-
$this->loop = $loop;
37-
$this->connector = $connector;
38-
$this->protocol = $protocol;
33+
$this->loop = $loop ?: Loop::get();
34+
$this->connector = $connector ?: new Connector($this->loop);
35+
$this->protocol = $protocol ?: new ProtocolFactory();
3936
}
4037

4138
/**

tests/FactoryLazyClientTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ public function setUpFactory()
2121
$this->factory = new Factory($this->loop, $this->connector);
2222
}
2323

24+
public function testConstructWithoutLoopAssignsLoopAutomatically()
25+
{
26+
$factory = new Factory();
27+
28+
$ref = new \ReflectionProperty($factory, 'loop');
29+
$ref->setAccessible(true);
30+
$loop = $ref->getValue($factory);
31+
32+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
33+
}
34+
2435
public function testWillConnectWithDefaultPort()
2536
{
2637
$this->connector->expects($this->never())->method('connect')->with('redis.example.com:6379')->willReturn(Promise\reject(new \RuntimeException()));

tests/FactoryStreamingClientTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ public function setUpFactory()
2222
$this->factory = new Factory($this->loop, $this->connector);
2323
}
2424

25+
public function testConstructWithoutLoopAssignsLoopAutomatically()
26+
{
27+
$factory = new Factory();
28+
29+
$ref = new \ReflectionProperty($factory, 'loop');
30+
$ref->setAccessible(true);
31+
$loop = $ref->getValue($factory);
32+
33+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
34+
}
35+
2536
/**
2637
* @doesNotPerformAssertions
2738
*/

0 commit comments

Comments
 (0)
0