Made default value and behavior of param $exceptions for Folder::tree() identical to same param in Folder::read()

This commit is contained in:
ADmad 2011-12-22 02:03:22 +05:30
parent b84c9a1aaa
commit bcab3d0cb9
3 changed files with 58 additions and 16 deletions

View file

@ -363,6 +363,41 @@ class FolderTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* testFolderReadWithHiddenFiles method
*
* @return void
*/
public function testFolderReadWithHiddenFiles() {
$this->skipIf(!is_writeable(TMP), 'Cant test Folder::read with hidden files unless the tmp folder is writable.');
$Folder = new Folder(TMP . 'folder_tree_hidden', true, 0777);
mkdir($Folder->path . DS . '.svn');
mkdir($Folder->path . DS . 'some_folder');
touch($Folder->path . DS . 'not_hidden.txt');
touch($Folder->path . DS . '.hidden.txt');
$expected = array(
array('some_folder'),
array('not_hidden.txt'),
);
$result = $Folder->read(true, true);
$this->assertEquals($expected, $result);
$expected = array(
array(
'.svn',
'some_folder'
),
array(
'.hidden.txt',
'not_hidden.txt'
),
);
$result = $Folder->read(true);
$this->assertEquals($expected, $result);
}
/**
* testFolderTree method
*
@ -417,41 +452,47 @@ class FolderTest extends CakeTestCase {
* @return void
*/
public function testFolderTreeWithHiddenFiles() {
$this->skipIf(!is_writeable(TMP), 'Cant test Folder::tree with hidden files unless the tmp folder is writable.');
$this->skipIf(!is_writeable(TMP), 'Can\'t test Folder::tree with hidden files unless the tmp folder is writable.');
$Folder = new Folder(TMP . 'folder_tree_hidden', true, 0777);
mkdir($Folder->path . DS . '.svn', 0777, true);
touch($Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php');
mkdir($Folder->path . DS . '.svn' . DS . 'inhiddenfolder');
touch($Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php');
touch($Folder->path . DS . 'not_hidden.txt');
touch($Folder->path . DS . '.hidden.txt');
mkdir($Folder->path . DS . 'visible_folder' . DS . '.git', 0777, true);
$expected = array(
array(
$Folder->path,
$Folder->path . DS . 'visible_folder',
),
array(
$Folder->path . DS . 'not_hidden.txt',
),
);
$result = $Folder->tree(null, false);
sort($result[1]);
sort($expected[1]);
$result = $Folder->tree(null, true);
$this->assertEquals($expected, $result);
$expected = array(
array(
$Folder->path,
$Folder->path . DS . 'visible_folder',
$Folder->path . DS . 'visible_folder' . DS . '.git',
$Folder->path . DS . '.svn',
$Folder->path . DS . '.svn' . DS . 'inhiddenfolder',
),
array(
$Folder->path . DS . 'not_hidden.txt',
$Folder->path . DS . '.hidden.txt',
$Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php',
$Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php',
),
);
$result = $Folder->tree(null, true);
$result = $Folder->tree(null, false);
sort($result[1]);
sort($expected[1]);
$this->assertEquals($expected, $result);

View file

@ -49,12 +49,9 @@ class CakeTestSuite extends PHPUnit_Framework_TestSuite {
*/
public function addTestDirectoryRecursive($directory = '.') {
$Folder = new Folder($directory);
$files = $Folder->tree(null, false, 'files');
$files = $Folder->tree(null, true, 'files');
foreach ($files as $file) {
if (strpos($file, DS . '.') !== false) {
continue;
}
$this->addTestFile($file);
}
}

View file

@ -398,22 +398,19 @@ class Folder {
* Returns an array of nested directories and files in each directory
*
* @param string $path the directory path to build the tree from
* @param mixed $exceptions Array of files to exclude, false to exclude dot files.
* @param string $type either file or dir. null returns both files and directories
* @param mixed $exceptions Either an array of files/folder to exclude
* or boolean true to not grab dot files/folders
* @param string $type either 'file' or 'dir'. null returns both files and directories
* @return mixed array of nested directories and files in each directory
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::tree
*/
public function tree($path = null, $exceptions = true, $type = null) {
public function tree($path = null, $exceptions = false, $type = null) {
if ($path == null) {
$path = $this->path;
}
$files = array();
$directories = array($path);
$skipHidden = false;
if ($exceptions === false) {
$skipHidden = true;
}
if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
if (isset($exceptions['.'])) {
@ -421,6 +418,13 @@ class Folder {
unset($exceptions['.']);
}
}
$skipHidden = false;
if ($exceptions === true) {
$skipHidden = true;
} elseif (isset($exceptions['.'])) {
$skipHidden = true;
unset($exceptions['.']);
}
try {
$directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME | RecursiveDirectoryIterator::CURRENT_AS_SELF | RecursiveDirectoryIterator::SKIP_DOTS);