/[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.5 - (hide annotations)
Fri Mar 28 03:05:20 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.4: +7 -4 lines
minor update: fixed labels, modified debugging level

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

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