/[cvs]/nfo/php/libs/org.netfrag.glib/Exporter.php
ViewVC logotype

Contents of /nfo/php/libs/org.netfrag.glib/Exporter.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Sat Apr 5 19:54:29 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +4 -1 lines
FILE REMOVED
moved to utils/

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed