]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiCallback.php
Let us put some abstraction
[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 abstract 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      */
28     public function callback($pearCb)
29     {
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      */
46     public function call()
47     {
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      */
58     abstract public function call_array($args);
59
60     /**
61      * Convert to Pear callback.
62      *
63      * @return string The name of the callback function.
64      *  (This value is suitable for passing as the callback parameter
65      *   to a number of different Pear functions and methods.)
66      */
67     abstract public function toPearCb();
68 }
69
70 /**
71  * Global function callback.
72  */
73 class WikiFunctionCb
74     extends WikiCallback
75 {
76     /**
77      * @param $functionName string Name of global function to call.
78      */
79     public function __construct($functionName)
80     {
81         $this->functionName = $functionName;
82     }
83
84     function call_array($args)
85     {
86         return call_user_func_array($this->functionName, $args);
87     }
88
89     function toPearCb()
90     {
91         return $this->functionName;
92     }
93 }
94
95 /**
96  * Object Method Callback.
97  */
98 class WikiMethodCb
99     extends WikiCallback
100 {
101     /**
102      * @param $object object Object on which to invoke method.
103      * @param $methodName string Name of method to call.
104      */
105     function __construct(&$object, $methodName)
106     {
107         $this->object = &$object;
108         $this->methodName = $methodName;
109     }
110
111     function call_array($args)
112     {
113         $method = &$this->methodName;
114         return call_user_func_array(array(&$this->object, $method), $args);
115     }
116
117     function toPearCb()
118     {
119         return array($this->object, $this->methodName);
120     }
121 }
122
123 /**
124  * Anonymous function callback.
125  */
126 class WikiAnonymousCb
127     extends WikiCallback
128 {
129     /**
130      * @param $args string Argument declarations
131      * @param $code string Function body
132      * @see create_function().
133      */
134     function __construct($args, $code)
135     {
136         $this->function = create_function($args, $code);
137     }
138
139     function call_array($args)
140     {
141         return call_user_func_array($this->function, $args);
142     }
143
144     function toPearCb()
145     {
146         trigger_error("Can't convert WikiAnonymousCb to Pear callback",
147             E_USER_ERROR);
148     }
149 }
150
151 // Local Variables:
152 // mode: php
153 // tab-width: 8
154 // c-basic-offset: 4
155 // c-hanging-comment-ender-p: nil
156 // indent-tabs-mode: nil
157 // End: