1 |
<? |
2 |
## ------------------------------------------------------------------------------------- |
3 |
## $Id: ProxyObject.php,v 1.6 2002/12/22 13:26:20 jonen Exp $ |
4 |
## ------------------------------------------------------------------------------------- |
5 |
## $Log: ProxyObject.php,v $ |
6 |
## Revision 1.6 2002/12/22 13:26:20 jonen |
7 |
## + added support of tangram independent id (e.g. Data::UUID) toggled by option at conrtuctor |
8 |
## |
9 |
## Revision 1.5 2002/12/18 22:36:49 jonen |
10 |
## + added support to get remote objects via backend via 'guid' |
11 |
## + renamed '_loadBackend' to '_loadRemote' |
12 |
## + constructor now accepts additional options: |
13 |
## + remote: try to get object via '_loadRemote' ... |
14 |
## + oid: use the given identifier as an 'oid' when doing '_loadRemote' |
15 |
## + guid: use the given identifier as a 'guid' when doing '_loadRemote' |
16 |
## |
17 |
## Revision 1.4 2002/12/12 02:46:31 joko |
18 |
## + state (session) gets flushed when data was successfully loaded from remote side |
19 |
## + fixed _loadBackend |
20 |
## + fixed flushState |
21 |
## |
22 |
## Revision 1.3 2002/12/06 04:12:54 joko |
23 |
## + replaced 'xyzCache' through 'xyzProxy' |
24 |
## + function store(...) |
25 |
## |
26 |
## Revision 1.2 2002/12/05 21:44:09 joko |
27 |
## + debugging |
28 |
## |
29 |
## Revision 1.1 2002/12/01 17:23:58 joko |
30 |
## + initial check-in |
31 |
## |
32 |
## Revision 1.11 2002/11/12 06:05:58 cvsjoko |
33 |
## + renamed class: Text_Encode -> TextEncode |
34 |
## |
35 |
## Revision 1.10 2002/10/29 19:15:33 cvsjoko |
36 |
## - moved utf8/iso-conversion to lib/utils/Text_Encode.php |
37 |
## |
38 |
## Revision 1.9 2002/10/26 12:32:36 cvsjoko |
39 |
## - removed debugging via "print" |
40 |
## + added generic logging via PEAR::Log |
41 |
## |
42 |
## Revision 1.8 2002/10/22 09:51:38 cvsmax |
43 |
## + moved semi-method 'createBackend' to method $site->user->create at User.class.php |
44 |
## |
45 |
## Revision 1.7 2002/10/21 18:27:09 cvsjoko |
46 |
## - manually disabled any form of caching |
47 |
## |
48 |
## Revision 1.6 2002/10/17 03:48:47 cvsmax |
49 |
## + new |
50 |
## + function _createBackend create new Backend |
51 |
## + function _create api-create |
52 |
## |
53 |
## Revision 1.5 2002/10/16 03:37:54 cvsjoko |
54 |
## + bugfix: wrong comparison in restriction to save to array |
55 |
## |
56 |
## Revision 1.4 2002/10/10 03:04:23 cvsjoko |
57 |
## + no debug-output |
58 |
## |
59 |
## Revision 1.3 2002/10/10 03:03:27 cvsjoko |
60 |
## + bugfix: save object to cache only if payload from backend isn't empty |
61 |
## |
62 |
## Revision 1.2 2002/10/10 02:40:06 cvsjoko |
63 |
## + new level of data-caching (session and persistant) |
64 |
## + function _loadState loads data from session |
65 |
## + function _saveState saves data to session |
66 |
## + function save api-save (session & backend) |
67 |
## + function _commit saves data to backend |
68 |
## + handy utils |
69 |
## + function _setAttributes |
70 |
## + function flushState |
71 |
## |
72 |
## Revision 1.1 2002/10/09 00:51:39 cvsjoko |
73 |
## + new |
74 |
## ------------------------------------------------------------------------------------- |
75 |
|
76 |
class ProxyObject { |
77 |
|
78 |
var $objectId; |
79 |
// purpose ... |
80 |
var $meta; |
81 |
var $payload; |
82 |
var $attributes; |
83 |
var $backend; |
84 |
|
85 |
function ProxyObject($objectId = "", $options = array() ) { |
86 |
session_register_safe("proxy"); |
87 |
$this->backend = new Remote; |
88 |
$this->_init($objectId, $options); |
89 |
} |
90 |
|
91 |
function _init($objectId="", $options = array() ) { |
92 |
if ($objectId) { |
93 |
$this->objectId = $objectId; |
94 |
$this->meta = $options; |
95 |
|
96 |
// set default load mechanismn |
97 |
if( $this->meta[remote] && ((!$this->meta[oid] && !$this->meta[guid]) || ($this->meta[oid] && $this->meta[guid])) ) { |
98 |
$this->meta[oid] = 1; |
99 |
} |
100 |
|
101 |
$this->load(); |
102 |
//$this->_saveProxy(); |
103 |
} |
104 |
} |
105 |
|
106 |
function load() { |
107 |
if (!$this->_loadState()) { |
108 |
if (!$this->_loadProxy()) { |
109 |
|
110 |
// just load object from remote side if its flagged to be a remote one ... |
111 |
if ($this->meta[remote]) { |
112 |
$this->_loadRemote(); |
113 |
} |
114 |
|
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
function save($data, $type) { |
120 |
$this->_setAttributes($data); |
121 |
$this->_saveState(); |
122 |
if ($type == 'commit') { |
123 |
$this->_commit(); |
124 |
} |
125 |
if ($type == 'create') { |
126 |
$this->_create(); |
127 |
} |
128 |
} |
129 |
|
130 |
function flush() { |
131 |
$this->flushState(); |
132 |
$this->flushProxy(); |
133 |
} |
134 |
|
135 |
function _commit() { |
136 |
$this->_saveBackend($this->attributes); |
137 |
$this->flushState(); |
138 |
$this->flushProxy(); |
139 |
} |
140 |
|
141 |
|
142 |
// TODO: make this work |
143 |
/* |
144 |
function _create() { |
145 |
$this->_createBackend($this->attributes); |
146 |
$this->flushState(); |
147 |
$this->flushProxy(); |
148 |
} |
149 |
*/ |
150 |
|
151 |
function getAttributes() { |
152 |
if (!$this->meta[decoded]) { |
153 |
$this->_decode(); |
154 |
$this->_saveState(); |
155 |
} |
156 |
return $this->attributes; |
157 |
} |
158 |
|
159 |
function _setAttributes($data) { |
160 |
$this->attributes = $data; |
161 |
} |
162 |
|
163 |
function flushProxy() { |
164 |
connectdb(); |
165 |
$sql = "DELETE FROM f_proxy WHERE oid='$this->objectId'"; |
166 |
send_sql($sql); |
167 |
} |
168 |
|
169 |
function flushState() { |
170 |
global $proxy; |
171 |
unset($proxy[$this->objectId]); |
172 |
$this->meta[decoded] = 0; |
173 |
} |
174 |
|
175 |
function _loadState() { |
176 |
global $proxy; |
177 |
logp(get_class($this) . "->_loadState()", LOG_DEBUG); |
178 |
if ($this->attributes = $proxy[$this->objectId]) { |
179 |
//print "_loadState:" . dumpVar($this->attributes); |
180 |
$this->meta[decoded] = 1; |
181 |
// TODO: make a parameter from this (0 deactivates session-layer) |
182 |
return 0; |
183 |
} |
184 |
} |
185 |
|
186 |
function _saveState() { |
187 |
global $proxy; |
188 |
logp(get_class($this) . "->_saveState()", LOG_DEBUG); |
189 |
$proxy[$this->objectId] = $this->attributes; |
190 |
//print "_saveState: " . dumpVar($this->attributes); |
191 |
// TODO: throw exception-message back to user if operation fails |
192 |
} |
193 |
|
194 |
function _loadProxy() { |
195 |
logp(get_class($this) . "->_loadProxy()", LOG_DEBUG); |
196 |
connectdb(); |
197 |
$sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'"; |
198 |
if ($res = send_sql($sql)) { |
199 |
$row = mysql_fetch_array($res, MYSQL_ASSOC); |
200 |
if ($row) { |
201 |
$this->payload = $row[payload]; |
202 |
// TODO: make a parameter from this (0 deactivates mysqldb-layer) |
203 |
return 0; |
204 |
} |
205 |
} |
206 |
} |
207 |
|
208 |
// TODO: use PEAR here |
209 |
function _saveProxy() { |
210 |
logp(get_class($this) . "->_saveProxy()", LOG_DEBUG); |
211 |
connectdb(); |
212 |
if ($this->payload) { |
213 |
//$sql = "INSERT INTO f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
214 |
$sql = "INSERT INTO f_proxy SET oid='$this->objectId', payload='$this->payload'"; |
215 |
if (!send_sql($sql)) { |
216 |
$sql = "UPDATE f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
217 |
send_sql($sql); |
218 |
} |
219 |
} |
220 |
} |
221 |
|
222 |
function _loadRemote() { |
223 |
logp(get_class($this) . "->_loadRemote()", LOG_DEBUG); |
224 |
|
225 |
// TODO: test backend for reachability first (eventually cache this information and "reset" it by another party) |
226 |
|
227 |
// check for guid or oid |
228 |
if($this->meta[guid]) { |
229 |
$args = array( guid => $this->objectId, classname => $this->meta[classname] ); |
230 |
$result = $this->backend->send('getObjectByGuid', $args ); |
231 |
} |
232 |
if($this->meta[oid]) { |
233 |
$result = $this->backend->send('getObject', $this->objectId); |
234 |
} |
235 |
|
236 |
if ($result) { |
237 |
//print "result: " . dumpVar($result) . "<br>"; |
238 |
if (count($result) == 0) { return; } |
239 |
|
240 |
if ($_GET[debug]) { |
241 |
print dumpVar($result); |
242 |
} |
243 |
$this->payload = serialize($result); |
244 |
// ----- move this to _encode some times |
245 |
|
246 |
$this->_saveProxy(); |
247 |
//print "oid: $this->objectId<br>"; |
248 |
$this->flushState(); |
249 |
} else { |
250 |
print "Error in _loadRemote!!!<br>"; |
251 |
} |
252 |
|
253 |
} |
254 |
|
255 |
function _saveBackend($result) { |
256 |
logp(get_class($this) . "->_saveBackend()", LOG_DEBUG); |
257 |
|
258 |
//$encoder = new TextEncode($result); |
259 |
//$encoder->toUTF8(); |
260 |
|
261 |
// check for guid or oid |
262 |
if($this->meta[guid]) { |
263 |
$args = array( 'guid' => $this->objectId, 'classname' => $this->meta[classname], 'data' => $result ); |
264 |
$response = $this->backend->send('saveObjectByGuid', $args, array( utf8 => 1) ); |
265 |
} |
266 |
if($this->meta[oid]) { |
267 |
$response = $this->backend->send('saveObject', array('oid' => $this->objectId, 'data' => $result), array( utf8 => 1) ); |
268 |
} |
269 |
} |
270 |
|
271 |
function _decode() { |
272 |
// fill attributes-hashtable from message-hashtable |
273 |
if (!$this->payload) { return; } |
274 |
//if ($this->attributes = $backend->decodeData($this->payload)) { |
275 |
if ($this->attributes = unserialize($this->payload)) { |
276 |
$this->meta[decoded] = 1; |
277 |
} |
278 |
} |
279 |
|
280 |
function store($struct) { |
281 |
$this->payload = serialize($struct); |
282 |
$this->_saveProxy(); |
283 |
} |
284 |
|
285 |
} |
286 |
|
287 |
?> |