Skip to content

Commit

Permalink
Merge pull request #56 from clue-labs/default-loop
Browse files Browse the repository at this point in the history
Simplify usage by supporting new default loop
  • Loading branch information
clue authored Aug 1, 2021
2 parents 1e805e5 + 636e151 commit 36e3972
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 156 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,34 @@ the [examples](examples) to get started.
### Factory

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

```php
$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Clue\React\Quassel\Factory();
```

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

If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
proxy servers etc.), you can explicitly pass a custom instance of the
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):

```php
$factory = new Factory($loop, $connector);
$connector = new React\Socket\Connector(null, array(
'dns' => '127.0.0.1',
'tcp' => array(
'bindto' => '192.168.10.1:0'
),
'tls' => array(
'verify_peer' => false,
'verify_peer_name' => false
)
));

$factory = new Clue\React\Quassel\Factory(null, $connector);
```

#### createClient()
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"require": {
"php": ">=5.3",
"clue/qdatastream": "^0.8",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"react/event-loop": "^1.2",
"react/promise": "~2.0|~1.1",
"react/socket": "^1.0 || ^0.8 || ^0.7",
"react/stream": "^1.0 || ^0.7 || ^0.6 || ^0.5 || ^0.4.6"
"react/socket": "^1.8",
"react/stream": "^1.2"
},
"require-dev": {
"clue/block-react": "^1.1",
Expand Down
5 changes: 1 addition & 4 deletions examples/01-channels.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
echo 'Password: ';
$pass = trim(fgets(STDIN));

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

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

Expand Down Expand Up @@ -90,5 +89,3 @@
})->then(null, function ($e) {
echo $e;
});

$loop->run();
5 changes: 1 addition & 4 deletions examples/02-chatbot.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
die('Keyword MUST contain at least 3 characters to avoid excessive spam');
}

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

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

Expand Down Expand Up @@ -61,5 +60,3 @@
})->then(null, function ($e) {
echo $e;
});

$loop->run();
5 changes: 1 addition & 4 deletions examples/03-pingbot.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
echo 'Password: ';
$pass = trim(fgets(STDIN));

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

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

Expand Down Expand Up @@ -127,5 +126,3 @@
})->then(null, function ($e) {
echo $e;
});

$loop->run();
5 changes: 1 addition & 4 deletions examples/04-connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
echo 'Password: ';
$pass = trim(fgets(STDIN));

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

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

Expand Down Expand Up @@ -88,5 +87,3 @@
})->then(null, function ($e) {
echo $e;
});

$loop->run();
5 changes: 1 addition & 4 deletions examples/05-backlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
echo 'Channel to export (empty=all): ';
$channel = trim(fgets(STDIN));

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

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

Expand Down Expand Up @@ -96,5 +95,3 @@
})->then(null, function ($e) {
echo $e;
});

$loop->run();
6 changes: 1 addition & 5 deletions examples/11-debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Clue\React\Quassel\Factory;
use Clue\React\Quassel\Client;
use Clue\React\Quassel\Io\Protocol;
use Clue\React\Quassel\Models\BufferInfo;

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

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

echo '[1/5] Connecting' . PHP_EOL;
$factory->createClient($host)->then(function (Client $client) use ($user) {
Expand Down Expand Up @@ -110,5 +108,3 @@
})->then(null, function ($e) {
echo $e;
});

$loop->run();
14 changes: 12 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Clue\React\Quassel\Io\Prober;
use Clue\React\Quassel\Io\Protocol;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise;
use React\Socket\ConnectorInterface;
Expand All @@ -13,15 +14,24 @@

class Factory
{
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, Prober $prober = null)
/** @var LoopInterface */
private $loop;

/** @var ConnectorInterface */
private $connector;

/** @var Prober */
private $prober;

public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, Prober $prober = null)
{
$this->loop = $loop ?: Loop::get();
if ($connector === null) {
$connector = new Connector($loop);
}
if ($prober === null) {
$prober = new Prober();
}
$this->loop = $loop;
$this->connector = $connector;
$this->prober = $prober;
}
Expand Down
Loading

0 comments on commit 36e3972

Please sign in to comment.