Skip to content

Commit 8bed9ef

Browse files
authored
Merge pull request #134 from clue-labs/default-loop
Simplify usage by supporting new default loop
2 parents f4f680a + 75c4e4f commit 8bed9ef

File tree

8 files changed

+53
-48
lines changed

8 files changed

+53
-48
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ It is written in pure PHP and does not require any extensions.
3333
This example runs a simple `SELECT` query and dumps all the records from a `book` table:
3434

3535
```php
36-
$loop = React\EventLoop\Factory::create();
37-
$factory = new Factory($loop);
36+
$factory = new React\MySQL\Factory();
3837

3938
$uri = 'test:test@localhost/test';
4039
$connection = $factory->createLazyConnection($uri);
@@ -51,8 +50,6 @@ $connection->query('SELECT * FROM book')->then(
5150
);
5251

5352
$connection->quit();
54-
55-
$loop->run();
5653
```
5754

5855
See also the [examples](examples).
@@ -62,19 +59,23 @@ See also the [examples](examples).
6259
### Factory
6360

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

6763
```php
68-
$loop = \React\EventLoop\Factory::create();
69-
$factory = new Factory($loop);
64+
$factory = new React\MySQL\Factory();
7065
```
7166

67+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
68+
pass the event loop instance to use for this object. You can use a `null` value
69+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
70+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
71+
given event loop instance.
72+
7273
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
7374
proxy servers etc.), you can explicitly pass a custom instance of the
7475
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
7576

7677
```php
77-
$connector = new \React\Socket\Connector($loop, array(
78+
$connector = new React\Socket\Connector(null, array(
7879
'dns' => '127.0.0.1',
7980
'tcp' => array(
8081
'bindto' => '192.168.10.1:0'
@@ -85,7 +86,7 @@ $connector = new \React\Socket\Connector($loop, array(
8586
)
8687
));
8788

88-
$factory = new Factory($loop, $connector);
89+
$factory = new React\MySQL\Factory(null, $connector);
8990
```
9091

9192
#### createConnection()
@@ -120,7 +121,7 @@ connection attempt and/or MySQL authentication.
120121
```php
121122
$promise = $factory->createConnection($url);
122123

123-
$loop->addTimer(3.0, function () use ($promise) {
124+
Loop::addTimer(3.0, function () use ($promise) {
124125
$promise->cancel();
125126
});
126127
```

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"require": {
77
"php": ">=5.4.0",
88
"evenement/evenement": "^3.0 || ^2.1 || ^1.1",
9-
"react/event-loop": "^1.0 || ^0.5",
9+
"react/event-loop": "^1.2",
1010
"react/promise": "^2.7",
1111
"react/promise-stream": "^1.1",
1212
"react/promise-timer": "^1.5",
13-
"react/socket": "^1.1"
13+
"react/socket": "^1.8"
1414
},
1515
"require-dev": {
1616
"clue/block-react": "^1.2",

examples/01-query.php

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

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

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

1110
$uri = 'test:test@localhost/test';
1211
$query = isset($argv[1]) ? $argv[1] : 'select * from book';
@@ -33,5 +32,3 @@
3332
});
3433

3534
$connection->quit();
36-
37-
$loop->run();

examples/02-query-stream.php

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

77
require __DIR__ . '/../vendor/autoload.php';
88

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

1211
$uri = 'test:test@localhost/test';
1312
$query = isset($argv[1]) ? $argv[1] : 'select * from book';
@@ -30,5 +29,3 @@
3029
});
3130

3231
$connection->quit();
33-
34-
$loop->run();

examples/11-interactive.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77

88
require __DIR__ . '/../vendor/autoload.php';
99

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

1312
$uri = 'test:test@localhost/test';
1413

1514
// open a STDIN stream to read keyboard input (not supported on Windows)
16-
$stdin = new ReadableResourceStream(STDIN, $loop);
15+
$stdin = new ReadableResourceStream(STDIN);
1716
$stdin->pause();
1817

1918
//create a mysql connection for executing queries
@@ -86,5 +85,3 @@
8685
echo 'Connection error: ' . $e->getMessage() . PHP_EOL;
8786
$stdin->close();
8887
});
89-
90-
$loop->run();

examples/12-slow-stream.php

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

33
// $ php examples/12-slow-stream.php "SHOW VARIABLES"
44

5+
use React\EventLoop\Loop;
56
use React\MySQL\ConnectionInterface;
67
use React\MySQL\Factory;
78

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

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

