/[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.2 - (hide annotations)
Wed Mar 5 18:54:41 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.1: +33 -32 lines
updated docu - phpDocumentor is very strict about its 'blocks'...

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

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