Skip to content

Commit 9a3976b

Browse files
committed
Simplify API, add new MysqlClient and remove Factory
1 parent 6209d11 commit 9a3976b

12 files changed

+620
-622
lines changed

README.md

Lines changed: 46 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ It is written in pure PHP and does not require any extensions.
2222

2323
* [Quickstart example](#quickstart-example)
2424
* [Usage](#usage)
25-
* [Factory](#factory)
26-
* [createConnection()](#createconnection)
27-
* [createLazyConnection()](#createlazyconnection)
25+
* [MysqlClient](#mysqlclient)
26+
* [__construct()](#__construct)
2827
* [ConnectionInterface](#connectioninterface)
2928
* [query()](#query)
3029
* [queryStream()](#querystream)
@@ -45,11 +44,10 @@ This example runs a simple `SELECT` query and dumps all the records from a `book
4544

4645
require __DIR__ . '/vendor/autoload.php';
4746

48-
$factory = new React\MySQL\Factory();
49-
$connection = $factory->createLazyConnection('user:pass@localhost/bookstore');
47+
$mysql = new React\MySQL\MysqlClient('user:pass@localhost/bookstore');
5048

51-
$connection->query('SELECT * FROM book')->then(
52-
function (QueryResult $command) {
49+
$mysql->query('SELECT * FROM book')->then(
50+
function (React\MySQL\QueryResult $command) {
5351
print_r($command->resultFields);
5452
print_r($command->resultRows);
5553
echo count($command->resultRows) . ' row(s) in set' . PHP_EOL;
@@ -64,137 +62,13 @@ See also the [examples](examples).
6462

6563
## Usage
6664

67-
### Factory
65+
### MysqlClient
6866

69-
The `Factory` is responsible for creating your [`ConnectionInterface`](#connectioninterface) instance.
67+
The `MysqlClient` is responsible for exchanging messages with your MySQL server
68+
and keeps track of pending queries.
7069

7170
```php
72-
$factory = new React\MySQL\Factory();
73-
```
74-
75-
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
76-
pass the event loop instance to use for this object. You can use a `null` value
77-
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
78-
This value SHOULD NOT be given unless you're sure you want to explicitly use a
79-
given event loop instance.
80-
81-
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
82-
proxy servers etc.), you can explicitly pass a custom instance of the
83-
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
84-
85-
```php
86-
$connector = new React\Socket\Connector([
87-
'dns' => '127.0.0.1',
88-
'tcp' => [
89-
'bindto' => '192.168.10.1:0'
90-
],
91-
'tls' => [
92-
'verify_peer' => false,
93-
'verify_peer_name' => false
94-
)
95-
]);
96-
97-
$factory = new React\MySQL\Factory(null, $connector);
98-
```
99-
100-
#### createConnection()
101-
102-
The `createConnection(string $url): PromiseInterface<ConnectionInterface>` method can be used to
103-
create a new [`ConnectionInterface`](#connectioninterface).
104-
105-
It helps with establishing a TCP/IP connection to your MySQL database
106-
and issuing the initial authentication handshake.
107-
108-
```php
109-
$factory->createConnection($url)->then(
110-
function (ConnectionInterface $connection) {
111-
// client connection established (and authenticated)
112-
},
113-
function (Exception $e) {
114-
// an error occurred while trying to connect or authorize client
115-
}
116-
);
117-
```
118-
119-
The method returns a [Promise](https://github.com/reactphp/promise) that
120-
will resolve with a [`ConnectionInterface`](#connectioninterface)
121-
instance on success or will reject with an `Exception` if the URL is
122-
invalid or the connection or authentication fails.
123-
124-
The returned Promise is implemented in such a way that it can be
125-
cancelled when it is still pending. Cancelling a pending promise will
126-
reject its value with an Exception and will cancel the underlying TCP/IP
127-
connection attempt and/or MySQL authentication.
128-
129-
```php
130-
$promise = $factory->createConnection($url);
131-
132-
Loop::addTimer(3.0, function () use ($promise) {
133-
$promise->cancel();
134-
});
135-
```
136-
137-
The `$url` parameter must contain the database host, optional
138-
authentication, port and database to connect to:
139-
140-
```php
141-
$factory->createConnection('user:secret@localhost:3306/database');
142-
```
143-
144-
Note that both the username and password must be URL-encoded (percent-encoded)
145-
if they contain special characters:
146-
147-
```php
148-
$user = 'he:llo';
149-
$pass = 'p@ss';
150-
151-
$promise = $factory->createConnection(
152-
rawurlencode($user) . ':' . rawurlencode($pass) . '@localhost:3306/db'
153-
);
154-
```
155-
156-
You can omit the port if you're connecting to default port `3306`:
157-
158-
```php
159-
$factory->createConnection('user:secret@localhost/database');
160-
```
161-
162-
If you do not include authentication and/or database, then this method
163-
will default to trying to connect as user `root` with an empty password
164-
and no database selected. This may be useful when initially setting up a
165-
database, but likely to yield an authentication error in a production system:
166-
167-
```php
168-
$factory->createConnection('localhost');
169-
```
170-
171-
This method respects PHP's `default_socket_timeout` setting (default 60s)
172-
as a timeout for establishing the connection and waiting for successful
173-
authentication. You can explicitly pass a custom timeout value in seconds
174-
(or use a negative number to not apply a timeout) like this:
175-
176-
```php
177-
$factory->createConnection('localhost?timeout=0.5');
178-
```
179-
180-
By default, the connection provides full UTF-8 support (using the
181-
`utf8mb4` charset encoding). This should usually not be changed for most
182-
applications nowadays, but for legacy reasons you can change this to use
183-
a different ASCII-compatible charset encoding like this:
184-
185-
```php
186-
$factory->createConnection('localhost?charset=utf8mb4');
187-
```
188-
189-
#### createLazyConnection()
190-
191-
Creates a new connection.
192-
193-
It helps with establishing a TCP/IP connection to your MySQL database
194-
and issuing the initial authentication handshake.
195-
196-
```php
197-
$connection = $factory->createLazyConnection($url);
71+
$connection = new React\MySQL\MysqlClient($uri);
19872

19973
$connection->query(…);
20074
```
@@ -215,9 +89,6 @@ database right away while the underlying connection may still be
21589
outstanding. Because creating this underlying connection may take some
21690
time, it will enqueue all outstanding commands and will ensure that all
21791
commands will be executed in correct order once the connection is ready.
218-
In other words, this "virtual" connection behaves just like a "real"
219-
connection as described in the `ConnectionInterface` and frees you from
220-
having to deal with its async resolution.
22192

22293
If the underlying database connection fails, it will reject all
22394
outstanding commands and will return to the initial "idle" state. This
@@ -234,15 +105,16 @@ and no further commands can be enqueued. Similarly, calling `quit()` on
234105
this instance when not currently connected will succeed immediately and
235106
will not have to wait for an actual underlying connection.
236107

237-
Depending on your particular use case, you may prefer this method or the
238-
underlying `createConnection()` which resolves with a promise. For many
239-
simple use cases it may be easier to create a lazy connection.
108+
#### __construct()
109+
110+
The `new MysqlClient(string $uri, ConnectorInterface $connector = null, LoopInterface $loop = null)` constructor can be used to
111+
create a new `MysqlClient` instance.
240112

241-
The `$url` parameter must contain the database host, optional
113+
The `$uri` parameter must contain the database host, optional
242114
authentication, port and database to connect to:
243115

244116
```php
245-
$factory->createLazyConnection('user:secret@localhost:3306/database');
117+
$mysql = new React\MySQL\MysqlClient('user:secret@localhost:3306/database');
246118
```
247119

248120
Note that both the username and password must be URL-encoded (percent-encoded)
@@ -252,15 +124,15 @@ if they contain special characters:
252124
$user = 'he:llo';
253125
$pass = 'p@ss';
254126

255-
$connection = $factory->createLazyConnection(
127+
$mysql = new React\MySQL\MysqlClient(
256128
rawurlencode($user) . ':' . rawurlencode($pass) . '@localhost:3306/db'
257129
);
258130
```
259131

260132
You can omit the port if you're connecting to default port `3306`:
261133

262134
```php
263-
$factory->createLazyConnection('user:secret@localhost/database');
135+
$mysql = new React\MySQL\MysqlClient('user:secret@localhost/database');
264136
```
265137

266138
If you do not include authentication and/or database, then this method
@@ -269,7 +141,7 @@ and no database selected. This may be useful when initially setting up a
269141
database, but likely to yield an authentication error in a production system:
270142

271143
```php
272-
$factory->createLazyConnection('localhost');
144+
$mysql = new React\MySQL\MysqlClient('localhost');
273145
```
274146

275147
This method respects PHP's `default_socket_timeout` setting (default 60s)
@@ -278,7 +150,7 @@ successful authentication. You can explicitly pass a custom timeout value
278150
in seconds (or use a negative number to not apply a timeout) like this:
279151

280152
```php
281-
$factory->createLazyConnection('localhost?timeout=0.5');
153+
$mysql = new React\MySQL\MysqlClient('localhost?timeout=0.5');
282154
```
283155

284156
By default, idle connections will be held open for 1ms (0.001s) when not
@@ -291,7 +163,7 @@ pass a custom idle timeout value in seconds (or use a negative number to
291163
not apply a timeout) like this:
292164

293165
```php
294-
$factory->createLazyConnection('localhost?idle=10.0');
166+
$mysql = new React\MySQL\MysqlClient('localhost?idle=10.0');
295167
```
296168

297169
By default, the connection provides full UTF-8 support (using the
@@ -300,9 +172,34 @@ applications nowadays, but for legacy reasons you can change this to use
300172
a different ASCII-compatible charset encoding like this:
301173

302174
```php
303-
$factory->createLazyConnection('localhost?charset=utf8mb4');
175+
$mysql = new React\MySQL\MysqlClient('localhost?charset=utf8mb4');
176+
```
177+
178+
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
179+
proxy servers etc.), you can explicitly pass a custom instance of the
180+
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
181+
182+
```php
183+
$connector = new React\Socket\Connector([
184+
'dns' => '127.0.0.1',
185+
'tcp' => [
186+
'bindto' => '192.168.10.1:0'
187+
],
188+
'tls' => [
189+
'verify_peer' => false,
190+
'verify_peer_name' => false
191+
)
192+
]);
193+
194+
$mysql = new React\MySQL\MysqlClient('user:secret@localhost:3306/database', $connector);
304195
```
305196

197+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
198+
pass the event loop instance to use for this object. You can use a `null` value
199+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
200+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
201+
given event loop instance.
202+
306203
### ConnectionInterface
307204

308205
The `ConnectionInterface` represents a connection that is responsible for

examples/01-query.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
// $ php examples/01-query.php
44
// $ MYSQL_URI=test:test@localhost/test php examples/01-query.php "SELECT * FROM book"
55

6-
use React\MySQL\Factory;
7-
use React\MySQL\QueryResult;
8-
96
require __DIR__ . '/../vendor/autoload.php';
107

11-
$factory = new Factory();
12-
$connection = $factory->createLazyConnection(getenv('MYSQL_URI') ?: 'test:test@localhost/test');
8+
$mysql = new React\MySQL\MysqlClient(getenv('MYSQL_URI') ?: 'test:test@localhost/test');
139

1410
$query = isset($argv[1]) ? $argv[1] : 'select * from book';
15-
$connection->query($query)->then(function (QueryResult $command) {
11+
$mysql->query($query)->then(function (React\MySQL\QueryResult $command) {
1612
if (isset($command->resultRows)) {
1713
// this is a response to a SELECT etc. with some rows (0+)
1814
print_r($command->resultFields);

examples/02-query-stream.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
// $ php examples/02-query-stream.php "SHOW VARIABLES"
44
// $ MYSQL_URI=test:test@localhost/test php examples/02-query-stream.php "SELECT * FROM book"
55

6-
use React\MySQL\Factory;
7-
86
require __DIR__ . '/../vendor/autoload.php';
97

10-
$factory = new Factory();
11-
$connection = $factory->createLazyConnection(getenv('MYSQL_URI') ?: 'test:test@localhost/test');
8+
$mysql = new React\MySQL\MysqlClient(getenv('MYSQL_URI') ?: 'test:test@localhost/test');
129

1310
$query = isset($argv[1]) ? $argv[1] : 'select * from book';
14-
$stream = $connection->queryStream($query);
11+
$stream = $mysql->queryStream($query);
1512

1613
$stream->on('data', function ($row) {
1714
echo json_encode($row, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;

0 commit comments

Comments
 (0)