]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiCallback.php
No tabs
[SourceForge/phpwiki.git] / lib / WikiCallback.php
1 <?php //-*-php-*-
2 // $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         return call_user_func_array(array(&$this->object, $method), $args);
123     }
124
125     function toPearCb() {
126         return array($this->object, $this->methodName);
127     }
128 }
129
130 /**
131  * Anonymous function callback.
132  */
133 class WikiAnonymousCb
134     extends WikiCallback
135 {
136     /**
137      * Constructor
138      *
139      * @param $args string Argument declarations
140      * @param $code string Function body
141      * @see create_function().
142      * @access public
143      */
144     function WikiAnonymousCb ($args, $code) {
145         $this->function = create_function($args, $code);
146     }
147
148     function call_array ($args) {
149         return call_user_func_array($this->function, $args);
150     }
151
152     function toPearCb() {
153         trigger_error("Can't convert WikiAnonymousCb to Pear callback",
154                       E_USER_ERROR);
155     }
156 }
157
158 // Local Variables:
159 // mode: php
160 // tab-width: 8
161 // c-basic-offset: 4
162 // c-hanging-comment-ender-p: nil
163 // indent-tabs-mode: nil
164 // End:
165 ?>