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 |
* |
13 |
* @author Andreas Motl <andreas.motl@ilo.de> |
14 |
* @package org.netfrag.glib |
15 |
* @module DataSource::Locator |
16 |
* |
17 |
*/ |
18 |
|
19 |
/** |
20 |
* $Id: Locator.php,v 1.5 2003/03/05 15:40:39 joko Exp $ |
21 |
* |
22 |
* $Log: Locator.php,v $ |
23 |
* Revision 1.5 2003/03/05 15:40:39 joko |
24 |
* updated docu (phpDocumentor testing....) |
25 |
* |
26 |
* Revision 1.4 2003/03/05 15:26:23 joko |
27 |
* updated docu (phpDocumentor testing....) |
28 |
* |
29 |
* Revision 1.3 2003/03/05 15:21:54 joko |
30 |
* updated docu (phpDocumentor testing....) |
31 |
* |
32 |
* Revision 1.2 2003/03/05 15:01:10 joko |
33 |
* updated docu |
34 |
* |
35 |
* Revision 1.1 2003/03/05 12:04:37 joko |
36 |
* + initial commit |
37 |
* |
38 |
* |
39 |
*/ |
40 |
|
41 |
/** |
42 |
* It helps DataSource::Generic working in different "operation modes". |
43 |
* |
44 |
* - Pass-Through-Reference: php Object will get passed through all layers |
45 |
* - Pass-Through-Memory: reference to a memory area will get used |
46 |
* + Build-Locator: build locator from datasource-type and adapter-type |
47 |
* - 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 |
* |
53 |
* <pre> |
54 |
* (flexible, just some parameters are required for each operation mode) |
55 |
* - an oldschool "dsn"-string (e.g. for rdbms-connection via PEAR) |
56 |
* + 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 |
* - an instance of an already instantiated arbitrary datasource handler (e.g. 'source') |
60 |
* this will get propagated (pass-through-mode) |
61 |
* - a datasource-type (of 'rpc|mysql|csv-file|xml-file') |
62 |
* this will be mapped to a module name and used as a *Proxy* |
63 |
* by the DataSource::Generic at runtime |
64 |
* + an adapter-type (of 'phpHtmlLib|pear') |
65 |
* this will be mapped to a module name and used as an *Adapter* |
66 |
* by the DataSource::Generic at runtime |
67 |
* - names of global constants where to find these informations |
68 |
* - datasource-type ('rpc|csv-file|...') <-> datasource-family ('orm|rdbms|odbms') |
69 |
* </pre> |
70 |
* |
71 |
* |
72 |
* <b>It can do</b> |
73 |
* + 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 |
* |
77 |
* |
78 |
* <b>How to use?</b> |
79 |
* |
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 |
* <code> |
87 |
* $locator = array( |
88 |
* datasource_type => 'rpc', |
89 |
* [adapter_type => 'phpHtmlLib',] |
90 |
* metadata => array( Host => 'localhost', Port => '8765' ), |
91 |
* ); |
92 |
* $source = new DataSource::Generic($locator); |
93 |
* $this->set_data_source( &$source ); |
94 |
* </code> |
95 |
* |
96 |
* 2. [proposal] for common/oldschool datahandles.... |
97 |
* <code> |
98 |
* $locator = array( |
99 |
* dsn => 'known dsn markup', |
100 |
* ); |
101 |
* $source = new DataSource::Generic($locator); |
102 |
* $this->set_data_source( &$source ); |
103 |
* </code> |
104 |
* |
105 |
* |
106 |
* |
107 |
* @author Andreas Motl <andreas.motl@ilo.de> |
108 |
* @link http://www.netfrag.org/~joko/ |
109 |
* @copyright (c) 2003 - All Rights reserved. |
110 |
* |
111 |
* @license GNU LGPL (GNU Lesser General Public License) |
112 |
* @link http://www.gnu.org/licenses/lgpl.txt |
113 |
* |
114 |
* @name DataSource::Locator |
115 |
* @filesource |
116 |
* |
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 |
* @see function build, which acts as a dispatcher |
129 |
* depending on $_options[datasource]. |
130 |
* (main dispatching level) |
131 |
* |
132 |
* The structure of a full blown locator looks like this: |
133 |
* |
134 |
* <code> |
135 |
* $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 |
* </code> |
143 |
* |
144 |
* Example 1 - data is inside a rdbms, using a dsn to connect to it: |
145 |
* <code> |
146 |
* $locator = array( |
147 |
* dsn => 'mysql://username:password@localhost/database', |
148 |
* ); |
149 |
* </code> |
150 |
* |
151 |
* Example 2 - data is inside an odbms, reachable by doing remote procedure calls (rpc): |
152 |
* <code> |
153 |
* $locator = array( |
154 |
* type => 'rpc', |
155 |
* metadata => array( |
156 |
* package => 'Data::Driver::Proxy', |
157 |
* Host => 'localhost', |
158 |
* Port => '8765', |
159 |
* ) |
160 |
* ); |
161 |
* </code> |
162 |
* |
163 |
* @deprecated |
164 |
* |
165 |
*/ |
166 |
var $_locator_metadata = NULL; |
167 |
|
168 |
|
169 |
/** |
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 |
|
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 |
// additional information required (passed on to the Proxy by DataSource::Generic) |
200 |
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 |
* @param object(DataSource::Locator)|hash optional: a &$locator instance |
210 |
*/ |
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 |
* to an encapsulated Data::Driver::Proxy instance |
286 |
* |
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 |
//package => 'DataSource::Generic', |
313 |
//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 |
?> |