1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AsyncPHP\Icicle\Database\Connector; |
4
|
|
|
|
5
|
|
|
use AsyncPHP\Doorman\Manager; |
6
|
|
|
use AsyncPHP\Doorman\Manager\ProcessManager; |
7
|
|
|
use AsyncPHP\Doorman\Task\ProcessCallbackTask; |
8
|
|
|
use AsyncPHP\Icicle\Database\Connector; |
9
|
|
|
use AsyncPHP\Remit\Client; |
10
|
|
|
use AsyncPHP\Remit\Client\ZeroMqClient; |
11
|
|
|
use AsyncPHP\Remit\Location\InMemoryLocation; |
12
|
|
|
use AsyncPHP\Remit\Server; |
13
|
|
|
use AsyncPHP\Remit\Server\ZeroMqServer; |
14
|
|
|
use Aura\Sql\ExtendedPdo; |
15
|
|
|
use Icicle\Loop; |
16
|
|
|
use Icicle\Promise\Deferred; |
17
|
|
|
use Icicle\Promise\PromiseInterface; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use PDO; |
20
|
|
|
|
21
|
|
|
final class DoormanConnector implements Connector |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private $id = 1; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $deferred = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Manager |
35
|
|
|
*/ |
36
|
|
|
private $manager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Server |
40
|
|
|
*/ |
41
|
|
|
private $server; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Client |
45
|
|
|
*/ |
46
|
|
|
private $client; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
* |
51
|
|
|
* @param array $config |
52
|
|
|
* |
53
|
|
|
* @return PromiseInterface |
54
|
|
|
* |
55
|
|
|
* @throws InvalidArgumentException |
56
|
|
|
*/ |
57
|
1 |
|
public function connect(array $config) |
58
|
|
|
{ |
59
|
1 |
|
$this->manager = new ProcessManager(); |
60
|
|
|
|
61
|
1 |
|
$this->validate($config); |
62
|
|
|
$this->connectRemit($config); |
63
|
|
|
$this->connectDoorman($config); |
64
|
|
|
|
65
|
|
|
$this->server->addListener("r", function ($result, $id) { |
66
|
|
|
if (isset($this->deferred[$id])) { |
67
|
|
|
$this->deferred[$id]->resolve($result); |
68
|
|
|
unset($this->deferred[$id]); |
69
|
|
|
} |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
$this->server->addListener("e", function ($error, $id) { |
73
|
|
|
if (isset($this->deferred[$id])) { |
74
|
|
|
$this->deferred[$id]->reject($error); |
75
|
|
|
unset($this->deferred[$id]); |
76
|
|
|
} |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
Loop\periodic(0, function () { |
80
|
|
|
$this->server->tick(); |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
$this->manager->tick(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $config |
88
|
|
|
* |
89
|
|
|
* @throws InvalidArgumentException |
90
|
|
|
*/ |
91
|
1 |
|
private function validate(array $config) |
92
|
|
|
{ |
93
|
1 |
|
if (!isset($config["remit"])) { |
94
|
1 |
|
throw new InvalidArgumentException("Undefined remit"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (!isset($config["remit"]["driver"])) { |
98
|
|
|
throw new InvalidArgumentException("Undefined remit driver"); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!isset($config["remit"]["server"])) { |
102
|
|
|
throw new InvalidArgumentException("Undefined remit server"); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!isset($config["remit"]["client"])) { |
106
|
|
|
throw new InvalidArgumentException("Undefined remit client"); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($config["remit"]["driver"] === "zeromq") { |
110
|
|
|
if (!isset($config["remit"]["server"]["port"])) { |
111
|
|
|
throw new InvalidArgumentException("Undefined remit server port"); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!isset($config["remit"]["client"]["port"])) { |
115
|
|
|
throw new InvalidArgumentException("Undefined remit client port"); |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
throw new InvalidArgumentException("Unrecognised remit driver"); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $config |
124
|
|
|
*/ |
125
|
|
|
private function connectRemit(array $config) |
126
|
|
|
{ |
127
|
|
|
$server = $config["remit"]["server"]; |
128
|
|
|
$client = $config["remit"]["client"]; |
129
|
|
|
|
130
|
|
|
if ($config["remit"]["driver"] === "zeromq") { |
131
|
|
|
$server = array_merge([ |
132
|
|
|
"host" => "127.0.0.1", |
133
|
|
|
], $server); |
134
|
|
|
|
135
|
|
|
$this->server = new ZeroMqServer( |
136
|
|
|
new InMemoryLocation( |
137
|
|
|
$server["host"], |
138
|
|
|
$server["port"] |
139
|
|
|
) |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
$client = array_merge([ |
143
|
|
|
"host" => "127.0.0.1", |
144
|
|
|
], $client); |
145
|
|
|
|
146
|
|
|
$this->client = new ZeroMqClient( |
147
|
|
|
new InMemoryLocation( |
148
|
|
|
$client["host"], |
149
|
|
|
$client["port"] |
150
|
|
|
) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $config |
157
|
|
|
*/ |
158
|
|
|
private function connectDoorman(array $config) |
159
|
|
|
{ |
160
|
|
|
$server = $config["remit"]["server"]; |
161
|
|
|
$client = $config["remit"]["client"]; |
162
|
|
|
|
163
|
|
|
if ($config["remit"]["driver"] === "zeromq") { |
164
|
|
|
$task = new ProcessCallbackTask(function () use ($config, $server, $client) { |
165
|
|
|
$config = array_merge([ |
|
|
|
|
166
|
|
|
"host" => "127.0.0.1", |
167
|
|
|
"port" => 3306, |
168
|
|
|
], $config); |
169
|
|
|
|
170
|
|
|
$server = array_merge([ |
|
|
|
|
171
|
|
|
"host" => "127.0.0.1", |
172
|
|
|
], $server); |
173
|
|
|
|
174
|
|
|
$client = array_merge([ |
|
|
|
|
175
|
|
|
"host" => "127.0.0.1", |
176
|
|
|
], $client); |
177
|
|
|
|
178
|
|
|
$remitServer = new ZeroMqServer( |
179
|
|
|
new InMemoryLocation( |
180
|
|
|
$client["host"], |
181
|
|
|
$client["port"] |
182
|
|
|
) |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$remitClient = new ZeroMqClient( |
186
|
|
|
new InMemoryLocation( |
187
|
|
|
$server["host"], |
188
|
|
|
$server["port"] |
189
|
|
|
) |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
$database = $config["database"]; |
193
|
|
|
$host = $config["host"]; |
194
|
|
|
$port = $config["port"]; |
195
|
|
|
$username = $config["username"]; |
196
|
|
|
$password = $config["password"]; |
197
|
|
|
|
198
|
|
|
$connection = new ExtendedPdo( |
199
|
|
|
new PDO( |
200
|
|
|
"mysql:dbname={$database};host={$host};port={$port}", |
201
|
|
|
$username, $password |
202
|
|
|
) |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
$remitServer->addListener("q", function ($query, $values, $id) use ($remitClient, $connection) { |
206
|
|
|
$remitClient->emit("r", [$connection->fetchAll($query, $values), $id]); |
207
|
|
|
}); |
208
|
|
|
|
209
|
|
|
Loop\periodic(0, function () use ($remitServer) { |
210
|
|
|
$remitServer->tick(); |
211
|
|
|
}); |
212
|
|
|
|
213
|
|
|
Loop\run(); |
214
|
|
|
}); |
215
|
|
|
|
216
|
|
|
$this->manager->addTask($task); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @inheritdoc |
222
|
|
|
* |
223
|
|
|
* @param string $query |
224
|
|
|
* @param array $values |
225
|
|
|
* |
226
|
|
|
* @return PromiseInterface |
227
|
|
|
* |
228
|
|
|
* @throws InvalidArgumentException |
229
|
|
|
*/ |
230
|
|
|
public function query($query, $values) |
231
|
|
|
{ |
232
|
|
|
$id = $this->id++; |
233
|
|
|
|
234
|
|
|
$deferred = new Deferred(); |
235
|
|
|
|
236
|
|
|
$this->client->emit("q", [$query, $values, "d{$id}"]); |
237
|
|
|
|
238
|
|
|
$this->deferred["d{$id}"] = $deferred; |
239
|
|
|
|
240
|
|
|
return $deferred->getPromise(); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope