Posted By

toto898 on 12/29/13


Tagged

path glob


Versions (?)

parse glob style path string


 / Published in: PHP
 

URL: https://github.com/apinstein/watchngo/blob/master/watchngo

support "**"

  1. /**
  2. * @param mixed VARGS: any number of strings or arrays, each being a file path or a glob to watch.
  3. * @return array The set of files matches the passed globs.
  4. */
  5. function recursive_glob($requestedGlobs)
  6. {
  7. // expoode vargs
  8. $requestedGlobs = array();
  9. foreach (func_get_args() as $arg) {
  10. if (!is_array($arg))
  11. {
  12. $arg = array($arg);
  13. }
  14. $requestedGlobs = array_merge($requestedGlobs, $arg);
  15. }
  16. $requestedGlobs = array_unique($requestedGlobs);
  17.  
  18. // expand all a/b/**/c/*.foo into globs
  19. $globsToProcess = array();
  20. foreach ($requestedGlobs as $glob) {
  21. $hasRecursiveDirGlob = (strpos($glob, '**') !== false);
  22. if ($hasRecursiveDirGlob)
  23. {
  24. list($baseDir, $subGlob) = explode('**', $glob, 2);
  25. $baseDir = rtrim($baseDir, '/');
  26. $subGlob = ltrim($subGlob, '/');
  27.  
  28. // root glob
  29. $globsToProcess[] = "{$baseDir}/{$subGlob}";
  30.  
  31. $rDirIterator = new RecursiveDirectoryIterator($baseDir);
  32. $itIterator = new RecursiveIteratorIterator($rDirIterator, RecursiveIteratorIterator::CHILD_FIRST);
  33. foreach ($itIterator as $path => $fileInfo) {
  34. if ($itIterator->getInnerIterator()->isDot()) continue;
  35. if ($itIterator->getInnerIterator()->isFile()) continue;
  36.  
  37. $globsToProcess[] = "{$path}/{$subGlob}";
  38. }
  39. }
  40. else
  41. {
  42. $globsToProcess[] = $glob;
  43. }
  44. }
  45.  
  46. $matches = array();
  47. $globsToProcess = array_unique($globsToProcess);
  48. foreach ($globsToProcess as $glob) {
  49. $matches = array_merge($matches, glob($glob));
  50. }
  51.  
  52. $matches = array_unique($matches);
  53. return $matches;
  54. }

Report this snippet  

You need to login to post a comment.