Created
April 8, 2020 22:41
-
-
Save jeremeamia/bbda0445232a4f4af78c3e48944e1a2e to your computer and use it in GitHub Desktop.
Recursive Glob implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function glob_recursive(string $dir, string $pattern, int $flags = GLOB_BRACE): Iterator | |
{ | |
$path = realpath($dir); | |
if ($path === false) { | |
throw new RuntimeException("Invalid dir: {$dir}"); | |
} | |
$glob = function (string $path) use ($pattern, $flags, &$glob) { | |
yield from glob("{$path}/{$pattern}", $flags); | |
foreach (new DirectoryIterator($path) as $file) { | |
if ($file->isDir() && !$file->isDot()) { | |
yield from $glob($file->getPathname()); | |
} | |
} | |
}; | |
$cwd = getcwd(); | |
chdir($path); | |
yield from $glob($path); | |
chdir($cwd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment