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