Created
March 7, 2022 08:15
-
-
Save rockschtar/84a52d79ffd7224089388a7a472e691e to your computer and use it in GitHub Desktop.
rglob
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
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