]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiCallback.php
rcs_id no longer makes sense with Subversion global version number
[SourceForge/phpwiki.git] / lib / WikiCallback.php
1 <?php //-*-php-*-
2 // rcs_id('$Id$');
3
4 /**
5  * A callback
6  *
7  * This is a virtual class.
8  *
9  * Subclases of WikiCallback can be used to represent either
10  * global function callbacks, or object method callbacks.
11  *
12  * @see WikiFunctionCb, WikiMethodCb.
13  */
14 class WikiCallback
15 {
16     /**
17      * Convert from Pear-style callback specification to a WikiCallback.
18      *
19      * This is a static member function.
20      *
21      * @param $pearCb mixed
22      * For a global function callback, $pearCb should be a string containing
23      * the name of the function.
24      * For an object method callback, $pearCb should be a array of length two:
25      * the first element should contain (a reference to) the object, the second
26      * element should be a string containing the name of the method to be invoked.
27      * @return object Returns the appropriate subclass of WikiCallback.
28      * @access public
29      */
30     function callback ($pearCb) {
31         if (is_string($pearCb))
32             return new WikiFunctionCb($pearCb);
33         else if (is_array($pearCb)) {
34             list($object, $method) = $handler;
35             return new WikiMethodCb($object, $method);
36         }
37         trigger_error("WikiCallback::new: bad arg", E_USER_ERROR);
38     }
39     
40     /**
41      * Call callback.
42      *
43      * @param ? mixed This method takes a variable number of arguments (zero or more).
44      * The callback function is called with the specified arguments.
45      * @return mixed The return value of the callback.
46      * @access public
47      */
48     function call () {
49         return $this->call_array(func_get_args());
50     }
51
52     /**
53      * Call callback (with args in array).
54      *
55      * @param $args array Contains the arguments to be passed to the callback.
56      * @return mixed The return value of the callback.
57      * @see call_user_func_array.
58      * @access public
59      */
60     function call_array ($args) {
61         trigger_error('pure virtual', E_USER_ERROR);
62     }
63
64     /**
65      * Convert to Pear callback.
66      *
67      * @return string The name of the callback function.
68      *  (This value is suitable for passing as the callback parameter
69      *   to a number of different Pear functions and methods.) 
70      * @access public
71      */
72     function toPearCb() {
73         trigger_error('pure virtual', E_USER_ERROR);
74     }
75 }
76
77 /**
78  * Global function callback.
79  */
80 class WikiFunctionCb
81     extends WikiCallback
82 {
83     /**
84      * Constructor
85      *
86      * @param $functionName string Name of global function to call.
87      * @access public
88      */
89     function WikiFunctionCb ($functionName) {
90         $this->functionName = $functionName;
91     }
92
93     function call_array ($args) {
94         return call_user_func_array($this->functionName, $args);
95     }
96
97     function toPearCb() {
98         return $this->functionName;
99     }
100 }
101
102 /**
103  * Object Method Callback.
104  */
105 class WikiMethodCb
106     extends WikiCallback
107 {
108     /**
109      * Constructor
110      *
111      * @param $object object Object on which to invoke method.
112      * @param $methodName string Name of method to call.
113      * @access public
114      */
115     function WikiMethodCb(&$object, $methodName) {
116         $this->object = &$object;
117         $this->methodName = $methodName;
118     }
119
120     function call_array ($args) {
121         $method = &$this->methodName;
122         //$obj = &$this->object;
123
124         // This should work, except PHP's before 4.0.5 (which includes mine)
125         // don't have 'call_user_method_array'.
126         if (check_php_version(4,0,5)) {
127             return call_user_func_array(array(&$this->object, $method), $args);
128         }
129
130         // This should work, but doesn't.  At least in my PHP, the object seems
131         // to get passed by value, rather than reference, so any changes to the
132         // object made by the called method get lost.
133         /*
134         switch (count($args)) {
135         case 0: return call_user_method($method, $obj);
136         case 1: return call_user_method($method, $obj, $args[0]);
137         case 2: return call_user_method($method, $obj, $args[0], $args[1]);
138         case 3: return call_user_method($method, $obj, $args[0], $args[1], $args[2]);
139         case 4: return call_user_method($method, $obj, $args[0], $args[1], $args[2], $args[3]);
140         default: trigger_error("Too many arguments to method callback", E_USER_ERROR);
141         }
142         */
143
144         // This seems to work, at least for me (so far):
145         switch (count($args)) {
146         case 0: return $this->object->$method();
147         case 1: return $this->object->$method($args[0]);
148         case 2: return $this->object->$method($args[0], $args[1]);
149         case 3: return $this->object->$method($args[0], $args[1], $args[2]);
150         case 4: return $this->object->$method($args[0], $args[1], $args[2], $args[3]);
151         default: trigger_error("Too many arguments to method callback", E_USER_ERROR);
152         }
153     }
154
155     function toPearCb() {
156         return array($this->object, $this->methodName);
157     }
158 }
159
160 /**
161  * Anonymous function callback.
162  */
163 class WikiAnonymousCb
164     extends WikiCallback
165 {
166     /**
167      * Constructor
168      *
169      * @param $args string Argument declarations
170      * @param $code string Function body
171      * @see create_function().
172      * @access public
173      */
174     function WikiAnonymousCb ($args, $code) {
175         $this->function = create_function($args, $code);
176     }
177
178     function call_array ($args) {
179         return call_user_func_array($this->function, $args);
180     }
181
182     function toPearCb() {
183         trigger_error("Can't convert WikiAnonymousCb to Pear callback",
184                       E_USER_ERROR);
185     }
186 }
187
188 // (c-file-style: "gnu")
189 // Local Variables:
190 // mode: php
191 // tab-width: 8
192 // c-basic-offset: 4
193 // c-hanging-comment-ender-p: nil
194 // indent-tabs-mode: nil
195 // End:   
196 ?>