1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AsyncPHP\Icicle\Database\Connector; |
4
|
|
|
|
5
|
|
|
use AsyncPHP\Icicle\Database\Connector; |
6
|
|
|
use Icicle\Loop; |
7
|
|
|
use Icicle\Promise\Deferred; |
8
|
|
|
use Icicle\Promise\PromiseInterface; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use MySQLi; |
11
|
|
|
|
12
|
|
|
final class MySQLConnector implements Connector |
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
|
|
|
* @throws InvalidArgumentException |
35
|
|
|
*/ |
36
|
1 |
|
public function connect(array $config) |
37
|
|
|
{ |
38
|
1 |
|
if (!isset($config["host"])) { |
39
|
1 |
|
$config["host"] = "127.0.0.1"; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
1 |
|
if (!isset($config["username"])) { |
43
|
|
|
throw new InvalidArgumentException("Undefined username"); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
if (!isset($config["password"])) { |
47
|
|
|
throw new InvalidArgumentException("Undefined password"); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
if (!isset($config["database"])) { |
51
|
|
|
throw new InvalidArgumentException("Undefined database"); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
if (!isset($config["port"])) { |
55
|
1 |
|
$config["port"] = 3306; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
1 |
|
if (!isset($config["socket"])) { |
59
|
1 |
|
$config["socket"] = null; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
1 |
|
$this->connection = mysqli_connect( |
63
|
1 |
|
$config["host"], |
64
|
1 |
|
$config["username"], |
65
|
1 |
|
$config["password"], |
66
|
1 |
|
$config["database"], |
67
|
1 |
|
$config["port"], |
68
|
1 |
|
$config["socket"] |
69
|
1 |
|
); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
* |
75
|
|
|
* @param string $query |
76
|
|
|
* |
77
|
|
|
* @return PromiseInterface |
78
|
|
|
*/ |
79
|
1 |
|
public function query($query) |
80
|
|
|
{ |
81
|
1 |
|
$deferred = new Deferred(); |
82
|
|
|
|
83
|
|
|
$links = $errors = $rejects = [ |
84
|
1 |
|
$this->connection, |
85
|
1 |
|
]; |
86
|
|
|
|
87
|
|
|
$outer = Loop\periodic($this->poll, function () use (&$outer, $links, $errors, $rejects, $query, $deferred) { |
|
|
|
|
88
|
1 |
|
if ($this->ready) { |
89
|
1 |
|
$this->ready = false; |
90
|
1 |
|
$outer->stop(); |
91
|
|
|
|
92
|
1 |
|
$result = $this->connection->query($query, MYSQLI_ASYNC); |
93
|
|
|
|
94
|
1 |
View Code Duplication |
if ($result === false) { |
|
|
|
|
95
|
|
|
$this->ready = true; |
96
|
|
|
return $deferred->reject($this->connection->error); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
$inner = Loop\periodic($this->poll, function () use (&$inner, $links, $errors, $rejects, $deferred) { |
|
|
|
|
100
|
1 |
|
if (mysqli_poll($links, $errors, $rejects, $this->poll)) { |
101
|
1 |
|
$inner->stop(); |
102
|
|
|
|
103
|
1 |
|
$result = $this->connection->reap_async_query(); |
104
|
|
|
|
105
|
1 |
View Code Duplication |
if ($result === false) { |
|
|
|
|
106
|
|
|
$this->ready = true; |
107
|
|
|
return $deferred->reject($this->connection->error); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
if ($result === true) { |
111
|
1 |
|
$this->ready = true; |
112
|
1 |
|
return $deferred->resolve(); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
$rows = $result->fetch_all(MYSQLI_ASSOC); |
116
|
1 |
|
$result->free(); |
117
|
|
|
|
118
|
1 |
|
$this->ready = true; |
119
|
1 |
|
return $deferred->resolve($rows); |
120
|
|
|
} |
121
|
1 |
|
}); |
122
|
1 |
|
} |
123
|
1 |
|
}); |
124
|
|
|
|
125
|
1 |
|
return $deferred->getPromise(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritdoc |
130
|
|
|
* |
131
|
|
|
* @param mixed $value |
132
|
|
|
* |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
1 |
|
public function escape($value) |
136
|
|
|
{ |
137
|
1 |
|
return mysqli_real_escape_string($this->connection, $value); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.