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