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