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