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