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