From c1ff625e526c9ca46fa7bf2d275e2318f058cfd1 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sat, 9 Dec 2023 21:16:58 -0600 Subject: [PATCH] Pass socket connector to connect() --- src/SocketMysqlConnection.php | 15 +++++++++++++-- src/SocketMysqlConnector.php | 5 ++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/SocketMysqlConnection.php b/src/SocketMysqlConnection.php index 11be004..7c3982f 100644 --- a/src/SocketMysqlConnection.php +++ b/src/SocketMysqlConnection.php @@ -4,7 +4,9 @@ use Amp\Cancellation; use Amp\DeferredFuture; -use Amp\Socket\Socket; +use Amp\Socket\SocketConnector; +use Amp\Socket\SocketException; +use Amp\Sql\SqlException; use Amp\Sql\TransactionIsolation; use Amp\Sql\TransactionIsolationLevel; use Revolt\EventLoop; @@ -19,10 +21,19 @@ final class SocketMysqlConnection implements MysqlConnection private readonly \Closure $release; public static function connect( - Socket $socket, + SocketConnector $connector, MysqlConfig $config, ?Cancellation $cancellation = null, ): self { + try { + $socket = $connector->connect($config->getConnectionString(), $config->getConnectContext(), $cancellation); + } catch (SocketException $exception) { + throw new SqlException( + 'Connecting to the MySQL server failed: ' . $exception->getMessage(), + previous: $exception, + ); + } + $processor = new Internal\ConnectionProcessor($socket, $config); $processor->connect($cancellation); return new self($processor); diff --git a/src/SocketMysqlConnector.php b/src/SocketMysqlConnector.php index da1251d..d9004a7 100644 --- a/src/SocketMysqlConnector.php +++ b/src/SocketMysqlConnector.php @@ -22,9 +22,8 @@ public function connect(SqlConfig $config, ?Cancellation $cancellation = null): throw new \TypeError(\sprintf("Must provide an instance of %s to MySQL connectors", MysqlConfig::class)); } - $socket = ($this->connector ?? Socket\socketConnector()) - ->connect($config->getConnectionString(), $config->getConnectContext(), $cancellation); + $connector = $this->connector ?? Socket\socketConnector(); - return SocketMysqlConnection::connect($socket, $config, $cancellation); + return SocketMysqlConnection::connect($connector, $config, $cancellation); } }