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