diff --git a/src/Filesystem/Folder.php b/src/Filesystem/Folder.php index b734df052bc..2637f0493bf 100644 --- a/src/Filesystem/Folder.php +++ b/src/Filesystem/Folder.php @@ -85,6 +85,14 @@ class Folder */ public $mode = 0755; + /** + * + */ + protected $_fsorts = [ + self::SORT_NAME => 'getPathname', + self::SORT_TIME => 'getCTime' + ]; + /** * Holds messages from last method. * @@ -193,6 +201,12 @@ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = f return [$dirs, $files]; } + if (!is_bool($sort) && array_key_exists($sort, $this->_fsorts)) { + $methodName = $this->_fsorts[$sort]; + } else { + $methodName = $this->_fsorts[self::SORT_NAME]; + } + foreach ($iterator as $item) { if ($item->isDot()) { continue; @@ -206,20 +220,15 @@ public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = f } if ($item->isDir()) { - $dirs[$item->getCTime()][] = $name; + $dirs[$item->{$methodName}()][] = $name; } else { - $files[$item->getCTime()][] = $name; + $files[$item->{$methodName}()][] = $name; } } if ($sort || $this->sort) { - if ($sort === self::SORT_TIME || $this->sort === self::SORT_TIME) { - ksort($dirs); - ksort($files); - } elseif ($sort === self::SORT_NAME || $this->sort === self::SORT_NAME) { - sort($dirs); - sort($files); - } + ksort($dirs); + ksort($files); } if (!empty($dirs)) { diff --git a/tests/TestCase/Filesystem/FolderTest.php b/tests/TestCase/Filesystem/FolderTest.php index e57085aac4e..bfc2cf621f6 100644 --- a/tests/TestCase/Filesystem/FolderTest.php +++ b/tests/TestCase/Filesystem/FolderTest.php @@ -1298,4 +1298,27 @@ public function testSortByTime2() $this->assertSame(['a.txt', 'c.txt', 'b.txt'], $results); } + + /** + * Verify that the order using name is correct. + */ + public function testSortByName() + { + $Folder = new Folder(TMP . 'tests', true); + + $fileA = new File($Folder->pwd() . DS . 'a.txt'); + $fileA->create(); + + $fileC = new File($Folder->pwd() . DS . 'c.txt'); + $fileC->create(); + + sleep(1); + + $fileB = new File($Folder->pwd() . DS . 'b.txt'); + $fileB->create(); + + $results = $Folder->find('.*', Folder::SORT_NAME); + + $this->assertSame(['a.txt', 'b.txt', 'c.txt'], $results); + } }