1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AsyncPHP\Icicle\Database\Connection; |
4
|
|
|
|
5
|
|
|
use AsyncPHP\Icicle\Database\Connection; |
6
|
|
|
use Icicle\Loop; |
7
|
|
|
use Icicle\Promise\Deferred; |
8
|
|
|
use Icicle\Promise\PromiseInterface; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use MySQLi; |
11
|
|
|
|
12
|
|
|
final class MySQLConnection implements Connection |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var null|MySQLi |
16
|
|
|
*/ |
17
|
|
|
private $connection = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
private $ready = true; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var float |
26
|
|
|
*/ |
27
|
|
|
private $poll = 0.000001; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
* |
32
|
|
|
* @param array $config |
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
* |
36
|
|
|
* @throws InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
public function connect(array $config) |
39
|
|
|
{ |
40
|
|
|
if (!isset($config["host"])) { |
41
|
|
|
$options["host"] = "127.0.0.1"; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!isset($config["username"])) { |
45
|
|
|
throw new InvalidArgumentException("Undefined connection username"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!isset($config["password"])) { |
49
|
|
|
throw new InvalidArgumentException("Undefined connection password"); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!isset($config["database"])) { |
53
|
|
|
throw new InvalidArgumentException("Undefined connection database"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!isset($config["port"])) { |
57
|
|
|
$options["port"] = 3306; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!isset($config["socket"])) { |
61
|
|
|
$options["socket"] = null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->connection = mysqli_connect( |
65
|
|
|
$config["host"], |
66
|
|
|
$config["username"], |
67
|
|
|
$config["password"], |
68
|
|
|
$config["database"], |
69
|
|
|
$config["port"], |
70
|
|
|
$config["socket"] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
* |
77
|
|
|
* @param string $query |
78
|
|
|
* |
79
|
|
|
* @return PromiseInterface |
80
|
|
|
*/ |
81
|
|
|
public function query($query) |
82
|
|
|
{ |
83
|
|
|
$deferred = new Deferred(); |
84
|
|
|
|
85
|
|
|
$links = $errors = $rejects = [ |
86
|
|
|
$this->connection, |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
$outer = Loop\periodic($this->poll, function () use (&$outer, $links, $errors, $rejects, $query, $deferred) { |
|
|
|
|
90
|
|
|
if ($this->ready) { |
91
|
|
|
$this->ready = false; |
92
|
|
|
$outer->stop(); |
93
|
|
|
|
94
|
|
|
$result = $this->connection->query($query, MYSQLI_ASYNC); |
95
|
|
|
|
96
|
|
View Code Duplication |
if ($result === false) { |
|
|
|
|
97
|
|
|
$this->ready = true; |
98
|
|
|
return $deferred->reject($this->connection->error); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$inner = Loop\periodic($this->poll, function () use (&$inner, $links, $errors, $rejects, $deferred) { |
|
|
|
|
102
|
|
|
if (mysqli_poll($links, $errors, $rejects, $this->poll)) { |
103
|
|
|
$inner->stop(); |
104
|
|
|
|
105
|
|
|
$result = $this->connection->reap_async_query(); |
106
|
|
|
|
107
|
|
View Code Duplication |
if ($result === false) { |
|
|
|
|
108
|
|
|
$this->ready = true; |
109
|
|
|
return $deferred->reject($this->connection->error); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($result === true) { |
113
|
|
|
$this->ready = true; |
114
|
|
|
return $deferred->resolve(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$rows = $result->fetch_all(); |
118
|
|
|
$result->free(); |
119
|
|
|
|
120
|
|
|
$this->ready = true; |
121
|
|
|
return $deferred->resolve($rows); |
122
|
|
|
} |
123
|
|
|
}); |
124
|
|
|
} |
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
return $deferred->getPromise(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.