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