]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiCallback.php
Whitespace only
[SourceForge/phpwiki.git] / lib / WikiCallback.php
1 <?php
2
3 /**
4  * A callback
5  *
6  * This is a virtual class.
7  *
8  * Subclases of WikiCallback can be used to represent either
9  * global function callbacks, or object method callbacks.
10  *
11  * @see WikiFunctionCb, WikiMethodCb.
12  */
13 class WikiCallback
14 {
15     /**
16      * Convert from Pear-style callback specification to a WikiCallback.
17      *
18      * This is a static member function.
19      *
20      * @param $pearCb mixed
21      * For a global function callback, $pearCb should be a string containing
22      * the name of the function.
23      * For an object method callback, $pearCb should be a array of length two:
24      * the first element should contain (a reference to) the object, the second
25      * element should be a string containing the name of the method to be invoked.
26      * @return object Returns the appropriate subclass of WikiCallback.
27      * @access public
28      */
29     function callback ($pearCb) {
30         if (is_string($pearCb))
31             return new WikiFunctionCb($pearCb);
32         else if (is_array($pearCb)) {
33             list($object, $method) = $handler;
34             return new WikiMethodCb($object, $method);
35         }
36         trigger_error("WikiCallback::new: bad arg", E_USER_ERROR);
37     }
38
39     /**
40      * Call callback.
41      *
42      * @param ? mixed This method takes a variable number of arguments (zero or more).
43      * The callback function is called with the specified arguments.
44      * @return mixed The return value of the callback.
45      * @access public
46      */
47     function call () {
48         return $this->call_array(func_get_args());
49     }
50
51     /**
52      * Call callback (with args in array).
53      *
54      * @param $args array Contains the arguments to be passed to the callback.
55      * @return mixed The return value of the callback.
56      * @see call_user_func_array.
57      * @access public
58      */
59     function call_array ($args) {
60         trigger_error('pure virtual', E_USER_ERROR);
61     }
62
63     /**
64      * Convert to Pear callback.
65      *
66      * @return string The name of the callback function.
67      *  (This value is suitable for passing as the callback parameter
68      *   to a number of different Pear functions and methods.)
69      * @access public
70      */
71     function toPearCb() {
72         trigger_error('pure virtual', E_USER_ERROR);
73     }
74 }
75
76 /**
77  * Global function callback.
78  */
79 class WikiFunctionCb
80     extends WikiCallback
81 {
82     /**
83      * Constructor
84      *
85      * @param $functionName string Name of global function to call.
86      * @access public
87      */
88     function WikiFunctionCb ($functionName) {
89         $this->functionName = $functionName;
90     }
91
92     function call_array ($args) {
93         return call_user_func_array($this->functionName, $args);
94     }
95
96     function toPearCb() {
97         return $this->functionName;
98     }
99 }
100
101 /**
102  * Object Method Callback.
103  */
104 class WikiMethodCb
105     extends WikiCallback
106 {
107     /**
108      * Constructor
109      *
110      * @param $object object Object on which to invoke method.
111      * @param $methodName string Name of method to call.
112      * @access public
113      */
114     function WikiMethodCb(&$object, $methodName) {
115         $this->object = &$object;
116         $this->methodName = $methodName;
117     }
118
119     function call_array ($args) {
120         $method = &$this->methodName;
121         return call_user_func_array(array(&$this->object, $method), $args);
122     }
123
124     function toPearCb() {
125         return array($this->object, $this->methodName);
126     }
127 }
128
129 /**
130  * Anonymous function callback.
131  */
132 class WikiAnonymousCb
133     extends WikiCallback
134 {
135     /**
136      * Constructor
137      *
138      * @param $args string Argument declarations
139      * @param $code string Function body
140      * @see create_function().
141      * @access public
142      */
143     function WikiAnonymousCb ($args, $code) {
144         $this->function = create_function($args, $code);
145     }
146
147     function call_array ($args) {
148         return call_user_func_array($this->function, $args);
149     }
150
151     function toPearCb() {
152         trigger_error("Can't convert WikiAnonymousCb to Pear callback",
153                       E_USER_ERROR);
154     }
155 }
156
157 // Local Variables:
158 // mode: php
159 // tab-width: 8
160 // c-basic-offset: 4
161 // c-hanging-comment-ender-p: nil
162 // indent-tabs-mode: nil
163 // End: