Skip to content
Snippets Groups Projects
Commit 24baa045 authored by Andreas Gohr's avatar Andreas Gohr
Browse files

better callback handling in search() function

The search() function now accepts an array in $func for giving
object methods as callback.

darcs-hash:20060428184554-7ad00-2d1726d078683ea41c72f6ca67ded3e1eccfdf17.gz
parent 83730152
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,11 @@ ...@@ -15,6 +15,11 @@
* This function recurses into a given base directory * This function recurses into a given base directory
* and calls the supplied function for each file and directory * and calls the supplied function for each file and directory
* *
* @param array ref $data The results of the search are stored here
* @param string $base Where to start the search
* @param callback $func Callback (function name or arayy with object,method)
* @param string $dir Current directory beyond $base
* @param int $lvl Recursion Level
* @author Andreas Gohr <andi@splitbrain.org> * @author Andreas Gohr <andi@splitbrain.org>
*/ */
function search(&$data,$base,$func,$opts,$dir='',$lvl=1){ function search(&$data,$base,$func,$opts,$dir='',$lvl=1){
...@@ -41,16 +46,40 @@ function search(&$data,$base,$func,$opts,$dir='',$lvl=1){ ...@@ -41,16 +46,40 @@ function search(&$data,$base,$func,$opts,$dir='',$lvl=1){
//give directories to userfunction then recurse //give directories to userfunction then recurse
foreach($dirs as $dir){ foreach($dirs as $dir){
if ($func($data,$base,$dir,'d',$lvl,$opts)){ if (search_callback($func,$data,$base,$dir,'d',$lvl,$opts)){
search($data,$base,$func,$opts,$dir,$lvl+1); search($data,$base,$func,$opts,$dir,$lvl+1);
} }
} }
//now handle the files //now handle the files
foreach($files as $file){ foreach($files as $file){
$func($data,$base,$file,'f',$lvl,$opts); search_callback($func,$data,$base,$file,'f',$lvl,$opts);
} }
} }
/**
* Used to run the a user callback
*
* Makes sure the $data array is passed by reference (unlike when using
* call_user_func())
*
* @todo If this can be generalized it may be useful elsewhere in the code
* @author Andreas Gohr <andi@splitbrain.org>
*/
function search_callback($func,&$data,$base,$file,$type,$lvl,$opts){
if(is_array($func)){
if(is_object($func[0])){
// instanciated object
return $func[0]->$func[1]($data,$base,$file,$type,$lvl,$opts);
}else{
// static call
$f = $func[0].'::'.$func[1];
return $f($data,$base,$file,$type,$lvl,$opts);
}
}
// simple function call
return $func($data,$base,$file,$type,$lvl,$opts);
}
/** /**
* The following functions are userfunctions to use with the search * The following functions are userfunctions to use with the search
* function above. This function is called for every found file or * function above. This function is called for every found file or
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment