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