Skip to content

Commit 701c22c

Browse files
authored
Merge pull request #78 from clue-labs/events
Define events on ConnectionInterface
2 parents d006640 + ce66bd0 commit 701c22c

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ It is written in pure PHP and does not require any extensions.
2121
* [ping()](#ping)
2222
* [quit()](#quit)
2323
* [close()](#close)
24+
* [Events](#events)
2425
* [Install](#install)
2526
* [Tests](#tests)
2627
* [License](#license)
@@ -301,6 +302,39 @@ Forcefully closing the connection will yield a warning in the server logs
301302
and should generally only be used as a last resort. See also
302303
[`quit()`](#quit) as a safe alternative.
303304

305+
#### Events
306+
307+
Besides defining a few methods, this interface also implements the
308+
`EventEmitterInterface` which allows you to react to certain events:
309+
310+
##### error event
311+
312+
The `error` event will be emitted once a fatal error occurs, such as
313+
when the connection is lost or is invalid.
314+
The event receives a single `Exception` argument for the error instance.
315+
316+
```php
317+
$connection->on('error', function (Exception $e) {
318+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
319+
});
320+
```
321+
322+
This event will only be triggered for fatal errors and will be followed
323+
by closing the connection. It is not to be confused with "soft" errors
324+
caused by invalid SQL queries.
325+
326+
##### close event
327+
328+
The `close` event will be emitted once the connection closes (terminates).
329+
330+
```php
331+
$connecion->on('close', function () {
332+
echo 'Connection closed' . PHP_EOL;
333+
});
334+
```
335+
336+
See also the [#close](#close) method.
337+
304338
## Install
305339

306340
The recommended way to install this library is [through Composer](https://getcomposer.org).

src/ConnectionInterface.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,45 @@
22

33
namespace React\MySQL;
44

5+
use Evenement\EventEmitterInterface;
56
use React\Promise\PromiseInterface;
67
use React\Stream\ReadableStreamInterface;
78

89
/**
910
* The `ConnectionInterface` represents a connection that is responsible for
1011
* communicating with your MySQL server instance, managing the connection state
1112
* and sending your database queries.
13+
*
14+
* Besides defining a few methods, this interface also implements the
15+
* `EventEmitterInterface` which allows you to react to certain events:
16+
*
17+
* error event:
18+
* The `error` event will be emitted once a fatal error occurs, such as
19+
* when the connection is lost or is invalid.
20+
* The event receives a single `Exception` argument for the error instance.
21+
*
22+
* ```php
23+
* $connection->on('error', function (Exception $e) {
24+
* echo 'Error: ' . $e->getMessage() . PHP_EOL;
25+
* });
26+
* ```
27+
*
28+
* This event will only be triggered for fatal errors and will be followed
29+
* by closing the connection. It is not to be confused with "soft" errors
30+
* caused by invalid SQL queries.
31+
*
32+
* close event:
33+
* The `close` event will be emitted once the connection closes (terminates).
34+
*
35+
* ```php
36+
* $connecion->on('close', function () {
37+
* echo 'Connection closed' . PHP_EOL;
38+
* });
39+
* ```
40+
*
41+
* See also the [#close](#close) method.
1242
*/
13-
interface ConnectionInterface
43+
interface ConnectionInterface extends EventEmitterInterface
1444
{
1545
/**
1646
* Performs an async query.

0 commit comments

Comments
 (0)