Total Complexity | 95 |
Total Lines | 764 |
Duplicated Lines | 0 % |
Coverage | 68.18% |
Changes | 0 |
Complex classes like Driver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Driver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Driver implements \Plasma\DriverInterface { |
||
17 | use \Evenement\EventEmitterTrait; |
||
18 | |||
19 | /** |
||
20 | * @var \React\EventLoop\LoopInterface |
||
21 | */ |
||
22 | protected $loop; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $options = array( |
||
28 | 'tls.context' => array(), |
||
29 | 'tls.force' => true, |
||
30 | 'tls.forceLocal' => false |
||
31 | ); |
||
32 | |||
33 | /** |
||
34 | * @var \React\Socket\ConnectorInterface |
||
35 | */ |
||
36 | protected $connector; |
||
37 | |||
38 | /** |
||
39 | * Internal class is intentional used, as there's no other way currently. |
||
40 | * @var \React\Socket\StreamEncryption |
||
41 | * @see https://github.com/reactphp/socket/issues/180 |
||
42 | */ |
||
43 | protected $encryption; |
||
44 | |||
45 | /** |
||
46 | * @var \React\Socket\Connection |
||
47 | */ |
||
48 | protected $connection; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $connectionState = \Plasma\DriverInterface::CONNECTION_CLOSED; |
||
54 | |||
55 | /** |
||
56 | * @var \Plasma\Drivers\MySQL\ProtocolParser |
||
57 | */ |
||
58 | protected $parser; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $queue; |
||
64 | |||
65 | /** |
||
66 | * @var int |
||
67 | */ |
||
68 | protected $busy = \Plasma\DriverInterface::STATE_IDLE; |
||
69 | |||
70 | /** |
||
71 | * @var bool |
||
72 | */ |
||
73 | protected $transaction = false; |
||
74 | |||
75 | /** |
||
76 | * @var \React\Promise\Deferred |
||
77 | */ |
||
78 | protected $goingAway; |
||
79 | |||
80 | /** |
||
81 | * Constructor. |
||
82 | */ |
||
83 | 17 | function __construct(\React\EventLoop\LoopInterface $loop, array $options) { |
|
84 | 17 | $this->validateOptions($options); |
|
85 | |||
86 | 17 | $this->loop = $loop; |
|
87 | 17 | $this->options = \array_merge($this->options, $options); |
|
88 | |||
89 | 17 | $this->connector = ($options['connector'] ?? (new \React\Socket\Connector($loop))); |
|
90 | 17 | $this->encryption = new \React\Socket\StreamEncryption($this->loop, false); |
|
91 | 17 | $this->queue = array(); |
|
92 | 17 | } |
|
93 | |||
94 | /** |
||
95 | * Returns the event loop. |
||
96 | * @var \React\EventLoop\LoopInterface |
||
97 | */ |
||
98 | 7 | function getLoop(): \React\EventLoop\LoopInterface { |
|
99 | 7 | return $this->loop; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * Retrieves the current connection state. |
||
104 | * @return int |
||
105 | */ |
||
106 | 5 | function getConnectionState(): int { |
|
107 | 5 | return $this->connectionState; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Retrieves the current busy state. |
||
112 | * @return int |
||
113 | */ |
||
114 | 1 | function getBusyState(): int { |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Get the length of the driver backlog queue. |
||
120 | * @return int |
||
121 | */ |
||
122 | 1 | function getBacklogLength(): int { |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Connects to the given URI. |
||
128 | * @return \React\Promise\PromiseInterface |
||
129 | */ |
||
130 | 12 | function connect(string $uri): \React\Promise\PromiseInterface { |
|
131 | 12 | if($this->goingAway || $this->connectionState === \Plasma\DriverInterface::CONNECTION_UNUSABLE) { |
|
132 | return \React\Promise\resolve((new \Plasma\Exception('Connection is going away or unusable'))); |
||
133 | } |
||
134 | |||
135 | 12 | $uri = 'mysql://'.\ltrim($uri, 'mysql://'); |
|
136 | |||
137 | 12 | $parts = \parse_url($uri); |
|
138 | 12 | if(!isset($parts['host'])) { |
|
139 | return \React\Promise\reject((new \InvalidArgumentException('Invalid connect uri given'))); |
||
140 | } |
||
141 | |||
142 | 12 | $host = $parts['host'].':'.($parts['port'] ?? 3306); |
|
143 | 12 | $this->connectionState = static::CONNECTION_STARTED; |
|
144 | 12 | $resolved = false; |
|
145 | |||
146 | 12 | if(!empty($parts['query'])) { |
|
147 | 1 | \parse_str($parts['query'], $query); |
|
148 | 1 | $charset = $query['charset'] ?? null; |
|
149 | 1 | $collate = $query['collate'] ?? null; |
|
150 | |||
151 | 1 | unset($query); |
|
152 | } else { |
||
153 | 11 | $charset = null; |
|
154 | 11 | $collate = null; |
|
155 | } |
||
156 | |||
157 | $connect = $this->connector->connect($host)->then(function (\React\Socket\ConnectionInterface $connection) use ($parts, &$resolved) { |
||
158 | // See description of property encryption |
||
159 | 12 | if(!($connection instanceof \React\Socket\Connection)) { |
|
160 | throw new \LogicException('Custom connection class is NOT supported yet (encryption limitation)'); |
||
161 | } |
||
162 | |||
163 | 12 | $this->busy = static::STATE_BUSY; |
|
164 | 12 | $this->connectionState = static::CONNECTION_MADE; |
|
165 | 12 | $this->connection = $connection; |
|
166 | |||
167 | $this->connection->on('error', function (\Throwable $error) { |
||
168 | $this->emit('error', array($error)); |
||
169 | 12 | }); |
|
170 | |||
171 | $this->connection->on('close', function () { |
||
172 | 3 | $this->connection = null; |
|
173 | 3 | $this->connectionState = static::CONNECTION_UNUSABLE; |
|
174 | |||
175 | 3 | $this->emit('close'); |
|
176 | 12 | }); |
|
177 | |||
178 | 12 | $deferred = new \React\Promise\Deferred(); |
|
179 | 12 | $this->parser = new \Plasma\Drivers\MySQL\ProtocolParser($this, $this->connection); |
|
180 | |||
181 | $this->parser->on('error', function (\Throwable $error) use (&$deferred, &$resolved) { |
||
182 | if($resolved) { |
||
183 | $this->emit('error', array($error)); |
||
184 | } else { |
||
185 | $deferred->reject($error); |
||
186 | } |
||
187 | 12 | }); |
|
188 | |||
189 | 12 | $user = ($parts['user'] ?? 'root'); |
|
190 | 12 | $password = ($parts['pass'] ?? ''); |
|
191 | 12 | $db = (!empty($parts['path']) ? \ltrim($parts['path'], '/') : ''); |
|
192 | |||
193 | 12 | $credentials = \compact('user', 'password', 'db'); |
|
194 | |||
195 | 12 | $this->startHandshake($credentials, $deferred); |
|
196 | return $deferred->promise()->then(function () use (&$resolved) { |
||
197 | 11 | $this->busy = static::STATE_IDLE; |
|
198 | 11 | $resolved = true; |
|
199 | |||
200 | 11 | if(\count($this->queue) > 0) { |
|
201 | $this->parser->invokeCommand($this->getNextCommand()); |
||
202 | } |
||
203 | 12 | }); |
|
204 | 12 | }); |
|
205 | |||
206 | 12 | if($charset) { |
|
207 | $connect = $connect->then(function () use ($charset, $collate) { |
||
208 | 1 | $query = 'SET NAMES "'.$charset.'"'.($collate ? ' COLLATE "'.$collate.'"' : ''); |
|
209 | |||
210 | 1 | $cmd = new \Plasma\Drivers\MySQL\Commands\QueryCommand($this, $query); |
|
211 | 1 | $this->executeCommand($cmd); |
|
212 | 1 | }); |
|
213 | } |
||
214 | |||
215 | 12 | return $connect; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * Pauses the underlying stream I/O consumption. |
||
220 | * If consumption is already paused, this will do nothing. |
||
221 | * @return bool Whether the operation was successful. |
||
222 | */ |
||
223 | function pauseStreamConsumption(): bool { |
||
224 | if($this->goingAway) { |
||
225 | return false; |
||
226 | } |
||
227 | |||
228 | $this->connection->pause(); |
||
229 | return true; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Resumes the underlying stream I/O consumption. |
||
234 | * If consumption is not paused, this will do nothing. |
||
235 | * @return bool Whether the operation was successful. |
||
236 | */ |
||
237 | function resumeStreamConsumption(): bool { |
||
238 | if($this->goingAway) { |
||
239 | return false; |
||
240 | } |
||
241 | |||
242 | $this->connection->resume(); |
||
243 | return true; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Closes all connections gracefully after processing all outstanding requests. |
||
248 | * @return \React\Promise\PromiseInterface |
||
249 | */ |
||
250 | 1 | function close(): \React\Promise\PromiseInterface { |
|
251 | 1 | if($this->goingAway) { |
|
252 | return $this->goingAway->promise(); |
||
253 | } |
||
254 | |||
255 | 1 | $state = $this->connectionState; |
|
256 | 1 | $this->connectionState = \Plasma\DriverInterface::CONNECTION_UNUSABLE; |
|
257 | |||
258 | 1 | $this->goingAway = new \React\Promise\Deferred(); |
|
259 | |||
260 | 1 | if(\count($this->queue) === 0) { |
|
261 | $this->goingAway->resolve(); |
||
262 | } |
||
263 | |||
264 | return $this->goingAway->promise()->then(function () use ($state) { |
||
265 | 1 | if($state !== static::CONNECTION_OK) { |
|
266 | return; |
||
267 | } |
||
268 | |||
269 | 1 | $deferred = new \React\Promise\Deferred(); |
|
270 | |||
271 | 1 | $quit = new \Plasma\Drivers\MySQL\Commands\QuitCommand(); |
|
272 | |||
273 | $this->connection->once('close', function () use (&$deferred) { |
||
274 | 1 | $deferred->resolve(); |
|
275 | 1 | }); |
|
276 | |||
277 | $quit->once('end', function () { |
||
278 | $this->connection->close(); |
||
279 | 1 | }); |
|
280 | |||
281 | 1 | $this->parser->invokeCommand($quit); |
|
282 | |||
283 | 1 | return $deferred->promise(); |
|
284 | 1 | }); |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * Forcefully closes the connection, without waiting for any outstanding requests. This will reject all outstanding requests. |
||
289 | * @return void |
||
290 | */ |
||
291 | 1 | function quit(): void { |
|
292 | 1 | if($this->goingAway) { |
|
293 | return; |
||
294 | } |
||
295 | |||
296 | 1 | $state = $this->connectionState; |
|
297 | 1 | $this->connectionState = \Plasma\DriverInterface::CONNECTION_UNUSABLE; |
|
298 | |||
299 | 1 | $this->goingAway = new \React\Promise\Deferred(); |
|
300 | 1 | $this->goingAway->resolve(); |
|
301 | |||
302 | /** @var \Plasma\Drivers\MySQL\Commands\CommandInterface $command */ |
||
303 | 1 | while($command = \array_shift($this->queue)) { |
|
304 | 1 | $command->emit('error', array((new \Plasma\Exception('Connection is going away')))); |
|
305 | } |
||
306 | |||
307 | 1 | if($state === static::CONNECTION_OK) { |
|
308 | 1 | $quit = new \Plasma\Drivers\MySQL\Commands\QuitCommand(); |
|
309 | 1 | $this->parser->invokeCommand($quit); |
|
310 | |||
311 | 1 | $this->connection->close(); |
|
312 | } |
||
313 | 1 | } |
|
314 | |||
315 | /** |
||
316 | * Whether this driver is currently in a transaction. |
||
317 | * @return bool |
||
318 | */ |
||
319 | 1 | function isInTransaction(): bool { |
|
320 | 1 | return $this->transaction; |
|
321 | } |
||
322 | |||
323 | /** |
||
324 | * Executes a plain query. Resolves with a `QueryResultInterface` instance. |
||
325 | * When the command is done, the driver must check itself back into the client. |
||
326 | * @param \Plasma\ClientInterface $client |
||
327 | * @param string $query |
||
328 | * @return \React\Promise\PromiseInterface |
||
329 | * @throws \Plasma\Exception |
||
330 | * @see \Plasma\QueryResultInterface |
||
331 | */ |
||
332 | 4 | function query(\Plasma\ClientInterface $client, string $query): \React\Promise\PromiseInterface { |
|
333 | 4 | if($this->goingAway) { |
|
334 | return \React\Promise\reject((new \Plasma\Exception('Connection is going away'))); |
||
335 | } |
||
336 | |||
337 | 4 | $command = new \Plasma\Drivers\MySQL\Commands\QueryCommand($this, $query); |
|
338 | 4 | $this->executeCommand($command); |
|
339 | |||
340 | 4 | if(!$this->transaction) { |
|
341 | $command->once('end', function () use (&$client) { |
||
342 | 1 | $client->checkinConnection($this); |
|
343 | 3 | }); |
|
344 | } |
||
345 | |||
346 | 4 | return $command->getPromise(); |
|
347 | } |
||
348 | |||
349 | /** |
||
350 | * Prepares a query. Resolves with a `StatementInterface` instance. |
||
351 | * When the command is done, the driver must check itself back into the client. |
||
352 | * @param \Plasma\ClientInterface $client |
||
353 | * @param string $query |
||
354 | * @return \React\Promise\PromiseInterface |
||
355 | * @throws \Plasma\Exception |
||
356 | * @see \Plasma\StatementInterface |
||
357 | */ |
||
358 | 2 | function prepare(\Plasma\ClientInterface $client, string $query): \React\Promise\PromiseInterface { |
|
359 | 2 | if($this->goingAway) { |
|
360 | return \React\Promise\reject((new \Plasma\Exception('Connection is going away'))); |
||
361 | } |
||
362 | |||
363 | 2 | $command = new \Plasma\Drivers\MySQL\Commands\StatementPrepareCommand($client, $this, $query); |
|
364 | 2 | $this->executeCommand($command); |
|
365 | |||
366 | 2 | return $command->getPromise(); |
|
367 | } |
||
368 | |||
369 | /** |
||
370 | * Prepares and executes a query. Resolves with a `QueryResultInterface` instance. |
||
371 | * This is equivalent to prepare -> execute -> close. |
||
372 | * If you need to execute a query multiple times, prepare the query manually for performance reasons. |
||
373 | * @param \Plasma\ClientInterface $client |
||
374 | * @param string $query |
||
375 | * @param array $params |
||
376 | * @return \React\Promise\PromiseInterface |
||
377 | * @throws \Plasma\Exception |
||
378 | * @see \Plasma\StatementInterface |
||
379 | */ |
||
380 | 1 | function execute(\Plasma\ClientInterface $client, string $query, array $params = array()): \React\Promise\PromiseInterface { |
|
381 | 1 | if($this->goingAway) { |
|
382 | return \React\Promise\reject((new \Plasma\Exception('Connection is going away'))); |
||
383 | } |
||
384 | |||
385 | return $this->prepare($client, $query)->then(function (\Plasma\StatementInterface $statement) use ($params) { |
||
386 | return $statement->execute($params)->then(function (\Plasma\QueryResultInterface $result) use (&$statement) { |
||
387 | if($result instanceof \Plasma\StreamQueryResultInterface) { |
||
388 | $statement->close(null, function (\Throwable $error) { |
||
389 | $this->emit('error', array($error)); |
||
390 | }); |
||
391 | |||
392 | return $result; |
||
393 | } |
||
394 | |||
395 | return $statement->close()->then(function () use ($result) { |
||
396 | return $result; |
||
397 | }); |
||
398 | }, function (\Throwable $error) use (&$statement) { |
||
399 | return $statement->close()->then(function () use ($error) { |
||
400 | throw $error; |
||
401 | }); |
||
402 | }); |
||
403 | 1 | }); |
|
404 | } |
||
405 | |||
406 | /** |
||
407 | * Quotes the string for use in the query. |
||
408 | * @param string $str |
||
409 | * @return string |
||
410 | * @throws \LogicException Thrown if the driver does not support quoting. |
||
411 | * @throws \Plasma\Exception |
||
412 | */ |
||
413 | function quote(string $str): string { // TODO |
||
414 | throw new \LogicException('Not implemented yet'); |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Begins a transaction. Resolves with a `TransactionInterface` instance. |
||
419 | * |
||
420 | * Checks out a connection until the transaction gets committed or rolled back. |
||
421 | * It must be noted that the user is responsible for finishing the transaction. The client WILL NOT automatically |
||
422 | * check the connection back into the pool, as long as the transaction is not finished. |
||
423 | * |
||
424 | * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition language (DDL) |
||
425 | * statement such as DROP TABLE or CREATE TABLE is issued within a transaction. |
||
426 | * The implicit COMMIT will prevent you from rolling back any other changes within the transaction boundary. |
||
427 | * @param \Plasma\ClientInterface $client |
||
428 | * @param int $isolation See the `TransactionInterface` constants. |
||
429 | * @return \React\Promise\PromiseInterface |
||
430 | * @throws \Plasma\Exception |
||
431 | * @see \Plasma\TransactionInterface |
||
432 | */ |
||
433 | 1 | function beginTransaction(\Plasma\ClientInterface $client, int $isolation = \Plasma\TransactionInterface::ISOLATION_COMMITTED): \React\Promise\PromiseInterface { |
|
434 | 1 | if($this->goingAway) { |
|
435 | return \React\Promise\reject((new \Plasma\Exception('Connection is going away'))); |
||
436 | } |
||
437 | |||
438 | 1 | if($this->transaction) { |
|
439 | throw new \Plasma\Exception('Driver is already in transaction'); |
||
440 | } |
||
441 | |||
442 | switch ($isolation) { |
||
443 | 1 | case \Plasma\TransactionInterface::ISOLATION_UNCOMMITTED: |
|
1 ignored issue
–
show
|
|||
444 | $query = 'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED'; |
||
445 | break; |
||
446 | 1 | case \Plasma\TransactionInterface::ISOLATION_COMMITTED: |
|
447 | 1 | $query = 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED'; |
|
448 | 1 | break; |
|
449 | case \Plasma\TransactionInterface::ISOLATION_REPEATABLE: |
||
450 | $query = 'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ'; |
||
451 | break; |
||
452 | case \Plasma\TransactionInterface::ISOLATION_SERIALIZABLE: |
||
453 | $query = 'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE'; |
||
454 | break; |
||
455 | default: |
||
456 | throw new \Plasma\Exception('Invalid isolation level given'); |
||
457 | break; |
||
458 | } |
||
459 | |||
460 | 1 | $this->transaction = true; |
|
461 | |||
462 | return $this->query($client, $query)->then(function () use (&$client) { |
||
463 | 1 | return $this->query($client, 'START TRANSACTION'); |
|
464 | })->then(function () use (&$client, $isolation) { |
||
465 | 1 | return (new \Plasma\Transaction($client, $this, $isolation)); |
|
466 | })->otherwise(function (\Throwable $e) { |
||
467 | $this->transaction = false; |
||
468 | throw $e; |
||
469 | 1 | }); |
|
470 | } |
||
471 | |||
472 | /** |
||
473 | * Informationally closes a transaction. This method is used by `Transaction` to inform the driver of the end of the transaction. |
||
474 | * @return void |
||
475 | */ |
||
476 | 1 | function endTransaction(): void { |
|
477 | 1 | $this->transaction = false; |
|
478 | 1 | } |
|
479 | |||
480 | /** |
||
481 | * Runs the given command. |
||
482 | * Returns a Promise, which resolves with the `end` event argument (defaults to `null), |
||
483 | * or rejects with the `Throwable` of the `error` event. |
||
484 | * When the command is done, the driver must check itself back into the client. |
||
485 | * @param \Plasma\ClientInterface $client |
||
486 | * @param \Plasma\CommandInterface $command |
||
487 | * @return \React\Promise\PromiseInterface |
||
488 | */ |
||
489 | 4 | function runCommand(\Plasma\ClientInterface $client, \Plasma\CommandInterface $command) { |
|
490 | 4 | if($this->goingAway) { |
|
491 | return \React\Promise\reject((new \Plasma\Exception('Connection is going away'))); |
||
492 | } |
||
493 | |||
494 | return (new \React\Promise\Promise(function (callable $resolve, callable $reject) use (&$client, &$command) { |
||
495 | $command->once('end', function ($value = null) use (&$client, &$resolve) { |
||
496 | 2 | if(!$this->transaction) { |
|
497 | 2 | $client->checkinConnection($this); |
|
498 | } |
||
499 | |||
500 | 2 | $resolve($value); |
|
501 | 4 | }); |
|
502 | |||
503 | $command->once('error', function (\Throwable $error) use (&$client, &$reject) { |
||
504 | 2 | if(!$this->transaction) { |
|
505 | 2 | $client->checkinConnection($this); |
|
506 | } |
||
507 | |||
508 | 2 | $reject($error); |
|
509 | 4 | }); |
|
510 | |||
511 | 4 | $this->executeCommand($command); |
|
512 | 4 | })); |
|
513 | } |
||
514 | |||
515 | /** |
||
516 | * Executes a command. |
||
517 | * @param \Plasma\CommandInterface $command |
||
518 | * @return void |
||
519 | * @internal |
||
520 | */ |
||
521 | 10 | function executeCommand(\Plasma\CommandInterface $command): void { |
|
522 | 10 | $this->queue[] = $command; |
|
523 | 10 | \assert((\Plasma\Drivers\MySQL\Messages\MessageUtility::debug('Command '.get_class($command).' added to queue') || true)); |
|
524 | |||
525 | 10 | if($this->parser && $this->busy === static::STATE_IDLE) { |
|
526 | 9 | \assert((\Plasma\Drivers\MySQL\Messages\MessageUtility::debug('Command '.get_class($command).' invoked into parser') || true)); |
|
527 | 9 | $this->parser->invokeCommand($this->getNextCommand()); |
|
528 | } |
||
529 | 10 | } |
|
530 | |||
531 | /** |
||
532 | * Get the handshake message, or null if none received yet. |
||
533 | * @return \Plasma\Drivers\MySQL\Messages\HandshakeMessage|null |
||
534 | */ |
||
535 | function getHandshake(): ?\Plasma\Drivers\MySQL\Messages\HandshakeMessage { |
||
536 | if($this->parser) { |
||
537 | return $this->parser->getHandshakeMessage(); |
||
538 | } |
||
539 | |||
540 | return null; |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * Get the next command, or null. |
||
545 | * @return \Plasma\CommandInterface|null |
||
546 | * @internal |
||
547 | */ |
||
548 | 9 | function getNextCommand(): ?\Plasma\CommandInterface { |
|
549 | 9 | if(\count($this->queue) === 0) { |
|
550 | 3 | if($this->goingAway) { |
|
551 | $this->goingAway->resolve(); |
||
552 | } |
||
553 | |||
554 | 3 | return null; |
|
555 | 9 | } elseif($this->busy === static::STATE_BUSY) { |
|
556 | return null; |
||
557 | } |
||
558 | |||
559 | /** @var \Plasma\CommandInterface $command */ |
||
560 | 9 | $command = \array_shift($this->queue); |
|
561 | |||
562 | 9 | \assert((\Plasma\Drivers\MySQL\Messages\MessageUtility::debug('Unshifted command '.get_class($command)) || true)); |
|
563 | |||
564 | 9 | if($command->waitForCompletion()) { |
|
565 | 9 | $this->busy = static::STATE_BUSY; |
|
566 | |||
567 | $command->once('error', function () use (&$command) { |
||
568 | 2 | \assert((\Plasma\Drivers\MySQL\Messages\MessageUtility::debug('Command '.get_class($command).' errored') || true)); |
|
569 | 2 | $this->busy = static::STATE_IDLE; |
|
570 | |||
571 | 2 | $this->endCommand(); |
|
572 | 9 | }); |
|
573 | |||
574 | $command->once('end', function () use (&$command) { |
||
575 | 5 | \assert((\Plasma\Drivers\MySQL\Messages\MessageUtility::debug('Command '.get_class($command).' ended') || true)); |
|
576 | 5 | $this->busy = static::STATE_IDLE; |
|
577 | |||
578 | 5 | $this->endCommand(); |
|
579 | 9 | }); |
|
580 | } else { |
||
581 | $this->endCommand(); |
||
582 | } |
||
583 | |||
584 | 9 | return $command; |
|
585 | } |
||
586 | |||
587 | /** |
||
588 | * Finishes up a command. |
||
589 | * @return void |
||
590 | */ |
||
591 | 6 | protected function endCommand() { |
|
592 | $this->loop->futureTick(function () { |
||
593 | 5 | if($this->goingAway && \count($this->queue) === 0) { |
|
594 | 1 | return $this->goingAway->resolve(); |
|
595 | } |
||
596 | |||
597 | 5 | $this->parser->invokeCommand($this->getNextCommand()); |
|
598 | 6 | }); |
|
599 | 6 | } |
|
600 | |||
601 | /** |
||
602 | * Starts the handshake process. |
||
603 | * @param array $credentials |
||
604 | * @param \React\Promise\Deferred $deferred |
||
605 | * @return void |
||
606 | */ |
||
607 | 12 | protected function startHandshake(array $credentials, \React\Promise\Deferred $deferred) { |
|
608 | $listener = function (\Plasma\Drivers\MySQL\Messages\MessageInterface $message) use ($credentials, &$deferred, &$listener) { |
||
609 | 12 | if($message instanceof \Plasma\Drivers\MySQL\Messages\HandshakeMessage) { |
|
610 | 12 | $this->parser->removeListener('message', $listener); |
|
611 | |||
612 | 12 | $this->connectionState = static::CONNECTION_SETENV; |
|
613 | 12 | $clientFlags = \Plasma\Drivers\MySQL\ProtocolParser::CLIENT_CAPABILITIES; |
|
614 | |||
615 | 12 | \extract($credentials); |
|
616 | |||
617 | 12 | if($db !== '') { |
|
618 | 4 | $clientFlags |= \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_CONNECT_WITH_DB; |
|
619 | } |
||
620 | |||
621 | // Check if we support auth plugins |
||
622 | 12 | $plugins = \Plasma\Drivers\MySQL\DriverFactory::getAuthPlugins(); |
|
623 | 12 | $plugin = null; |
|
624 | |||
625 | 12 | foreach($plugins as $key => $plug) { |
|
626 | 12 | if(\is_int($key) && ($message->capability & $key) !== 0) { |
|
627 | 12 | $plugin = $plug; |
|
628 | 12 | $clientFlags |= \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PLUGIN_AUTH; |
|
629 | 12 | break; |
|
630 | } elseif($key === $message->authPluginName) { |
||
631 | $plugin = $plug; |
||
632 | $clientFlags |= \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PLUGIN_AUTH; |
||
633 | break; |
||
634 | } |
||
635 | } |
||
636 | |||
637 | 12 | $remote = \parse_url($this->connection->getRemoteAddress())['host']; |
|
638 | |||
639 | 12 | if($remote !== '127.0.0.1' || $this->options['tls.forceLocal']) { |
|
640 | if(($message->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_SSL) !== 0) { // If SSL supported, connect through SSL |
||
641 | $clientFlags |= \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_SSL; |
||
642 | |||
643 | $ssl = new \Plasma\Drivers\MySQL\Commands\SSLRequestCommand($message, $clientFlags); |
||
644 | |||
645 | $ssl->once('end', function () use ($credentials, $clientFlags, $plugin, &$deferred, &$message) { |
||
646 | $this->connectionState = static::CONNECTION_SSL_STARTUP; |
||
647 | |||
648 | $this->enableTLS()->then(function () use ($credentials, $clientFlags, $plugin, &$deferred, &$message) { |
||
649 | $this->createHandshakeResponse($message, $credentials, $clientFlags, $plugin, $deferred); |
||
650 | }, function (\Throwable $error) use (&$deferred) { |
||
651 | $deferred->reject($$error); |
||
652 | $this->connection->close(); |
||
653 | }); |
||
654 | }); |
||
655 | |||
656 | return $this->parser->invokeCommand($ssl); |
||
657 | } elseif($this->options['tls.force'] || $this->options['tls.forceLocal']) { |
||
658 | $deferred->reject((new \Plasma\Exception('TLS is not supported by the server'))); |
||
659 | $this->connection->close(); |
||
660 | return; |
||
661 | } |
||
662 | } |
||
663 | |||
664 | 12 | $this->createHandshakeResponse($message, $credentials, $clientFlags, $plugin, $deferred); |
|
665 | } |
||
666 | 12 | }; |
|
667 | |||
668 | 12 | $this->parser->on('message', $listener); |
|
669 | |||
670 | $this->parser->on('message', function (\Plasma\Drivers\MySQL\Messages\MessageInterface $message) { |
||
671 | 12 | if($message instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage) { |
|
672 | 11 | $this->connectionState = static::CONNECTION_OK; |
|
673 | } |
||
674 | |||
675 | 12 | $this->emit('eventRelay', array('message', $message)); |
|
676 | 12 | }); |
|
677 | 12 | } |
|
678 | |||
679 | /** |
||
680 | * Enables TLS on the connection. |
||
681 | * @return \React\Promise\PromiseInterface |
||
682 | */ |
||
683 | protected function enableTLS(): \React\Promise\PromiseInterface { |
||
684 | // Set required SSL/TLS context options |
||
685 | foreach($this->options['tls.context'] as $name => $value) { |
||
686 | \stream_context_set_option($this->connection->stream, 'ssl', $name, $value); |
||
687 | } |
||
688 | |||
689 | return $this->encryption->enable($this->connection)->otherwise(function (\Throwable $error) { |
||
690 | $this->connection->close(); |
||
691 | throw new \RuntimeException('Connection failed during TLS handshake: '.$error->getMessage(), $error->getCode()); |
||
692 | }); |
||
693 | } |
||
694 | |||
695 | /** |
||
696 | * Sends the auth command. |
||
697 | * @param \Plasma\Drivers\MySQL\Messages\HandshakeMessage $message |
||
698 | * @param array $credentials |
||
699 | * @param int $clientFlags |
||
700 | * @param string|null $plugin |
||
701 | * @param \React\Promise\Deferred $deferred |
||
702 | * @return void |
||
703 | */ |
||
704 | 12 | protected function createHandshakeResponse( |
|
705 | \Plasma\Drivers\MySQL\Messages\HandshakeMessage $message, array $credentials, int $clientFlags, ?string $plugin, \React\Promise\Deferred $deferred |
||
706 | ) { |
||
707 | 12 | \extract($credentials); |
|
708 | |||
709 | 12 | $auth = new \Plasma\Drivers\MySQL\Commands\HandshakeResponseCommand($this->parser, $message, $clientFlags, $plugin, $user, $password, $db); |
|
710 | |||
711 | $auth->once('end', function () use (&$deferred) { |
||
712 | 11 | $deferred->resolve(); |
|
713 | 12 | }); |
|
714 | |||
715 | $auth->once('error', function (\Throwable $error) use (&$deferred) { |
||
716 | 1 | $deferred->reject($error); |
|
717 | 1 | $this->connection->close(); |
|
718 | 12 | }); |
|
719 | |||
720 | 12 | if($plugin) { |
|
721 | $listener = function (\Plasma\Drivers\MySQL\Messages\MessageInterface $message) use ($password, &$deferred, &$listener) { |
||
722 | /** @var \Plasma\Drivers\MySQL\AuthPlugins\AuthPluginInterface|null $plugin */ |
||
723 | 12 | static $plugin; |
|
724 | |||
725 | 12 | if($message instanceof \Plasma\Drivers\MySQL\Messages\AuthSwitchRequestMessage) { |
|
726 | $name = $message->authPluginName; |
||
727 | |||
728 | if($name !== null) { |
||
729 | foreach($plugins as $key => $plug) { |
||
730 | if($key === $name) { |
||
731 | $plugin = new $plug($this->parser, $message); |
||
732 | |||
733 | $command = new \Plasma\Drivers\MySQL\Commands\AuthSwitchResponseCommand($message, $plugin, $password); |
||
734 | return $this->parser->invokeCommand($command); |
||
735 | } |
||
736 | } |
||
737 | } |
||
738 | |||
739 | $deferred->reject((new \Plasma\Exception('Requested authentication method '.($name ? '"'.$name.'" ' : '').'is not supported'))); |
||
740 | 12 | } elseif($message instanceof \Plasma\Drivers\MySQL\Messages\AuthMoreDataMessage) { |
|
741 | if($plugin === null) { |
||
742 | $deferred->reject((new \Plasma\Exception('No auth plugin is in use, but we received auth more data packet'))); |
||
743 | return $this->connection->close(); |
||
744 | } |
||
745 | |||
746 | try { |
||
747 | $command = $plugin->receiveMoreData($message); |
||
748 | return $this->parser->invokeCommand($command); |
||
749 | } catch (\Plasma\Exception $e) { |
||
750 | $deferred->reject($e); |
||
751 | $this->connection->close(); |
||
752 | } |
||
753 | 12 | } elseif($message instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage) { |
|
754 | 11 | $this->parser->removeListener('message', $listener); |
|
755 | } |
||
756 | 12 | }; |
|
757 | |||
758 | 12 | $this->parser->on('message', $listener); |
|
759 | } |
||
760 | |||
761 | 12 | $this->parser->invokeCommand($auth); |
|
762 | 12 | $this->connectionState = static::CONNECTION_AWAITING_RESPONSE; |
|
763 | 12 | } |
|
764 | |||
765 | /** |
||
766 | * Validates the given options. |
||
767 | * @param array $options |
||
768 | * @return void |
||
769 | * @throws \InvalidArgumentException |
||
770 | */ |
||
771 | 17 | protected function validateOptions(array $options) { |
|
780 | 17 | } |
|
781 | } |
||
782 |