Skip to content

Commit

Permalink
Merge branch '3.x' into feature/3.x/add-azure-blob-storage
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge authored Jan 30, 2022
2 parents c48fdc7 + d4eedc1 commit c0cb31f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 3.0.2 - 2022-01-30

### Fixes

* [FTP] Support relative or empty connection root directories (#1410)

## 3.0.1 - 2022-01-15

### Fixes
Expand All @@ -20,6 +26,12 @@
* FilesystemAdapter::directoryExists to check for directory existence
* FilesystemAdapter::fileExists to check for file existence

## 2.4.1 - 2022-01-30

### Fixed

- [FTP] Fix relative connection root handling

## 2.4.0 - 2022-01-04

### Added
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"require-dev": {
"ext-zip": "*",
"ext-fileinfo": "*",
"ext-ftp": "*",
"microsoft/azure-storage-blob": "^1.1",
"phpunit/phpunit": "^9.5.11",
"phpstan/phpstan": "^0.12.26",
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parameters:
- src/PhpseclibV2
- src/PhpseclibV3
ignoreErrors:
- '#invalid typehint type FTP\\Connection#'
- '#FTP\\Connection not found#'
- '#unknown class FTP\\Connection#'
- '#Call to function iterator_to_array\(\) on a separate line has no effect\.#'
Expand Down
40 changes: 31 additions & 9 deletions src/Ftp/FtpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
use League\MimeTypeDetection\MimeTypeDetector;
use Throwable;

use function ftp_chdir;
use function ftp_pwd;

class FtpAdapter implements FilesystemAdapter
{
private const SYSTEM_TYPE_WINDOWS = 'windows';
Expand Down Expand Up @@ -77,6 +80,8 @@ class FtpAdapter implements FilesystemAdapter
*/
private $mimeTypeDetector;

private ?string $rootDirectory = null;

public function __construct(
FtpConnectionOptions $connectionOptions,
FtpConnectionProvider $connectionProvider = null,
Expand All @@ -88,7 +93,8 @@ public function __construct(
$this->connectionProvider = $connectionProvider ?: new FtpConnectionProvider();
$this->connectivityChecker = $connectivityChecker ?: new NoopCommandConnectivityChecker();
$this->visibilityConverter = $visibilityConverter ?: new PortableVisibilityConverter();
$this->prefixer = new PathPrefixer($connectionOptions->root());
$this->rootDirectory = $this->resolveConnectionRoot($this->connection());
$this->prefixer = new PathPrefixer($this->rootDirectory);
$this->mimeTypeDetector = $mimeTypeDetector ?: new FinfoMimeTypeDetector();
}

Expand All @@ -111,14 +117,16 @@ private function connection()
start:
if ( ! $this->hasFtpConnection()) {
$this->connection = $this->connectionProvider->createConnection($this->connectionOptions);

return $this->connection;
}

if ($this->connectivityChecker->isConnected($this->connection) === false) {
$this->connection = false;
goto start;
}

ftp_chdir($this->connection, $this->connectionOptions->root());
ftp_chdir($this->connection, $this->rootDirectory);

return $this->connection;
}
Expand Down Expand Up @@ -434,15 +442,15 @@ private function normalizeUnixObject(string $item, string $base): StorageAttribu
$isDirectory = $this->listingItemIsDirectory($permissions);
$permissions = $this->normalizePermissions($permissions);
$path = $base === '' ? $name : rtrim($base, '/') . '/' . $name;
$lastModified = $this->connectionOptions->timestampsOnUnixListingsEnabled()
? $this->normalizeUnixTimestamp($month, $day, $timeOrYear)
: null;
$lastModified = $this->connectionOptions->timestampsOnUnixListingsEnabled() ? $this->normalizeUnixTimestamp(
$month,
$day,
$timeOrYear
) : null;

if ($isDirectory) {
return new DirectoryAttributes(
$path,
$this->visibilityConverter->inverseForDirectory($permissions),
$lastModified
$path, $this->visibilityConverter->inverseForDirectory($permissions), $lastModified
);
}

Expand Down Expand Up @@ -585,7 +593,7 @@ private function ensureDirectoryExists(string $dirname, ?string $visibility): vo
$connection = $this->connection();

$dirPath = '';
$parts = explode('/', rtrim($dirname, '/'));
$parts = explode('/', trim($dirname, '/'));
$mode = $visibility ? $this->visibilityConverter->forDirectory($visibility) : false;

foreach ($parts as $part) {
Expand Down Expand Up @@ -629,4 +637,18 @@ public function directoryExists(string $path): bool

return @ftp_chdir($connection, $path) === true;
}

/**
* @param resource|\FTP\Connection $connection
*/
private function resolveConnectionRoot($connection): string
{
$root = $this->connectionOptions->root();

if ($root !== '') {
ftp_chdir($connection, $root);
}

return ftp_pwd($connection);
}
}
24 changes: 24 additions & 0 deletions src/Ftp/FtpAdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ public function resetFunctionMocks(): void
reset_function_mocks();
}

/**
* @test
*/
public function using_empty_string_for_root(): void
{
$options = FtpConnectionOptions::fromArray([
'host' => 'localhost',
'port' => 2121,
'root' => '',
'username' => 'foo',
'password' => 'pass',
]);

$this->runScenario(function () use ($options) {
$adapter = new FtpAdapter($options);

$adapter->write('dirname1/dirname2/path.txt', 'contents', new Config());
$adapter->write('dirname1/dirname2/path.txt', 'contents', new Config());

$this->assertTrue($adapter->fileExists('dirname1/dirname2/path.txt'));
$this->assertSame('contents', $adapter->read('dirname1/dirname2/path.txt'));
});
}

/**
* @test
*/
Expand Down

0 comments on commit c0cb31f

Please sign in to comment.