From a3fa8c0b5a16c2ecb1ab4681e3979e567e89d7d6 Mon Sep 17 00:00:00 2001 From: Tristan Veness Date: Tue, 26 Mar 2019 03:54:04 +1100 Subject: [PATCH] Opt-in 'enableTimestampsOnUnixListings' for AbstractFtpAdapter --- src/Adapter/AbstractFtpAdapter.php | 28 ++++++++- src/Adapter/Ftp.php | 1 + tests/FtpTests.php | 96 ++++++++++++++++++++---------- 3 files changed, 91 insertions(+), 34 deletions(-) diff --git a/src/Adapter/AbstractFtpAdapter.php b/src/Adapter/AbstractFtpAdapter.php index 3f04c64cb..d0b2929ba 100644 --- a/src/Adapter/AbstractFtpAdapter.php +++ b/src/Adapter/AbstractFtpAdapter.php @@ -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. * @@ -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 */ @@ -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; } /** diff --git a/src/Adapter/Ftp.php b/src/Adapter/Ftp.php index a3ebaa75f..f622b9dbc 100644 --- a/src/Adapter/Ftp.php +++ b/src/Adapter/Ftp.php @@ -53,6 +53,7 @@ class Ftp extends AbstractFtpAdapter 'ignorePassiveAddress', 'recurseManually', 'utf8', + 'enableTimestampsOnUnixListings', ]; /** diff --git a/tests/FtpTests.php b/tests/FtpTests.php index dfe8fe2c9..c90ebc05f 100755 --- a/tests/FtpTests.php +++ b/tests/FtpTests.php @@ -647,6 +647,7 @@ public function expectedUnixListings() { [ /*$directory=*/ '', /*$recursive=*/ false, + /*$enableTimestamps=*/ true, /*'expectedListing'=>*/ [ [ 'type' => 'dir', @@ -676,6 +677,7 @@ public function expectedUnixListings() { [ /*$directory=*/ '', /*$recursive=*/ true, + /*$enableTimestamps=*/ true, /*'expectedListing'=>*/ [ [ 'type' => 'dir', @@ -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, + ], + ] ] ]; } @@ -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); }