Added sorting by modified time in Folder util

This commit is contained in:
Steampilot 2016-05-12 16:28:04 +02:00
parent 66077f9312
commit 723ed96fd6
2 changed files with 118 additions and 6 deletions

View file

@ -1227,4 +1227,85 @@ class FolderTest extends CakeTestCase {
$Folder->delete(); $Folder->delete();
} }
/**
* testSortByTime method
*
* Verify that the order using modified time is correct.
*
* @return void
*/
public function testSortByTime() {
$Folder = new Folder(TMP . 'test_sort_by_time', true);
$file2 = new File($Folder->pwd() . DS . 'file_2.tmp');
$file2->create();
sleep(1);
$file1 = new File($Folder->pwd() . DS . 'file_1.tmp');
$file1->create();
$expected = array('file_2.tmp', 'file_1.tmp');
$result = $Folder->find('.*', Folder::SORT_TIME);
$this->assertSame($expected, $result);
$Folder->delete();
}
/**
* testSortByTime2 method
*
* Verify that the sort order using modified time is correct.
*
* @return void
*/
public function testSortByTime2() {
$Folder = new Folder(TMP . 'test_sort_by_time2', true);
$fileC = new File($Folder->pwd() . DS . 'c.txt');
$fileC->create();
sleep(1);
$fileA = new File($Folder->pwd() . DS . 'a.txt');
$fileA->create();
sleep(1);
$fileB = new File($Folder->pwd() . DS . 'b.txt');
$fileB->create();
$expected = array('c.txt', 'a.txt', 'b.txt');
$result = $Folder->find('.*', Folder::SORT_TIME);
$this->assertSame($expected, $result);
$Folder->delete();
}
/**
* Verify that the sort order using name is correct.
*
* @return void
*/
public function testSortByName() {
$Folder = new Folder(TMP . 'test_sort_by_name', 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();
$expected = array('a.txt', 'b.txt', 'c.txt');
$result = $Folder->find('.*', Folder::SORT_NAME);
$this->assertSame($expected, $result);
$Folder->delete();
}
} }

View file

@ -46,6 +46,16 @@ class Folder {
*/ */
const SKIP = 'skip'; const SKIP = 'skip';
/**
* Sort mode by name
*/
const SORT_NAME = 'name';
/**
* Sort mode by time
*/
const SORT_TIME = 'time';
/** /**
* Path to Folder. * Path to Folder.
* *
@ -71,6 +81,14 @@ class Folder {
*/ */
public $mode = 0755; public $mode = 0755;
/**
* Functions array to be called depending on the sort type chosen.
*/
protected $_fsorts = array(
self::SORT_NAME => 'getPathname',
self::SORT_TIME => 'getCTime'
);
/** /**
* Holds messages from last method. * Holds messages from last method.
* *
@ -155,14 +173,14 @@ class Folder {
* Returns an array of the contents of the current directory. * Returns an array of the contents of the current directory.
* The returned array holds two arrays: One of directories and one of files. * The returned array holds two arrays: One of directories and one of files.
* *
* @param bool $sort Whether you want the results sorted, set this and the sort property * @param string|bool $sort Whether you want the results sorted, set this and the sort property
* to false to get unsorted results. * to false to get unsorted results.
* @param array|bool $exceptions Either an array or boolean true will not grab dot files * @param array|bool $exceptions Either an array or boolean true will not grab dot files
* @param bool $fullPath True returns the full path * @param bool $fullPath True returns the full path
* @return mixed Contents of current directory as an array, an empty array on failure * @return mixed Contents of current directory as an array, an empty array on failure
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::read * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::read
*/ */
public function read($sort = true, $exceptions = false, $fullPath = false) { public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) {
$dirs = $files = array(); $dirs = $files = array();
if (!$this->pwd()) { if (!$this->pwd()) {
@ -178,6 +196,11 @@ class Folder {
} catch (Exception $e) { } catch (Exception $e) {
return array($dirs, $files); return array($dirs, $files);
} }
if (!is_bool($sort) && isset($this->_fsorts[$sort])) {
$methodName = $this->_fsorts[$sort];
} else {
$methodName = $this->_fsorts[self::SORT_NAME];
}
foreach ($iterator as $item) { foreach ($iterator as $item) {
if ($item->isDot()) { if ($item->isDot()) {
@ -191,14 +214,22 @@ class Folder {
$name = $item->getPathName(); $name = $item->getPathName();
} }
if ($item->isDir()) { if ($item->isDir()) {
$dirs[] = $name; $dirs[$item->{$methodName}()][] = $name;
} else { } else {
$files[] = $name; $files[$item->{$methodName}()][] = $name;
} }
} }
if ($sort || $this->sort) { if ($sort || $this->sort) {
sort($dirs); ksort($dirs);
sort($files); ksort($files);
}
if ($dirs) {
$dirs = call_user_func_array('array_merge', $dirs);
}
if ($files) {
$files = call_user_func_array('array_merge', $files);
} }
return array($dirs, $files); return array($dirs, $files);
} }