Skip to content

Instantly share code, notes, and snippets.

@rockschtar
Created March 7, 2022 08:15
Show Gist options
  • Save rockschtar/84a52d79ffd7224089388a7a472e691e to your computer and use it in GitHub Desktop.
Save rockschtar/84a52d79ffd7224089388a7a472e691e to your computer and use it in GitHub Desktop.
rglob
private function rglob($pattern, $flags = 0)
{
if (strpos($pattern, '**') === false) {
$files = glob($pattern, $flags);
} else {
$position = strpos($pattern, '**');
$rootPattern = substr($pattern, 0, $position - 1);
$restPattern = substr($pattern, $position + 2);
$patterns = array($rootPattern . $restPattern);
$rootPattern .= '/*';
while ($dirs = glob($rootPattern, GLOB_ONLYDIR)) {
$rootPattern .= '/*';
foreach ($dirs as $dir) {
$patterns[] = $dir . $restPattern;
}
}
foreach ($patterns as $pat) {
$filesToMerge[] = $this->rglob($pat, $flags);
}
$files = array_merge([], ...$filesToMerge);
}
$files = array_unique($files);
sort($files);
return $files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment