Skip to content

Commit 36e3972

Browse files
authored
Merge pull request #56 from clue-labs/default-loop
Simplify usage by supporting new default loop
2 parents 1e805e5 + 636e151 commit 36e3972

12 files changed

+150
-156
lines changed

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,34 @@ the [examples](examples) to get started.
6565
### Factory
6666

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

7069
```php
71-
$loop = \React\EventLoop\Factory::create();
72-
$factory = new Factory($loop);
70+
$factory = new Clue\React\Quassel\Factory();
7371
```
7472

75-
If you need custom DNS, proxy or TLS settings, you can explicitly pass a
76-
custom instance of the [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
73+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
74+
pass the event loop instance to use for this object. You can use a `null` value
75+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
76+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
77+
given event loop instance.
78+
79+
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
80+
proxy servers etc.), you can explicitly pass a custom instance of the
81+
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
7782

7883
```php
79-
$factory = new Factory($loop, $connector);
84+
$connector = new React\Socket\Connector(null, array(
85+
'dns' => '127.0.0.1',
86+
'tcp' => array(
87+
'bindto' => '192.168.10.1:0'
88+
),
89+
'tls' => array(
90+
'verify_peer' => false,
91+
'verify_peer_name' => false
92+
)
93+
));
94+
95+
$factory = new Clue\React\Quassel\Factory(null, $connector);
8096
```
8197

8298
#### createClient()

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
"require": {
2020
"php": ">=5.3",
2121
"clue/qdatastream": "^0.8",
22-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
22+
"react/event-loop": "^1.2",
2323
"react/promise": "~2.0|~1.1",
24-
"react/socket": "^1.0 || ^0.8 || ^0.7",
25-
"react/stream": "^1.0 || ^0.7 || ^0.6 || ^0.5 || ^0.4.6"
24+
"react/socket": "^1.8",
25+
"react/stream": "^1.2"
2626
},
2727
"require-dev": {
2828
"clue/block-react": "^1.1",

examples/01-channels.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
echo 'Password: ';
1818
$pass = trim(fgets(STDIN));
1919

20-
$loop = \React\EventLoop\Factory::create();
21-
$factory = new Factory($loop);
20+
$factory = new Factory();
2221

2322
$uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host;
2423

@@ -90,5 +89,3 @@
9089
})->then(null, function ($e) {
9190
echo $e;
9291
});
93-
94-
$loop->run();

examples/02-chatbot.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
die('Keyword MUST contain at least 3 characters to avoid excessive spam');
2626
}
2727

28-
$loop = \React\EventLoop\Factory::create();
29-
$factory = new Factory($loop);
28+
$factory = new Factory();
3029

3130
$uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host;
3231

@@ -61,5 +60,3 @@
6160
})->then(null, function ($e) {
6261
echo $e;
6362
});
64-
65-
$loop->run();

examples/03-pingbot.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
echo 'Password: ';
2020
$pass = trim(fgets(STDIN));
2121

22-
$loop = \React\EventLoop\Factory::create();
23-
$factory = new Factory($loop);
22+
$factory = new Factory();
2423

2524
$uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host;
2625

@@ -127,5 +126,3 @@
127126
})->then(null, function ($e) {
128127
echo $e;
129128
});
130-
131-
$loop->run();

examples/04-connect.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
echo 'Password: ';
2020
$pass = trim(fgets(STDIN));
2121

22-
$loop = \React\EventLoop\Factory::create();
23-
$factory = new Factory($loop);
22+
$factory = new Factory();
2423

2524
$uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host;
2625

@@ -88,5 +87,3 @@
8887
})->then(null, function ($e) {
8988
echo $e;
9089
});
91-
92-
$loop->run();

examples/05-backlog.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
echo 'Channel to export (empty=all): ';
2323
$channel = trim(fgets(STDIN));
2424

25-
$loop = \React\EventLoop\Factory::create();
26-
$factory = new Factory($loop);
25+
$factory = new Factory();
2726

2827
$uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host;
2928

@@ -96,5 +95,3 @@
9695
})->then(null, function ($e) {
9796
echo $e;
9897
});
99-
100-
$loop->run();

examples/11-debug.php

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

33
use Clue\React\Quassel\Factory;
44
use Clue\React\Quassel\Client;
5-
use Clue\React\Quassel\Io\Protocol;
65
use Clue\React\Quassel\Models\BufferInfo;
76

87
require __DIR__ . '/../vendor/autoload.php';
@@ -19,8 +18,7 @@
1918
echo 'Password: ';
2019
$user['password'] = trim(fgets(STDIN));
2120

22-
$loop = \React\EventLoop\Factory::create();
23-
$factory = new Factory($loop);
21+
$factory = new Factory();
2422

2523
echo '[1/5] Connecting' . PHP_EOL;
2624
$factory->createClient($host)->then(function (Client $client) use ($user) {
@@ -110,5 +108,3 @@
110108
})->then(null, function ($e) {
111109
echo $e;
112110
});
113-
114-
$loop->run();

src/Factory.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Clue\React\Quassel\Io\Prober;
66
use Clue\React\Quassel\Io\Protocol;
7+
use React\EventLoop\Loop;
78
use React\EventLoop\LoopInterface;
89
use React\Promise;
910
use React\Socket\ConnectorInterface;
@@ -13,15 +14,24 @@
1314

1415
class Factory
1516
{
16-
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, Prober $prober = null)
17+
/** @var LoopInterface */
18+
private $loop;
19+
20+
/** @var ConnectorInterface */
21+
private $connector;
22+
23+
/** @var Prober */
24+
private $prober;
25+
26+
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, Prober $prober = null)
1727
{
28+
$this->loop = $loop ?: Loop::get();
1829
if ($connector === null) {
1930
$connector = new Connector($loop);
2031
}
2132
if ($prober === null) {
2233
$prober = new Prober();
2334
}
24-
$this->loop = $loop;
2535
$this->connector = $connector;
2636
$this->prober = $prober;
2737
}

0 commit comments

Comments
 (0)