From 960bd77046d6bf3effb992c5c53b48c8648c15b5 Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Sat, 9 Mar 2024 14:16:40 +0100 Subject: [PATCH] Cleanup ftp impls --- FtpAdapter.php | 8 +++----- FtpAdapterTestCase.php | 7 ++++++- FtpConnectionProvider.php | 5 ++++- NoopCommandConnectivityCheckerTest.php | 2 +- UnableToConnectToFtpHost.php | 4 ++-- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/FtpAdapter.php b/FtpAdapter.php index 76a3644..09c14f7 100644 --- a/FtpAdapter.php +++ b/FtpAdapter.php @@ -75,10 +75,7 @@ public function __construct( */ public function __destruct() { - if ($this->hasFtpConnection()) { - @ftp_close($this->connection); - } - $this->connection = false; + $this->disconnect(); } /** @@ -108,8 +105,9 @@ private function connection() public function disconnect(): void { if ($this->hasFtpConnection()) { - ftp_close($this->connection); + @ftp_close($this->connection); } + $this->connection = false; } private function isPureFtpdServer(): bool diff --git a/FtpAdapterTestCase.php b/FtpAdapterTestCase.php index 85bef62..aeea202 100644 --- a/FtpAdapterTestCase.php +++ b/FtpAdapterTestCase.php @@ -4,7 +4,6 @@ namespace League\Flysystem\Ftp; -use Ftp\StubConnectionProvider; use Generator; use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase; use League\Flysystem\Config; @@ -51,6 +50,12 @@ public function resetFunctionMocks(): void reset_function_mocks(); } + public static function clearFilesystemAdapterCache(): void + { + parent::clearFilesystemAdapterCache(); + static::$connectionProvider = null; + } + /** * @test */ diff --git a/FtpConnectionProvider.php b/FtpConnectionProvider.php index 09e12a6..67bbaa6 100644 --- a/FtpConnectionProvider.php +++ b/FtpConnectionProvider.php @@ -4,6 +4,8 @@ namespace League\Flysystem\Ftp; +use function error_clear_last; +use function error_get_last; use const FTP_USEPASVADDRESS; class FtpConnectionProvider implements ConnectionProvider @@ -40,10 +42,11 @@ public function createConnection(FtpConnectionOptions $options) */ private function createConnectionResource(string $host, int $port, int $timeout, bool $ssl) { + error_clear_last(); $connection = $ssl ? @ftp_ssl_connect($host, $port, $timeout) : @ftp_connect($host, $port, $timeout); if ($connection === false) { - throw UnableToConnectToFtpHost::forHost($host, $port, $ssl); + throw UnableToConnectToFtpHost::forHost($host, $port, $ssl, error_get_last()['message'] ?? ''); } return $connection; diff --git a/NoopCommandConnectivityCheckerTest.php b/NoopCommandConnectivityCheckerTest.php index e157538..e8299f6 100644 --- a/NoopCommandConnectivityCheckerTest.php +++ b/NoopCommandConnectivityCheckerTest.php @@ -26,7 +26,7 @@ public function detecting_a_good_connection(): void { $options = FtpConnectionOptions::fromArray([ 'host' => 'localhost', - 'port' => 2122, + 'port' => 2121, 'root' => '/home/foo/upload', 'username' => 'foo', 'password' => 'pass', diff --git a/UnableToConnectToFtpHost.php b/UnableToConnectToFtpHost.php index 0543498..8d66908 100644 --- a/UnableToConnectToFtpHost.php +++ b/UnableToConnectToFtpHost.php @@ -8,10 +8,10 @@ final class UnableToConnectToFtpHost extends RuntimeException implements FtpConnectionException { - public static function forHost(string $host, int $port, bool $ssl): UnableToConnectToFtpHost + public static function forHost(string $host, int $port, bool $ssl, string $reason = ''): UnableToConnectToFtpHost { $usingSsl = $ssl ? ', using ssl' : ''; - return new UnableToConnectToFtpHost("Unable to connect to host $host at port $port$usingSsl."); + return new UnableToConnectToFtpHost("Unable to connect to host $host at port $port$usingSsl. $reason"); } }