1313
$uri = 'test:test@localhost/test';
1414
$query = isset($argv[1]) ? $argv[1] : 'select * from book';
1515

1616
//create a mysql connection for executing query
17-
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) use ($query, $loop) {
17+
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) use ($query) {
1818
// The protocol parser reads rather large chunked from the underlying connection
1919
// and as such can yield multiple (dozens to hundreds) rows from a single data
2020
// chunk. We try to artifically limit the stream chunk size here to try to
@@ -44,13 +44,13 @@
4444
$stream = $connection->queryStream($query);
4545

4646
$throttle = null;
47-
$stream->on('data', function ($row) use ($loop, &$throttle, $stream) {
47+
$stream->on('data', function ($row) use (&$throttle, $stream) {
4848
echo json_encode($row, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
4949

5050
// simple throttle mechanism: explicitly pause the result stream and
5151
// resume it again after some time.
5252
if ($throttle === null) {
53-
$throttle = $loop->addTimer(1.0, function () use ($stream, &$throttle) {
53+
$throttle = Loop::addTimer(1.0, function () use ($stream, &$throttle) {
5454
$throttle = null;
5555
$stream->resume();
5656
});
@@ -62,16 +62,14 @@
6262
echo 'Error: ' . $e->getMessage() . PHP_EOL;
6363
});
6464

65-
$stream->on('close', function () use ($loop, &$throttle) {
65+
$stream->on('close', function () use (&$throttle) {
6666
echo 'CLOSED' . PHP_EOL;
6767

6868
if ($throttle) {
69-
$loop->cancelTimer($throttle);
69+
Loop::cancelTimer($throttle);
7070
$throttle = null;
7171
}
7272
});
7373

7474
$connection->quit();
7575
}, 'printf');
76-
77-
$loop->run();

src/Factory.php

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

33
namespace React\MySQL;
44

5+
use React\EventLoop\Loop;
56
use React\EventLoop\LoopInterface;
67
use React\MySQL\Commands\AuthenticateCommand;
78
use React\MySQL\Io\Connection;
@@ -17,24 +18,31 @@
1718

1819
class Factory
1920
{
21+
/** @var LoopInterface */
2022
private $loop;
23+
24+
/** @var ConnectorInterface */
2125
private $connector;
2226

2327
/**
2428
* The `Factory` is responsible for creating your [`ConnectionInterface`](#connectioninterface) instance.
25-
* It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
2629
*
2730
* ```php
28-
* $loop = \React\EventLoop\Factory::create();
29-
* $factory = new Factory($loop);
31+
* $factory = new React\MySQL\Factory();
3032
* ```
3133
*
34+
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
35+
* pass the event loop instance to use for this object. You can use a `null` value
36+
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
37+
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
38+
* given event loop instance.
39+
*
3240
* If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
3341
* proxy servers etc.), you can explicitly pass a custom instance of the
3442
* [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
3543
*
3644
* ```php
37-
* $connector = new \React\Socket\Connector($loop, array(
45+
* $connector = new React\Socket\Connector(null, array(
3846
* 'dns' => '127.0.0.1',
3947
* 'tcp' => array(
4048
* 'bindto' => '192.168.10.1:0'
@@ -45,20 +53,16 @@ class Factory
4553
* )
4654
* ));
4755
*
48-
* $factory = new Factory($loop, $connector);
56+
* $factory = new React\MySQL\Factory(null, $connector);
4957
* ```
5058
*
51-
* @param LoopInterface $loop
52-
* @param ConnectorInterface|null $connector
59+
* @param ?LoopInterface $loop
60+
* @param ?ConnectorInterface $connector
5361
*/
54-
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null)
62+
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null)
5563
{
56-
if ($connector === null) {
57-
$connector = new Connector($loop);
58-
}
59-
60-
$this->loop = $loop;
61-
$this->connector = $connector;
64+
$this->loop = $loop ?: Loop::get();
65+
$this->connector = $connector ?: new Connector($this->loop);
6266
}
6367

6468
/**

tests/FactoryTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99

1010
class FactoryTest extends BaseTestCase
1111
{
12+
public function testConstructWithoutLoopAssignsLoopAutomatically()
13+
{
14+
$factory = new Factory();
15+
16+
$ref = new \ReflectionProperty($factory, 'loop');
17+
$ref->setAccessible(true);
18+
$loop = $ref->getValue($factory);
19+
20+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
21+
}
22+
1223
public function testConnectWillUseHostAndDefaultPort()
1324
{
1425
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

0 commit comments

Comments
 (0)