Rewrote BSFunctions::ScanDirectory() to use a helper method and PHP's DirectoryIterator
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 17 Dec 2006 19:30:33 +0000 (19:30 +0000)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 17 Dec 2006 19:30:33 +0000 (19:30 +0000)
Functions.php

index a6f9b4252ffcf607104eed49ac68ee20f0e85c00..4d03d062d16e5a20d667c8721f97a50390c9b250 100644 (file)
@@ -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;
        }