1 |
joko |
1.1 |
<?php |
2 |
|
|
|
3 |
|
|
/** |
4 |
|
|
* This file contains the Exporter. |
5 |
|
|
* |
6 |
|
|
* @author Andreas Motl <andreas.motl@ilo.de> |
7 |
|
|
* @package org.netfrag.glib |
8 |
|
|
* @name Exporter |
9 |
|
|
* |
10 |
|
|
*/ |
11 |
|
|
|
12 |
|
|
/** |
13 |
joko |
1.2 |
* $Id: Exporter.php,v 1.1 2003/04/05 19:53:45 joko Exp $ |
14 |
joko |
1.1 |
* |
15 |
|
|
* $Log: Exporter.php,v $ |
16 |
joko |
1.2 |
* Revision 1.1 2003/04/05 19:53:45 joko |
17 |
|
|
* moved here from ../ |
18 |
|
|
* |
19 |
joko |
1.1 |
* Revision 1.6 2003/04/04 17:33:49 joko |
20 |
|
|
* updated/enhanced/fixed behaviour of 'function create_stub_function' |
21 |
|
|
* |
22 |
|
|
* Revision 1.5 2003/03/28 03:05:20 joko |
23 |
|
|
* minor update: fixed labels, modified debugging level |
24 |
|
|
* |
25 |
|
|
* Revision 1.4 2003/03/10 22:58:45 joko |
26 |
|
|
* + fixed metadata for phpDocumentor |
27 |
|
|
* |
28 |
|
|
* Revision 1.3 2003/03/05 23:16:46 joko |
29 |
|
|
* updated docu - phpDocumentor is very strict about its 'blocks'... |
30 |
|
|
* |
31 |
|
|
* Revision 1.2 2003/03/05 18:54:41 joko |
32 |
|
|
* updated docu - phpDocumentor is very strict about its 'blocks'... |
33 |
|
|
* |
34 |
|
|
* Revision 1.1 2003/03/03 21:08:21 joko |
35 |
|
|
* refactored from flib/utils |
36 |
|
|
* |
37 |
|
|
* |
38 |
|
|
*/ |
39 |
|
|
|
40 |
|
|
/** |
41 |
|
|
* --- shortcut functions |
42 |
|
|
* |
43 |
|
|
*/ |
44 |
|
|
|
45 |
|
|
//function &export_symbol() { return Exporter::export_symbol(func_get_args()); } |
46 |
|
|
|
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* The Exporter |
50 |
|
|
* |
51 |
|
|
* This tries to mimic a very small subset of the |
52 |
|
|
* functionality of CPAN's Exporter. |
53 |
|
|
* |
54 |
|
|
* <pre> |
55 |
|
|
* x export_symbol |
56 |
|
|
* x export_symbols |
57 |
|
|
* |
58 |
|
|
* Synopsis: |
59 |
|
|
* <code> |
60 |
|
|
* define('EXPORT_OK', 'loadModule mkInstance' ); |
61 |
|
|
* require_once("php_extensions.php"); |
62 |
|
|
* </code> |
63 |
|
|
* |
64 |
|
|
* Example output: |
65 |
|
|
* |
66 |
|
|
* Exporting symbol 'php::loadModule' to 'global::loadModule' (type='__php_function'). |
67 |
|
|
* selected stub: |
68 |
|
|
* <code> |
69 |
|
|
* function &loadModule() { return php::loadModule(Exporter::wrap_args(func_get_args())); } |
70 |
|
|
* </code> |
71 |
|
|
* |
72 |
|
|
* Exporting symbol 'php::mkInstance' to 'global::mkInstance' (type='__php_function'). |
73 |
|
|
* selected stub: |
74 |
|
|
* <code> |
75 |
|
|
* function &mkInstance() { return php::mkInstance(Exporter::wrap_args(func_get_args())); } |
76 |
|
|
* </code> |
77 |
|
|
* </pre> |
78 |
|
|
* |
79 |
|
|
* |
80 |
|
|
* @author Andreas Motl <andreas.motl@ilo.de> |
81 |
|
|
* @copyright (c) 2003 - All Rights reserved. |
82 |
|
|
* @license GNU LGPL (GNU Lesser General Public License) |
83 |
|
|
* |
84 |
|
|
* @link http://www.netfrag.org/~joko/ |
85 |
|
|
* @link http://www.gnu.org/licenses/lgpl.txt |
86 |
|
|
* |
87 |
|
|
* @package org.netfrag.glib |
88 |
|
|
* @name Exporter |
89 |
|
|
* |
90 |
|
|
* @link http://search.cpan.org/author/JHI/perl-5.8.0/lib/Exporter.pm |
91 |
|
|
* |
92 |
|
|
* @todo (x) look at http://www.php.net/manual/en/function.method-exists.php |
93 |
|
|
* @todo (x) $bool_exists = (in_array(strtolower($methodName), get_class_methods($className))); |
94 |
|
|
* @todo (x) -> implemented in php::class_has_method |
95 |
|
|
* |
96 |
|
|
*/ |
97 |
|
|
class Exporter { |
98 |
|
|
|
99 |
|
|
function export_symbol($from = null, $symbol = null, $to = array()) { |
100 |
|
|
|
101 |
|
|
// 1. resolve source |
102 |
|
|
|
103 |
|
|
// --- object |
104 |
|
|
if (is_object($from)) { |
105 |
|
|
$source_type = 'object'; |
106 |
|
|
$source_pack = get_class($from); |
107 |
|
|
|
108 |
|
|
// --- array/hash |
109 |
|
|
} elseif (is_array($from)) { |
110 |
|
|
if (is_hash($from)) { |
111 |
|
|
$source_type = 'hash'; |
112 |
|
|
$error = 1; |
113 |
|
|
} else { |
114 |
|
|
$source_type = 'array'; |
115 |
|
|
$error = 1; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
// --- namespace identifier (e.g. Data::Driver::RPC) or ... |
119 |
|
|
} elseif (is_string($from) && strstr($from, '::')) { |
120 |
|
|
$source_type = 'namespace'; |
121 |
|
|
$error = 1; |
122 |
|
|
|
123 |
|
|
// --- ... plain php classname |
124 |
|
|
} elseif (is_string($from)) { |
125 |
|
|
$source_type = '__php_function'; |
126 |
|
|
$source_pack = $from; |
127 |
joko |
1.2 |
$target_symbol = $to['php_function']; |
128 |
joko |
1.1 |
|
129 |
|
|
// --- unknown |
130 |
|
|
} else { |
131 |
|
|
user_error("Exporter could not handle this: " . Dumper($this)); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
// 2. resolve target |
136 |
|
|
|
137 |
|
|
if (!$target_symbol) { $target_symbol = $symbol; } |
138 |
|
|
if (!$target_pack) { $target_pack = '[global]'; } |
139 |
|
|
|
140 |
|
|
|
141 |
|
|
// 3. resolve target symbol |
142 |
|
|
if (php::is_hash($target_symbol)) { |
143 |
|
|
//print "IS_ARRAY: " . Dumper($target_symbol); |
144 |
|
|
//$symbol = $target_symbol[1]; |
145 |
|
|
//$target_symbol = $target_symbol[0]; |
146 |
|
|
foreach ($target_symbol as $sym_src => $sym_tgt) { |
147 |
|
|
Exporter::export_symbol($from, $sym_src, array( php_function => $sym_tgt ) ); |
148 |
|
|
} |
149 |
|
|
return 1; |
150 |
|
|
|
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
// debug |
154 |
|
|
$msg = "Exporter: exporting '$source_type' symbol '$source_pack::$symbol' to '$target_pack::$target_symbol'."; |
155 |
|
|
if (DEBUG_EXPORTER == 1) { |
156 |
|
|
print "$msg<br/>"; |
157 |
|
|
} |
158 |
|
|
if (LOG_EXPORTER == 1) { |
159 |
|
|
php::append_log($msg); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
// catch errors |
163 |
|
|
if ($error) { |
164 |
|
|
user_error(Dumper($this)); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
|
168 |
|
|
// dispatch symbol export type (mode) |
169 |
|
|
switch ($source_type) { |
170 |
|
|
|
171 |
|
|
case 'object': |
172 |
|
|
if (method_exists($this, $symbol)) { |
173 |
|
|
user_error("object - method exists!"); |
174 |
|
|
} |
175 |
|
|
break; |
176 |
|
|
|
177 |
|
|
case '__php_function': |
178 |
|
|
if (!Exporter::create_stub_function($target_symbol, $source_pack, $symbol)) { |
179 |
|
|
user_error("Exporter error: creating stub function for '$symbol' failed."); |
180 |
|
|
return; |
181 |
|
|
} |
182 |
|
|
break; |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
return 1; |
186 |
|
|
|
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
function export_symbols($from = null, $symbols = array(), $to = array()) { |
190 |
|
|
|
191 |
|
|
php::array_cast_safe($symbols); |
192 |
|
|
|
193 |
|
|
foreach ($symbols as $symbol) { |
194 |
|
|
/* |
195 |
|
|
// if no 'to' argument was given, assume 'php_function' which results in a 1:1 function/method mapping |
196 |
|
|
if (!sizeof($to)) { |
197 |
|
|
$to[php_function] = $symbol; |
198 |
|
|
} |
199 |
|
|
*/ |
200 |
|
|
|
201 |
|
|
//print Dumper($symbol); |
202 |
|
|
Exporter::export_symbol($from, $symbol, $to); |
203 |
|
|
|
204 |
|
|
} |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
function create_stub_function($target_name, $source_class, $source_name) { |
208 |
|
|
|
209 |
|
|
static $target_cache; |
210 |
joko |
1.2 |
if (!isset($target_cache)) { $target_cache = array(); } |
211 |
joko |
1.1 |
|
212 |
|
|
if (!php::class_has_method($source_class, $source_name)) { |
213 |
|
|
user_error("Exporter::create_stub_function - Error: class '$source_class' does not implement method '$source_name', will skip exporting this symbol."); |
214 |
|
|
return; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
// prevent redeclarations |
218 |
joko |
1.2 |
if (array_key_exists($target_name, $target_cache)) { |
219 |
joko |
1.1 |
$msg = "Exporter::create_stub_function - Warning: already declared: $target_name"; |
220 |
|
|
php::append_log($msg, PEAR_LOG_DEBUG); |
221 |
|
|
return 1; |
222 |
|
|
} else { |
223 |
joko |
1.2 |
$target_cache[$target_name] = 1; |
224 |
joko |
1.1 |
} |
225 |
|
|
|
226 |
|
|
//function &$target_name() { return $source_class::$source_name(Exporter::wrap_args(func_get_args())); } |
227 |
|
|
//function &$target_name() { return $source_class::$source_name(php::array_shrink(func_get_args())); } |
228 |
|
|
|
229 |
|
|
//function &$target_name() { \$args = func_get_args(); return call_user_func_array('$source_class::$source_name', &\$args); } |
230 |
|
|
//function &$target_name() { \$args = func_get_args(); return call_user_func_array(array($source_class, $source_name), &\$args); } |
231 |
|
|
//function &$target_name() { \$args = php::array_shrink(func_get_args()); return call_user_func_array(array($source_class, $source_name), &\$args); } |
232 |
|
|
//function &$target_name() { \$args = Exporter::wrap_args(func_get_args()); return call_user_func_array(array($source_class, $source_name), &\$args); } |
233 |
|
|
|
234 |
|
|
// taha that's it! |
235 |
|
|
$stub = |
236 |
|
|
<<<TPL_FUNC |
237 |
|
|
function &$target_name() { |
238 |
|
|
\$passthru = php::array_shrink(func_get_args()); |
239 |
|
|
if (is_array(\$passthru)) { |
240 |
|
|
return call_user_func_array(array('$source_class', '$source_name'), &\$passthru); |
241 |
|
|
} else { |
242 |
|
|
return call_user_func(array('$source_class', '$source_name'), &\$passthru); |
243 |
|
|
} |
244 |
|
|
} |
245 |
|
|
// signal good |
246 |
|
|
return 1; |
247 |
|
|
TPL_FUNC; |
248 |
|
|
|
249 |
|
|
/* |
250 |
|
|
// possible alternative? not yet... |
251 |
|
|
$function = <<<TPL_FUNC |
252 |
|
|
class $target_class() { |
253 |
|
|
function &$target_name() { return $source_class::$source_name(func_get_args()); } |
254 |
|
|
} |
255 |
|
|
TPL_FUNC; |
256 |
|
|
*/ |
257 |
|
|
|
258 |
|
|
// debug |
259 |
|
|
//print "selected stub: <br/>$stub<br/>"; |
260 |
|
|
|
261 |
|
|
// V1 |
262 |
|
|
//eval($stub); |
263 |
|
|
//return 1; |
264 |
|
|
|
265 |
|
|
// V2 |
266 |
|
|
return eval($stub); |
267 |
|
|
|
268 |
|
|
} |
269 |
|
|
|
270 |
|
|
// dives to level into a numeric array since php wraps the args into |
271 |
|
|
// one each time by using 'func_get_args', we do this twice: |
272 |
|
|
// - 1. inside the stub, which calls ... |
273 |
|
|
// - 2. ... the argument wrapper: this stuff here... |
274 |
|
|
function &wrap_args() { |
275 |
|
|
$args = func_get_args(); |
276 |
|
|
//print Dumper($args); |
277 |
|
|
// shrink array to single item? |
278 |
|
|
//if (sizeof($args) == 1) { $args = $args[0]; } |
279 |
|
|
// again? (remember: twice!) |
280 |
|
|
//if (sizeof($args) == 1) { $args = $args[0]; } |
281 |
|
|
$args = $args[0][0]; |
282 |
|
|
//print Dumper($args); |
283 |
|
|
return $args; |
284 |
|
|
} |
285 |
|
|
|
286 |
|
|
} |
287 |
|
|
|
288 |
|
|
?> |