File::findFiles - Save 200 million nanoseconds
authorTim Otten <totten@civicrm.org>
Thu, 8 Sep 2022 22:08:29 +0000 (15:08 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 8 Sep 2022 22:08:29 +0000 (15:08 -0700)
CRM/Utils/File.php

index 675709e9e63d05a7e293a923be7ae23e91da373d..977b9fc8368e8d0cf94ee6635f288379a35c4df1 100644 (file)
@@ -766,25 +766,11 @@ HTACCESS;
         }
       }
       // Find subdirs to recurse into.
-      if ($dh = opendir($subdir)) {
-        while (FALSE !== ($entry = readdir($dh))) {
-          $path = $subdir . DIRECTORY_SEPARATOR . $entry;
-          // Exclude . (self) and .. (parent) to avoid infinite loop.
-          // Exclude configured exclude dirs.
-          // Exclude dirs we can't read.
-          // Exclude anything that's not a dir.
-          if (
-            $entry !== '.'
-            && $entry !== '..'
-            && (empty($excludeDirsPattern) || !preg_match($excludeDirsPattern, $path))
-            && is_dir($path)
-            && is_readable($path)
-          ) {
-            $todos[] = $path;
-          }
-        }
-        closedir($dh);
+      $subdirs = glob("$subdir/*", GLOB_ONLYDIR);
+      if (!empty($excludeDirsPattern)) {
+        $subdirs = preg_grep($excludeDirsPattern, $subdirs, PREG_GREP_INVERT);
       }
+      $todos = array_merge($todos, $subdirs);
     }
     return $result;
   }