Skip to content

Commit

Permalink
Cleanup ftp impls
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Mar 9, 2024
1 parent 1231daf commit 960bd77
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
8 changes: 3 additions & 5 deletions FtpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ public function __construct(
*/
public function __destruct()
{
if ($this->hasFtpConnection()) {
@ftp_close($this->connection);
}
$this->connection = false;
$this->disconnect();
}

/**
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion FtpAdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace League\Flysystem\Ftp;

use Ftp\StubConnectionProvider;
use Generator;
use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase;
use League\Flysystem\Config;
Expand Down Expand Up @@ -51,6 +50,12 @@ public function resetFunctionMocks(): void
reset_function_mocks();
}

public static function clearFilesystemAdapterCache(): void
{
parent::clearFilesystemAdapterCache();
static::$connectionProvider = null;
}

/**
* @test
*/
Expand Down
5 changes: 4 additions & 1 deletion FtpConnectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion NoopCommandConnectivityCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions UnableToConnectToFtpHost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 960bd77

Please sign in to comment.