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