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

Contents of /nfo/php/libs/org.netfrag.glib/DataSource/Locator.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.20 - (show annotations)
Tue Mar 11 03:17:37 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.19: +11 -7 lines
+ fixed metadata for phpDocumentor

1 <?php
2 /**
3 * This file contains the DataSource::Locator class.
4 *
5 * It acts as a container for DataSource connection
6 * metadata. Think of it as an enhanced dsn.
7 *
8 * It gets used by DataSource::Generic
9 * to be informed about the most important stuff it
10 * has to do.
11 *
12 * @author Andreas Motl <andreas.motl@ilo.de>
13 * @package org.netfrag.glib
14 * @name DataSource::Locator
15 *
16 *
17 */
18
19
20 /**
21 * <b>Cvs-Log:</b>
22 *
23 * <pre>
24 * $Id: Locator.php,v 1.19 2003/03/11 03:05:01 joko Exp $
25 *
26 * $Log: Locator.php,v $
27 * Revision 1.19 2003/03/11 03:05:01 joko
28 * + fixed metadata for phpDocumentor
29 *
30 * Revision 1.18 2003/03/11 02:42:51 joko
31 * + fixed metadata for phpDocumentor
32 *
33 * Revision 1.17 2003/03/11 02:34:55 joko
34 * + fixed metadata for phpDocumentor
35 *
36 * Revision 1.16 2003/03/11 02:28:12 joko
37 * + fixed metadata for phpDocumentor
38 *
39 * Revision 1.15 2003/03/11 02:23:03 joko
40 * + fixed metadata for phpDocumentor
41 *
42 * Revision 1.14 2003/03/11 02:14:24 joko
43 * + fixed metadata for phpDocumentor
44 *
45 * Revision 1.13 2003/03/11 02:04:37 joko
46 * + fixed metadata for phpDocumentor
47 *
48 * Revision 1.12 2003/03/11 01:10:00 joko
49 * + fixed metadata for phpDocumentor
50 *
51 * Revision 1.11 2003/03/11 00:12:49 joko
52 * + fixed metadata for phpDocumentor
53 *
54 * Revision 1.10 2003/03/10 23:25:03 joko
55 * + fixed metadata for phpDocumentor
56 *
57 * Revision 1.9 2003/03/10 22:31:55 joko
58 * + fixed metadata for phpDocumentor
59 *
60 * Revision 1.8 2003/03/09 15:50:36 joko
61 * + additional metadata for Autodia
62 *
63 * Revision 1.7 2003/03/05 17:13:16 joko
64 * updated docu (phpDocumentor testing....)
65 *
66 * Revision 1.6 2003/03/05 16:10:08 joko
67 * updated docu (phpDocumentor testing....)
68 *
69 * Revision 1.5 2003/03/05 15:40:39 joko
70 * updated docu (phpDocumentor testing....)
71 *
72 * Revision 1.4 2003/03/05 15:26:23 joko
73 * updated docu (phpDocumentor testing....)
74 *
75 * Revision 1.3 2003/03/05 15:21:54 joko
76 * updated docu (phpDocumentor testing....)
77 *
78 * Revision 1.2 2003/03/05 15:01:10 joko
79 * updated docu
80 *
81 * Revision 1.1 2003/03/05 12:04:37 joko
82 * + initial commit
83 * </pre>
84 *
85 *
86 */
87
88 /**
89 * It helps DataSource::Generic working in different "operation modes".
90 *
91 * <p>
92 * <b>Modes:</b> (these are proposals, more or less...)
93 * o Pass-Through-Reference: php Object will get passed through all layers
94 * o Pass-Through-Memory: reference to a memory area will get used
95 * + Build-Locator: build locator from datasource-type and adapter-type
96 * o Use-Locator: directly use DataSource::Locator instance passed in
97 * o Merge-Locators: merge metadata of two or more DataSource::Locator instances
98 * o Build-AutoLocator: use global constants making up our metadata
99 * </p>
100 *
101 * <b>It can/should contain:</b>
102 * (flexible, just some parameters are required for each operation mode)
103 * o an oldschool "dsn"-string (e.g. for rdbms-connection via PEAR)
104 * + (x) name of a Proxy module to use to *wrap/hide* the connection/transport-layer
105 * (e.g. DataSource::Proxy::XMLRPC via DesignPattern::RemoteProxy)
106 * + (x) metadata (a hash) directly describing *where* to connect to (e.g. 'Host', 'Port')
107 * o an instance of an already instantiated arbitrary datasource handler (e.g. 'source')
108 * this will get propagated (pass-through-mode)
109 * o a datasource-type (of 'rpc|mysql|csv-file|xml-file')
110 * this will be mapped to a module name and used as a *Proxy*
111 * by the DataSource::Generic at runtime
112 * + (x) an adapter-type (of 'phpHtmlLib|pear')
113 * this will be mapped to a module name and used as an *Adapter*
114 * by the DataSource::Generic at runtime
115 * o names of global constants where to find these informations
116 * o datasource-type ('rpc|csv-file|...') <-> datasource-family ('orm|rdbms|odbms')
117 *
118 * <b>It can do:</b>
119 * - builds a larger locator from a minimum of information passed in via constructor-arguments
120 * - direct fallback mode to some predefined constant names if locator is empty and above method fails
121 * - direct fallback mode to some predefined values if just *everything* fails
122 *
123 * <b>How to use?</b>
124 *
125 * Pass an array holding "locator metadata" to the constructor.
126 * This module takes care of the rest.
127 * Pass an array to the constructor: (e.g.)
128 *
129 * 1. for doing rpc-calls....
130 * <code>
131 * $locator = array(
132 * datasource_type => 'rpc',
133 * [adapter_type => 'phpHtmlLib',]
134 * metadata => array( Host => 'localhost', Port => '8765' ),
135 * );
136 * $source = ne w DataSource::Generic($locator);
137 * $this->set_data_source( &$source );
138 * </code>
139 *
140 * 2. [proposal] for common/oldschool datahandles....
141 * <code>
142 * $locator = array(
143 * dsn => 'known dsn markup',
144 * );
145 * $source = ne w DataSource::Generic($locator);
146 * $this->set_data_source( &$source );
147 * </code>
148 *
149 *
150 *
151 * @author Andreas Motl <andreas.motl@ilo.de>
152 * @link http://www.netfrag.org/~joko/
153 * @copyright (c) 2003 - All Rights reserved.
154 *
155 * @license GNU LGPL (GNU Lesser General Public License)
156 * @link http://www.gnu.org/licenses/lgpl.txt
157 *
158 * @package org.netfrag.glib
159 * @subpackage DataSource
160 * @name DataSource::Locator
161 *
162 */
163 class DataSource_Locator {
164
165 /**
166 * This var holds the locator metadata hash,
167 *
168 * which is built from some predefined rules
169 * using metadata from $_options and some
170 * other presets.
171 *
172 * See {@link build}, which acts as a dispatcher
173 * depending on $_options[datasource]. <br>
174 * (main dispatching level)
175 *
176 * The structure of a full blown locator looks like this:
177 *
178 * <code><pre>
179 * $locator = array(
180 * type => '<your type specifying the datasource-type>',
181 * metadata => array(
182 * ... your arbitrary deep metadata structure ...
183 * ),
184 * [dsn => '<your dsn markup>'],
185 * );
186 * </pre></code>
187 *
188 * Example 1 - data is inside a rdbms, using a dsn to connect to it:
189 * <pre><code>
190 * $locator = array(
191 * dsn => 'mysql://username:password@localhost/database',
192 * );
193 * </code></pre>
194 *
195 * Example 2 - data is inside an odbms, reachable by doing remote procedure calls (rpc):
196 * <code>
197 * $locator = array(
198 * type => 'rpc',
199 * metadata => array(
200 * package => 'Data::Driver::Proxy',
201 * Host => 'localhost',
202 * Port => '8765',
203 * )
204 * );
205 * </code>
206 *
207 * @deprecated
208 *
209 */
210 var $_locator_metadata = NULL;
211
212
213 /**
214 * This var holds the arguments passed in to the constructor.
215 * We will try to build full blown locator metadata information from that.
216 *
217 * @deprecated
218 *
219 */
220 var $_in = NULL;
221
222 /**
223 * This var holds the locator metadata informations inside
224 * a single hash. This is returned from '->get()'.
225 *
226 * @deprecated
227 *
228 */
229 var $_out = NULL;
230
231
232 /**
233 * Locator metadata.
234 *
235 */
236
237 // name of a Proxy module
238 var $_datasource_type;
239
240 // name of an Adapter module
241 var $_adapter_type;
242
243 // additional information required (passed on to the Proxy by DataSource::Generic)
244 var $_metadata = array();
245
246 //var $_dsn;
247
248
249 /**
250 * The constructor is used to pass in the
251 * locator metadata hash.
252 *
253 * @param object(DataSource::Locator)|hash optional: a &$locator instance
254 */
255 function DataSource_Locator() {
256 $args_multi = func_get_args();
257
258 //php::array_shrink($args_multi);
259
260 //print Dumper($args_multi);
261 //exit;
262
263 foreach ($args_multi as $args) {
264 if (isset($args)) {
265 $this->merge_args($args);
266 }
267 }
268
269 $this->build();
270 //$this->check();
271 }
272
273 function merge_args(&$args) {
274
275 //print "args: " . Dumper($args);
276
277 // check if passed in locator is already of type *me* ...
278 // ... if so, merge it into current instance
279 if (is_object($args) && get_class($args) == get_class($this)) {
280 $this->merge_to($this, $args);
281
282 } else {
283
284 // FIXME: handle locator merging here?
285 //$this->_arguments = &$args;
286
287 // FIXME: to php::merge_to($this, $locator) here!!!
288 //$this->_data
289 // for now: assume plain hash, so just iterate and overwrite!
290 $this->merge_to($this, $args, '_');
291
292 }
293 }
294
295 function merge_to(&$target, $source, $prefix_key = '') {
296
297 //print "target: " . Dumper($target);
298 //print "source: " . Dumper($source);
299
300 // pre-flight checks
301 if (!isset($target)) {
302 user_error("variable passed as reference (merge target) is not set.");
303 return;
304 }
305 //if (!is_hash($hash)) {
306 if (!isset($source)) {
307 user_error("variable passed as source is not set.");
308 return;
309 }
310
311 // perform merge
312 foreach ($source as $key => $val) {
313 $name = $prefix_key . $key;
314 if (is_object($target)) {
315 $target->$name = $val;
316 //} elseif (php::is_hash($target)) {
317 } else {
318 $target[$name] = $val;
319 }
320 }
321
322 // indicate good
323 return 1;
324
325 }
326
327 /**
328 * Set the locator metadata hash we will feed *partly*
329 * to an encapsulated Data::Driver::Proxy instance
330 *
331 * @param LocatorMetadataHash array -
332 *
333 */
334 function set( &$locator ) {
335 $this->_out = &$locator;
336 }
337
338 function &get_metadata() {
339 //return ($this->_out);
340 return $this->_metadata;
341 }
342
343
344 function build( ) {
345
346 // trace
347 //print Dumper($this);
348
349 $good = 0;
350 switch ($this->_datasource_type) {
351 case 'rpc':
352 // get rpc connection information from global constants
353 $metadata = array(
354 //package => 'Data::Driver::Proxy',
355 //package => 'DataSource::Proxy::XMLRPC',
356 //package => 'DataSource::Generic',
357 //package => 'DesignPattern::RemoteProxy',
358 Host => RPC_HOSTNAME,
359 Port => RPC_PORT,
360 );
361 $good = 1;
362 break;
363 }
364
365 if ($good) {
366
367 // V1:
368 /*
369 $this->_out = array(
370 datasource_type => $datasource,
371 adapter_type => 'Data',
372 metadata => $metadata,
373 );
374 */
375
376 // V2:
377 //$this->_out = $metadata;
378
379 // V3:
380 $this->merge_to($this->_metadata, $metadata);
381
382 }
383
384 // trace
385 //print Dumper($this);
386
387 }
388
389 function check() {
390 if (!sizeof($this->_metadata)) {
391 user_error("DataSource::Locator::check() failed: " . "Locator metadata not established, please check configuration options.");
392 return;
393 }
394 return 1;
395 }
396
397
398 }
399
400 ?>

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