Skip to content

Commit

Permalink
Opt-in 'enableTimestampsOnUnixListings' for AbstractFtpAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanveness committed Mar 25, 2019
1 parent d0170f1 commit a3fa8c0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 34 deletions.
28 changes: 25 additions & 3 deletions src/Adapter/AbstractFtpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ abstract class AbstractFtpAdapter extends AbstractAdapter
*/
protected $safeStorage;

/**
* True to enable timestamps for FTP servers that return unix-style listings
*
* @var bool
*/
protected $enableTimestampsOnUnixListings = false;

/**
* Constructor.
*
Expand Down Expand Up @@ -309,6 +316,18 @@ public function setSystemType($systemType)
return $this;
}

/**
* True to enable timestamps for FTP servers that return unix-style listings
*
* @param bool $bool
* @return $this
*/
public function setEnableTimestampsOnUnixListings($bool = false) {
$this->enableTimestampsOnUnixListings = $bool;

return $this;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -426,9 +445,12 @@ protected function normalizeUnixObject($item, $base)
$visibility = $permissions & 0044 ? AdapterInterface::VISIBILITY_PUBLIC : AdapterInterface::VISIBILITY_PRIVATE;
$size = (int) $size;

$timestamp = $this->normalizeUnixTimestamp($month, $day, $timeOrYear);

return compact('type', 'path', 'visibility', 'size', 'timestamp');
$result = compact('type', 'path', 'visibility', 'size');
if ($this->enableTimestampsOnUnixListings) {
$timestamp = $this->normalizeUnixTimestamp($month, $day, $timeOrYear);
$result += compact('timestamp');
}
return $result;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Adapter/Ftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Ftp extends AbstractFtpAdapter
'ignorePassiveAddress',
'recurseManually',
'utf8',
'enableTimestampsOnUnixListings',
];

/**
Expand Down
96 changes: 65 additions & 31 deletions tests/FtpTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ public function expectedUnixListings() {
[
/*$directory=*/ '',
/*$recursive=*/ false,
/*$enableTimestamps=*/ true,
/*'expectedListing'=>*/ [
[
'type' => 'dir',
Expand Down Expand Up @@ -676,6 +677,7 @@ public function expectedUnixListings() {
[
/*$directory=*/ '',
/*$recursive=*/ true,
/*$enableTimestamps=*/ true,
/*'expectedListing'=>*/ [
[
'type' => 'dir',
Expand Down Expand Up @@ -704,36 +706,68 @@ public function expectedUnixListings() {
[
/*$directory=*/ 'lastfiledir',
/*$recursive=*/ true,
/*$enableTimestamps=*/ true,
/*'expectedListing'=>*/ [
[
'type' => 'file',
'path' => 'lastfiledir/file1.txt',
'visibility' => 'public',
'size' => 409,
'timestamp' => 1566205260,
],
[
'type' => 'file',
'path' => 'lastfiledir/file2.txt',
'visibility' => 'public',
'size' => 409,
'timestamp' => 1565773260,
],
[
'type' => 'file',
'path' => 'lastfiledir/file3.txt',
'visibility' => 'public',
'size' => 409,
'timestamp' => 1549447560,
],
[
'type' => 'file',
'path' => 'lastfiledir/file4.txt',
'visibility' => 'public',
'size' => 409,
'timestamp' => 1395273600,
],
]
[
'type' => 'file',
'path' => 'lastfiledir/file1.txt',
'visibility' => 'public',
'size' => 409,
'timestamp' => 1566205260,
],
[
'type' => 'file',
'path' => 'lastfiledir/file2.txt',
'visibility' => 'public',
'size' => 409,
'timestamp' => 1565773260,
],
[
'type' => 'file',
'path' => 'lastfiledir/file3.txt',
'visibility' => 'public',
'size' => 409,
'timestamp' => 1549447560,
],
[
'type' => 'file',
'path' => 'lastfiledir/file4.txt',
'visibility' => 'public',
'size' => 409,
'timestamp' => 1395273600,
],
]
],
[
/*$directory=*/ 'lastfiledir',
/*$recursive=*/ true,
/*$enableTimestamps=*/ false,
/*'expectedListing'=>*/ [
[
'type' => 'file',
'path' => 'lastfiledir/file1.txt',
'visibility' => 'public',
'size' => 409,
],
[
'type' => 'file',
'path' => 'lastfiledir/file2.txt',
'visibility' => 'public',
'size' => 409,
],
[
'type' => 'file',
'path' => 'lastfiledir/file3.txt',
'visibility' => 'public',
'size' => 409,
],
[
'type' => 'file',
'path' => 'lastfiledir/file4.txt',
'visibility' => 'public',
'size' => 409,
],
]
]
];
}
Expand All @@ -742,8 +776,8 @@ public function expectedUnixListings() {
* @depends testInstantiable
* @dataProvider expectedUnixListings
*/
public function testListingFromUnixFormat($directory, $recursive, $expectedListing) {
$adapter = new Ftp($this->options);
public function testListingFromUnixFormat($directory, $recursive, $enableTimestamps, $expectedListing) {
$adapter = new Ftp($this->options += ['enableTimestampsOnUnixListings' => $enableTimestamps]);
$listing = $adapter->listContents($directory, $recursive);
$this->assertEquals($listing, $expectedListing);
}
Expand Down

0 comments on commit a3fa8c0

Please sign in to comment.