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

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