Skip to content

Instantly share code, notes, and snippets.

@jeremeamia
Created April 8, 2020 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremeamia/bbda0445232a4f4af78c3e48944e1a2e to your computer and use it in GitHub Desktop.
Save jeremeamia/bbda0445232a4f4af78c3e48944e1a2e to your computer and use it in GitHub Desktop.
Recursive Glob implementation
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