1 |
<? |
2 |
/** |
3 |
* This file contains the Data::Lift component. |
4 |
* |
5 |
* @author Andreas Motl <andreas.motl@ilo.de> |
6 |
* @package org.netfrag.glib |
7 |
* @name Data::Lift |
8 |
* |
9 |
*/ |
10 |
|
11 |
/** |
12 |
* Cvs-Log: |
13 |
* |
14 |
* $Id: Generic.php,v 1.19 2003/04/18 15:40:51 joko Exp $ |
15 |
* |
16 |
* $Log: Generic.php,v $ |
17 |
* Revision 1.8 2003/04/19 16:13:52 jonen |
18 |
* + added class var '$_hidden_elements' which are passed to 'ExplorerTree' if exists |
19 |
* |
20 |
* Revision 1.7 2003/04/18 15:48:00 joko |
21 |
* better error handling: a) croak messages, b) just perform lift module if instance could be created |
22 |
* |
23 |
* Revision 1.6 2003/03/09 15:49:20 joko |
24 |
* fix towards optimizing autodocumentation: |
25 |
* enriched with metadata to tell autodia how to build object-relationships by discarding the fancy namespacing stuff there |
26 |
* |
27 |
* Revision 1.5 2003/03/08 20:04:59 root |
28 |
* + optimized comments for Autodia |
29 |
* |
30 |
* Revision 1.4 2003/03/08 18:22:23 joko |
31 |
* updated comments: now in phpDocumentor style |
32 |
* |
33 |
* Revision 1.3 2003/03/03 21:28:11 joko |
34 |
* updated comments |
35 |
* |
36 |
* Revision 1.2 2003/02/27 16:30:17 joko |
37 |
* + enhanced '_autodetect' |
38 |
* + added '_check' |
39 |
* + now throughout returning lifted values by reference |
40 |
* |
41 |
* Revision 1.1 2003/02/22 16:20:04 joko |
42 |
* + initial commit |
43 |
* |
44 |
*/ |
45 |
|
46 |
/** |
47 |
* Data::Lift |
48 |
* |
49 |
* <pre> |
50 |
* Data::Lift - Pass data around between various "actors". |
51 |
* |
52 |
* These "actors" are kinda plugin-modules |
53 |
* lying at "locations" and actually mungle the data. |
54 |
* |
55 |
* "Locations" are by now: |
56 |
* x local filesystem |
57 |
* o remote anything |
58 |
* |
59 |
* The "actors" require (by now) to be native php classes |
60 |
* having a method "perform". Please have a look at others |
61 |
* lying in the Data::Lift namespace at org.netfrag.glib to |
62 |
* get a picture of how to implement own "actors". |
63 |
* </pre> |
64 |
* |
65 |
* |
66 |
* @author Andreas Motl <andreas.motl@ilo.de> |
67 |
* @copyright (c) 2003 - All Rights reserved. |
68 |
* @license GNU LGPL (GNU Lesser General Public License) |
69 |
* |
70 |
* @link http://www.netfrag.org/~joko/ |
71 |
* @link http://www.gnu.org/licenses/lgpl.txt |
72 |
* |
73 |
* @package org.netfrag.glib |
74 |
* @subpackage DataLift |
75 |
* @name Data::Lift |
76 |
* |
77 |
* @link http://cvs.netfrag.org/php/libs/org.netfrag.glib |
78 |
* |
79 |
* |
80 |
* @todo refactor Data::Deep to a plugin module |
81 |
* @todo refactor Data::Encode to a plugin module |
82 |
* @todo combine Data::Lift and Data::Container somehow |
83 |
* @todo integrate with Data::Driver somehow -- proposal: |
84 |
* // $lift = ne w Data::Lift($locator); |
85 |
* // $lift->perform(); // talks to a Data::Driver |
86 |
* |
87 |
* |
88 |
*/ |
89 |
class Data_Lift { |
90 |
|
91 |
/** |
92 |
* this is to trick Autodia let understanding "namespaces" in php |
93 |
* unless the pattern can be tweaked by mode (namespace=0|1) |
94 |
* // $this = ne w Data::Lift(&$payload, $options = array()); |
95 |
*/ |
96 |
|
97 |
var $dblocations; |
98 |
var $actor; |
99 |
var $actor_instance; |
100 |
|
101 |
var $payload; |
102 |
var $vartype; |
103 |
var $metatype; |
104 |
var $_link_args = NULL; |
105 |
|
106 |
function Data_Lift(&$payload, $options = array()) { |
107 |
$this->_init_locations(); |
108 |
//print Dumper($options); |
109 |
//exit; |
110 |
$this->options = $options; |
111 |
if ($options[metatype]) { $this->metatype = $options[metatype]; } |
112 |
if ($options[link_args]) { $this->_link_args = $options[link_args]; } |
113 |
$this->set(&$payload); |
114 |
} |
115 |
|
116 |
function _init_locations() { |
117 |
$this->actor = array(); |
118 |
$this->dblocations = array( |
119 |
'bla' => 'blub', |
120 |
); |
121 |
} |
122 |
|
123 |
function set(&$payload, $metatype = '') { |
124 |
$this->payload = &$payload; |
125 |
|
126 |
if ($metatype) { $this->metatype = $metatype; } |
127 |
$this->_autodetect(); |
128 |
$this->_check(); |
129 |
} |
130 |
|
131 |
function _autodetect() { |
132 |
|
133 |
// FIXME: distinguish between is_array and is_hash here! |
134 |
if (is_object($this->payload)) { |
135 |
$this->vartype = 'object'; |
136 |
$this->metatype = get_class($this->payload); |
137 |
if ($parent_class = get_parent_class($this->payload)) { |
138 |
$this->metatype = $parent_class; |
139 |
} |
140 |
|
141 |
} elseif (is_array($this->payload)) { |
142 |
$this->vartype = 'hash'; |
143 |
|
144 |
} else { |
145 |
$this->vartype = 'scalar'; |
146 |
|
147 |
} |
148 |
} |
149 |
|
150 |
function _check() { |
151 |
|
152 |
$good = $this->vartype && $this->metatype; |
153 |
|
154 |
//print "metatype: " . $this->metatype . "<br>"; |
155 |
|
156 |
if (!$good) { |
157 |
$msg = "Data::Lift cannot handle this payload: "; |
158 |
$msg .= "[vartype=" . $this->vartype . ", metatype=" . $this->metatype . "]<br/>"; |
159 |
$msg .= "payload: '" . Dumper($this->payload) . "'"; |
160 |
user_error($msg); |
161 |
} |
162 |
|
163 |
} |
164 |
|
165 |
function &to($level) { |
166 |
$this->_resolve_location($level); |
167 |
//print Dumper($this->actor); |
168 |
//exit; |
169 |
//$result = array( name => 'abc', description => 'def' ); |
170 |
$result = &$this->_perform_actor(); |
171 |
return $result; |
172 |
} |
173 |
|
174 |
function _resolve_location($level) { |
175 |
|
176 |
// location can be resolved by location-table |
177 |
if ($location = $this->dblocations[$level]) { |
178 |
$this->actor = split('/', $location); |
179 |
} |
180 |
|
181 |
// try to automagically resolve location |
182 |
$part = array(); |
183 |
array_push($part, $this->vartype); |
184 |
array_push($part, $this->metatype); |
185 |
array_push($part, $level); |
186 |
$this->actor = $part; |
187 |
|
188 |
} |
189 |
|
190 |
function &_perform_actor() { |
191 |
//$actor_file = join('/', $this->actor) . '.php'; |
192 |
//include($actor_file); |
193 |
|
194 |
/** |
195 |
* <!-- Autodia --> |
196 |
* can do: (this is metadata supplied for Autodia, don't delete!) |
197 |
* $this->_actor_instance = new Data_Lift_hash_job_html() |
198 |
* $this->_actor_instance = new Data_Lift_hash_tree_PEAR_Tree() |
199 |
* $this->_actor_instance = new Data_Lift_object_tree_common_PEAR_HTML_TreeMenu() |
200 |
* $this->_actor_instance = new Data_Lift_object_tree_common_PEAR_HTML_TreeMenu_DHTML() |
201 |
* $this->_actor_instance = new Data_Lift_object_tree_common_PEAR_HTML_TreeMenu_Listbox() |
202 |
* |
203 |
*/ |
204 |
|
205 |
$actor_name = 'Data/Lift/' . join('/', $this->actor); |
206 |
//$actor_name = 'Data::Lift::' . join('::', $this->actor); |
207 |
|
208 |
//$actor_object = mkObject($actor_name); |
209 |
//return $actor_object->perform($this->payload); |
210 |
|
211 |
// fix [2003-04-13]: just perform if instance good |
212 |
//if ($actor_object = php::mkComponent($actor_name)) { |
213 |
// enhanced [2003-04-20]: pass Lift's options to actor's constructor |
214 |
$actor_object = mkObject($actor_name, $this->options); |
215 |
if (is_object($actor_object) && method_exists($actor_object, 'perform')) { |
216 |
// enhanced [2003-04-20]: *inject* Lift's options to actor's instance - prefix attribute by '_' to prevent collisions |
217 |
$actor_object->_options = $this->options; |
218 |
return $actor_object->perform($this->payload); |
219 |
if ($actor_name == "Data/Lift/hash/topic/ExplorerTree") { |
220 |
return $actor_object->perform($this->payload, $this->_link_args); |
221 |
} else { |
222 |
return $actor_object->perform($this->payload); |
223 |
} |
224 |
} else { |
225 |
user_error("Data::Lift could not call method 'perform' on actor object. [actor_name='$actor_name']"); |
226 |
//return array(); |
227 |
} |
228 |
|
229 |
} |
230 |
|
231 |
function get() { |
232 |
return $this->payload; |
233 |
} |
234 |
|
235 |
function add($data = array()) { |
236 |
$this->payload = array_merge($this->payload, $data); |
237 |
} |
238 |
|
239 |
} |
240 |
|
241 |
?> |