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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Mon Mar 10 22:58:45 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.3: +8 -8 lines
+ fixed metadata for phpDocumentor

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.4 * $Id: Exporter.php,v 1.3 2003/03/05 23:16:46 joko Exp $
14 joko 1.1 *
15 joko 1.2 * $Log: Exporter.php,v $
16 joko 1.4 * Revision 1.3 2003/03/05 23:16:46 joko
17     * updated docu - phpDocumentor is very strict about its 'blocks'...
18     *
19 joko 1.3 * Revision 1.2 2003/03/05 18:54:41 joko
20     * updated docu - phpDocumentor is very strict about its 'blocks'...
21     *
22 joko 1.2 * Revision 1.1 2003/03/03 21:08:21 joko
23     * refactored from flib/utils
24     *
25     *
26     */
27    
28     /**
29     * --- shortcut functions
30 joko 1.1 *
31     */
32    
33 joko 1.2 //function &export_symbol() { return Exporter::export_symbol(func_get_args()); }
34    
35 joko 1.1
36     /**
37 joko 1.2 * The Exporter
38 joko 1.1 *
39 joko 1.2 * This tries to mimic a very small subset of the
40     * functionality of CPAN's Exporter.
41     *
42 joko 1.4 * <pre>
43 joko 1.3 * x export_symbol
44 joko 1.1 * x export_symbols
45     *
46     * Synopsis:
47 joko 1.2 * <code>
48 joko 1.1 * define('EXPORT_OK', 'loadModule mkInstance' );
49     * require_once("php_extensions.php");
50 joko 1.2 * </code>
51 joko 1.1 *
52     * Example output:
53     *
54     * Exporting symbol 'php::loadModule' to 'global::loadModule' (type='__php_function').
55     * selected stub:
56 joko 1.2 * <code>
57 joko 1.1 * function &loadModule() { return php::loadModule(Exporter::wrap_args(func_get_args())); }
58 joko 1.2 * </code>
59 joko 1.1 *
60     * Exporting symbol 'php::mkInstance' to 'global::mkInstance' (type='__php_function').
61     * selected stub:
62 joko 1.2 * <code>
63 joko 1.1 * function &mkInstance() { return php::mkInstance(Exporter::wrap_args(func_get_args())); }
64 joko 1.2 * </code>
65     * </pre>
66 joko 1.1 *
67     *
68     * @author Andreas Motl <andreas.motl@ilo.de>
69     * @copyright (c) 2003 - All Rights reserved.
70     * @license GNU LGPL (GNU Lesser General Public License)
71     *
72 joko 1.2 * @link http://www.netfrag.org/~joko/
73     * @link http://www.gnu.org/licenses/lgpl.txt
74 joko 1.1 *
75     * @package org.netfrag.glib
76 joko 1.2 * @name Exporter
77 joko 1.3 *
78     * @link http://search.cpan.org/author/JHI/perl-5.8.0/lib/Exporter.pm
79 joko 1.1 *
80 joko 1.4 * @todo (x) look at http://www.php.net/manual/en/function.method-exists.php
81     * @todo (x) $bool_exists = (in_array(strtolower($methodName), get_class_methods($className)));
82     * @todo (x) -> implemented in php::class_has_method
83 joko 1.1 *
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     ?>

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