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