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