Created
April 14, 2011 07:14
-
-
Save indeyets/919047 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Mimics ant pattern matching. | |
* New addition (afaict): any pattern ending in '/' will only match directories | |
* @see http://ant.apache.org/manual/dirtasks.html#patterns | |
* @todo more complete testing | |
*/ | |
function pake_antpattern($files, $rootdir) | |
{ | |
$results = array(); | |
foreach ($files as $file) { | |
//echo " Beginning with $file in dir $rootdir\n"; | |
$type = 'any'; | |
// if user set '/ 'as last char: we look for directories only | |
if (substr($file, -1) == '/') { | |
$type = 'dir'; | |
$file = substr($file, 0, -1); | |
} | |
// managing 'any subdir or file' as last item: trick! | |
if (strlen($file) >= 3 and substr($file, -3) == '/**') { | |
$file .= '/*'; | |
} | |
$dir = dirname($file); | |
$file = basename($file); | |
if (strpos($dir, '**') !== false) { | |
$parts = explode('/', $dir); | |
$path = $rootdir; | |
foreach($parts as $i => $part) { | |
if ($part == '**') { | |
//echo " Looking for subdirs in dir {$path}\n"; | |
$tail = array_slice($parts, $i + 1); | |
$newfile = implode('/', $tail).'/'.$file; | |
if ($type == 'dir') | |
$newfile .= '/'; | |
foreach (pakeFinder::type('dir')->in($path) as $newdir) { | |
//echo " Iterating in $newdir, looking for $newfile\n"; | |
$found = pake_antpattern(array($newfile), $newdir); | |
$results = array_merge($results, $found); | |
} | |
break; | |
} | |
$path .= '/'.$part; | |
} | |
} else { | |
//echo " Looking for $type $file in dir $rootdir/$dir\n"; | |
$finder = pakeFinder::type($type)->name($file)->maxdepth(0); | |
$results = array_merge($results, $finder->in($rootdir.'/'.$dir)); | |
} | |
} | |
return $results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment