From 24baa0451a0f6f2a27f8320713ce87bdaf866b53 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 28 Apr 2006 20:45:54 +0200 Subject: 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 --- inc/search.php | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'inc/search.php') diff --git a/inc/search.php b/inc/search.php index 254848d4b..5de5a1f47 100644 --- a/inc/search.php +++ b/inc/search.php @@ -15,6 +15,11 @@ * This function recurses into a given base 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 */ 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 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); } } //now handle the files 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 + */ +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 * function above. This function is called for every found file or -- cgit v1.2.3