/[cvs]/nfo/php/libs/org.netfrag.glib/DataSource/Locator.php
ViewVC logotype

Annotation of /nfo/php/libs/org.netfrag.glib/DataSource/Locator.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Wed Mar 5 15:40:39 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.4: +36 -18 lines
updated docu (phpDocumentor testing....)

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed