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