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