From 6719f70d6f85f4af09a99b179c6367442e6bfe63 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 17 Dec 2006 19:30:33 +0000 Subject: [PATCH] Rewrote BSFunctions::ScanDirectory() to use a helper method and PHP's DirectoryIterator --- Functions.php | 69 +++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/Functions.php b/Functions.php index a6f9b42..4d03d06 100644 --- a/Functions.php +++ b/Functions.php @@ -559,50 +559,49 @@ class BSFunctions * @param string Path to scan * @param bool Whether or not to recursively scan the directories encountered * @param bool Ignore files beginning with a dot - * @param bool Ignore 'CVS' dirctories * * @return array A list of all the files in the specified path */ - public static function ScanDirectory($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1) + public static function ScanDirectory($path, $recurse = true, $ignoreDot = true) { - static $filelist; - - if ($unset) - { - $filelist = array(); - } - - if (substr($path, (strlen($path) - 1), 1) != '/') - { - $path .= '/'; - } + return self::_helpScanDirectory($path, $recurse, $ignoreDot, ''); + } + + // ################################################################### + /** + * Scans a specified directory path and returns an array of all the + * items in that directory. Directories found by this are end in a "/" + * + * @param string Path to scan + * @param bool Whether or not to recursively scan the directories encountered + * @param bool Ignore files beginning with a dot + * @param string Add to the beginning of the path + * + * @return array A list of all the files in the specified path + */ + private static function _helpScanDirectory($path, $recurse = true, $ignoreDot = true, $pathAdd = '') + { + $filelist = array(); + $path = self::FetchSourcePath($path); - if ($handle = opendir($path)) + $dir = new DirectoryIterator($path); + foreach ($dir AS $file) { - while (($file = readdir($handle)) !== false) + $name = $file->getFilename(); + if (($file->isDot() OR $name[0] == '.') AND $ignoreDot) { - $isdot = ((substr($file, 0, 1) == '.') ? true : false); - $isdot = (($ignoredot) ? $isdot : true); - $iscvs = (($file == 'CVS') ? true : false); - $iscvs = (($ignorecvs) ? $iscvs : true); - if (!$isdot AND !$iscvs) - { - if (is_dir($path . $file)) - { - $filelist["$basepath"][] = "$file/"; - if ($recurse) - { - self::ScanDirectory("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0); - } - } - else - { - $filelist["$basepath"][] = $file; - } - } + continue; + } + + if ($file->isDir() AND $recurse) + { + $filelist = array_merge($filelist, self::_helpScanDirectory($path . $name, $recurse, $ignoreDot, $pathAdd . BSFunctions::FetchSourcePath(str_replace($path, '', $file->getPathname())))); + continue; } - closedir($handle); + + $filelist[] = $pathAdd . $name; } + return $filelist; } -- 2.43.5