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