mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge pull request #8816 from steampilot/2.x-folder-time-sort
Added sorting by modified time in Folder util
This commit is contained in:
commit
326583153a
2 changed files with 118 additions and 6 deletions
|
@ -1227,4 +1227,85 @@ class FolderTest extends CakeTestCase {
|
|||
$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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -46,6 +46,16 @@ class Folder {
|
|||
*/
|
||||
const SKIP = 'skip';
|
||||
|
||||
/**
|
||||
* Sort mode by name
|
||||
*/
|
||||
const SORT_NAME = 'name';
|
||||
|
||||
/**
|
||||
* Sort mode by time
|
||||
*/
|
||||
const SORT_TIME = 'time';
|
||||
|
||||
/**
|
||||
* Path to Folder.
|
||||
*
|
||||
|
@ -71,6 +81,14 @@ class Folder {
|
|||
*/
|
||||
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.
|
||||
*
|
||||
|
@ -155,14 +173,14 @@ class Folder {
|
|||
* Returns an array of the contents of the current directory.
|
||||
* 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.
|
||||
* @param array|bool $exceptions Either an array or boolean true will not grab dot files
|
||||
* @param bool $fullPath True returns the full path
|
||||
* @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
|
||||
*/
|
||||
public function read($sort = true, $exceptions = false, $fullPath = false) {
|
||||
public function read($sort = self::SORT_NAME, $exceptions = false, $fullPath = false) {
|
||||
$dirs = $files = array();
|
||||
|
||||
if (!$this->pwd()) {
|
||||
|
@ -178,6 +196,11 @@ class Folder {
|
|||
} catch (Exception $e) {
|
||||
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) {
|
||||
if ($item->isDot()) {
|
||||
|
@ -191,14 +214,22 @@ class Folder {
|
|||
$name = $item->getPathName();
|
||||
}
|
||||
if ($item->isDir()) {
|
||||
$dirs[] = $name;
|
||||
$dirs[$item->{$methodName}()][] = $name;
|
||||
} else {
|
||||
$files[] = $name;
|
||||
$files[$item->{$methodName}()][] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
if ($sort || $this->sort) {
|
||||
sort($dirs);
|
||||
sort($files);
|
||||
ksort($dirs);
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue