Skip to content

Instantly share code, notes, and snippets.

@indeyets
Created April 14, 2011 07:14
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 indeyets/919047 to your computer and use it in GitHub Desktop.
Save indeyets/919047 to your computer and use it in GitHub Desktop.
<?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