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