]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiCallback.php
Allow bold, italics or underlined for numbers
[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     {
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     {
50         return $this->call_array(func_get_args());
51     }
52
53     /**
54      * Call callback (with args in array).
55      *
56      * @param $args array Contains the arguments to be passed to the callback.
57      * @return mixed The return value of the callback.
58      * @see call_user_func_array.
59      * @access public
60      */
61     function call_array($args)
62     {
63         trigger_error('pure virtual', E_USER_ERROR);
64     }
65
66     /**
67      * Convert to Pear callback.
68      *
69      * @return string The name of the callback function.
70      *  (This value is suitable for passing as the callback parameter
71      *   to a number of different Pear functions and methods.)
72      * @access public
73      */
74     function toPearCb()
75     {
76         trigger_error('pure virtual', E_USER_ERROR);
77     }
78 }
79
80 /**
81  * Global function callback.
82  */
83 class WikiFunctionCb
84     extends WikiCallback
85 {
86     /**
87      * Constructor
88      *
89      * @param $functionName string Name of global function to call.
90      * @access public
91      */
92     function WikiFunctionCb($functionName)
93     {
94         $this->functionName = $functionName;
95     }
96
97     function call_array($args)
98     {
99         return call_user_func_array($this->functionName, $args);
100     }
101
102     function toPearCb()
103     {
104         return $this->functionName;
105     }
106 }
107
108 /**
109  * Object Method Callback.
110  */
111 class WikiMethodCb
112     extends WikiCallback
113 {
114     /**
115      * Constructor
116      *
117      * @param $object object Object on which to invoke method.
118      * @param $methodName string Name of method to call.
119      * @access public
120      */
121     function WikiMethodCb(&$object, $methodName)
122     {
123         $this->object = &$object;
124         $this->methodName = $methodName;
125     }
126
127     function call_array($args)
128     {
129         $method = &$this->methodName;
130         return call_user_func_array(array(&$this->object, $method), $args);
131     }
132
133     function toPearCb()
134     {
135         return array($this->object, $this->methodName);
136     }
137 }
138
139 /**
140  * Anonymous function callback.
141  */
142 class WikiAnonymousCb
143     extends WikiCallback
144 {
145     /**
146      * Constructor
147      *
148      * @param $args string Argument declarations
149      * @param $code string Function body
150      * @see create_function().
151      * @access public
152      */
153     function WikiAnonymousCb($args, $code)
154     {
155         $this->function = create_function($args, $code);
156     }
157
158     function call_array($args)
159     {
160         return call_user_func_array($this->function, $args);
161     }
162
163     function toPearCb()
164     {
165         trigger_error("Can't convert WikiAnonymousCb to Pear callback",
166             E_USER_ERROR);
167     }
168 }
169
170 // Local Variables:
171 // mode: php
172 // tab-width: 8
173 // c-basic-offset: 4
174 // c-hanging-comment-ender-p: nil
175 // indent-tabs-mode: nil
176 // End: