summaryrefslogtreecommitdiff
path: root/inc/search.php
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2006-04-28 20:45:54 +0200
committerAndreas Gohr <andi@splitbrain.org>2006-04-28 20:45:54 +0200
commit24baa0451a0f6f2a27f8320713ce87bdaf866b53 (patch)
tree8b1a568540db86b6d9362309b132a994bb2cf5f2 /inc/search.php
parent83730152bf4050753830c82cd64c94eaa4c3507c (diff)
downloadrpg-24baa0451a0f6f2a27f8320713ce87bdaf866b53.tar.gz
rpg-24baa0451a0f6f2a27f8320713ce87bdaf866b53.tar.bz2
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
Diffstat (limited to 'inc/search.php')
-rw-r--r--inc/search.php33
1 files changed, 31 insertions, 2 deletions
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 <andi@splitbrain.org>
*/
function search(&$data,$base,$func,$opts,$dir='',$lvl=1){
@@ -41,17 +46,41 @@ 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 <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
* function above. This function is called for every found file or
* directory. When a directory is given to the function it has to