Skip to content

Commit

Permalink
Changed the way sort are apply
Browse files Browse the repository at this point in the history
As @chinpei215 pointed there were some errors when SORT_NAME was
provided.
  • Loading branch information
tzaoh committed Apr 30, 2016
1 parent 3c0e77a commit 6147d70
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/Filesystem/Folder.php
Expand Up @@ -85,6 +85,14 @@ class Folder
*/
public $mode = 0755;

/**
*
*/
protected $_fsorts = [
self::SORT_NAME => 'getPathname',
self::SORT_TIME => 'getCTime'
];

/**
* Holds messages from last method.
*
Expand Down Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down
23 changes: 23 additions & 0 deletions tests/TestCase/Filesystem/FolderTest.php
Expand Up @@ -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);
}
}

0 comments on commit 6147d70

Please sign in to comment.