-
Notifications
You must be signed in to change notification settings - Fork 0
/
van.php
288 lines (235 loc) · 7.9 KB
/
van.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
if (!defined("VAN_ROOT")) {
define("VAN_ROOT", dirname(__FILE__) . "/");
}
if (!defined("VAN_TIME_NOW")) {
define("VAN_TIME_NOW", time());
}
if (!defined("VC_TIME_NOW")) {
define("VC_TIME_NOW", time());
}
if (!defined('VAN_MIC_TIME_NOW')) {
define('VAN_MIC_TIME_NOW', str_replace('.', '', microtime(true)));
}
$GLOBALS["__van_objs"] = array();
$GLOBALS["__van_objs_num"] = 1;
$GLOBALS["__van_links"] = array();
include VAN_ROOT . "van.class.php";
include VAN_ROOT . "lib.func.php";
function van_assert($assert, $errmsg){
if ($assert !== true){
throw new Exception($errmsg);
}
}
function van_add_classloader($classname){
$loader = new $classname;
van_assert($loader instanceof IAutoload, "class is not instance of IAutoload");
$oid = van_load($loader);
spl_autoload_register(array(van_get($oid), "autoload"));
return $oid;
}
function van_load($obj){
$oid = $GLOBALS["__van_objs_num"] ;
$GLOBALS["__van_objs_num"] ++;
$GLOBALS["__van_objs"][$oid]["obj"] = $obj;
$GLOBALS["__van_objs"][$oid]["nl"] = 0;
return $oid;
}
function van_unload($oid){
return van_unlink($oid);
}
function van_get($oid) {
if (isset($GLOBALS["__van_objs"][$oid])) {
return $GLOBALS["__van_objs"][$oid]["obj"];
}
if (isset($GLOBALS["__van_links"][$oid])) {
$oid = $GLOBALS["__van_links"][$oid];
van_assert(isset($GLOBALS["__van_objs"][$oid]), "empty obj for oid");
return $GLOBALS["__van_objs"][$oid]["obj"];
}
}
/* link the obj
*
* @param $obj, mixed, new obj or oid from van_link(), van_load()
* @return $oid
*/
function van_link($linkname, $obj){
if (is_object($obj)) {
$oid = van_load($obj);
return van_link($linkname, $oid);
}
van_unlink($linkname);
$oid = $obj;
if (isset($GLOBALS["__van_links"][$oid])) {
$oid = $GLOBALS["__van_links"][$oid];
}
if (isset($GLOBALS["__van_objs"][$oid])) {
$GLOBALS["__van_links"][$linkname] = $oid;
$GLOBALS["__van_objs"][$oid]['nl'] += 1;
}
return $oid;
}
function van_batch_link(array $cfg) {
foreach ($cfg as $linkname => $io) {
$obj = van_create_obj($io);
van_link($linkname, $obj);
}
}
function van_create_obj(array $cfg) {
$io = $cfg;
if (class_exists($io['CLASS'])) {
$io["CLASS"] = trim($io["CLASS"]);
$IO_CLASS = $io['CLASS'];
unset($io['CLASS']);
$obj = new $IO_CLASS($io);
foreach ($io as $attr_name => $attr_value) {
if (property_exists($obj, $attr_name)) {
if (is_array($attr_value) && isset($attr_value["CLASS"])) {
if (class_exists($attr_value["CLASS"])) {
$obj->$attr_name = van_create_obj($attr_value);
}
} else {
$obj->$attr_name = $attr_value;
}
}
}
return $obj;
}
return null;
}
function van_unlink($linkname){
if (isset($GLOBALS["__van_links"][$linkname])) {
$oid = $GLOBALS["__van_links"][$linkname];
unset($GLOBALS["__van_links"][$linkname]);
} else {
$oid = $linkname;
}
if (isset($GLOBALS["__van_objs"][$oid])) {
$GLOBALS["__van_objs"][$oid]["nl"] -= 1;
if ($GLOBALS["__van_objs"][$oid]["nl"] <= 0) {
unset($GLOBALS["__van_objs"][$oid]);
}
}
}
function van_read($oid, $query){
van_assert( ($obj = van_get($oid)) instanceof IIo, "obj is not instance of IIo" );
return $obj->read($query);
}
function van_write($oid, $data){
van_assert( ($obj = van_get($oid)) instanceof IIo, "obj is not instance of IIo" );
return $obj->write($data);
}
function van_flush($oid){
van_assert( ($obj = van_get($oid)) instanceof IIo, "obj is not instance of IIo" );
$obj->flush();
}
function van_pop($oid ){
van_assert( ($obj = van_get($oid)) instanceof IIo, "obj is not instance of IIo" );
return $obj->pop();
}
function van_map($oid, $data ){
van_assert( ($obj = van_get($oid)) instanceof IMap, "obj is not instance of IMap" );
return $obj->map($data);
}
function van_run($oid, $params=array()){
van_assert( ($obj = van_get($oid)) instanceof IJob, "obj is not instance of IJob" );
return $obj->run($params);
}
function van_set_attr($oid, $attr_name, $attr_value){
van_assert( ($obj = van_get($oid)) instanceof ICtrl, "obj is not instance of ICtrl" );
van_assert( property_exists($obj, $attr_name), "empty attr_name");
$obj->$attr_name = $attr_value;
}
function van_get_attr($oid, $attr_name){
van_assert( ($obj = van_get($oid)) instanceof ICtrl, "obj is not instance of ICtrl" );
van_assert( isset($obj->$attr_name), "empty attr_name");
return $obj->$attr_name;
}
function van_add_attr($oid, $attr_name, $attr_value){
van_assert( ($obj = van_get($oid)) instanceof ICtrl, "obj is not instance of ICtrl" );
van_assert( isset($obj->$attr_name), "empty attr_name");
van_assert( is_array($obj->$attr_name), "attribute is not an array");
array_push($obj->$attr_name, $attr_value);
}
function van_set_state($oid, $state){
van_assert( ($obj = van_get($oid)) instanceof IState, "obj is not instance of IState" );
return $obj->set_state($state);
}
function van_get_state($oid){
van_assert( ($obj = van_get($oid)) instanceof IState, "obj is not instance of IState" );
return $obj->get_state();
}
interface IIo {
/* read data from io
*
* @param $query, mixed
* @return $data, mixed
* @err must throw exception by van_assert() function
*/
public function read($query);
/* write data into io
*
* @param $data, mixed
* @return $result, mixed
* @err must throw exception by van_assert() function
*/
public function write($query);
/* flush buffer
*
* @return always true
* @err must throw exception by van_assert() function
*/
public function flush();
/* pop buffer
*
* @return $data, mixed
* @err always true
*/
public function pop();
}
interface ICtrl {
}
interface IJob {
/* run job
*
* @return $result, mixed, will be captured by We::run();
* @err throw exception
*/
public function run($param);
}
interface IState{
/* set state
*
* @param $state,
* @return true | false
* @err return false
*/
public function set_state($state);
/* get state
*
* @return null or string
* @err always true;
*/
public function get_state();
}
interface IMap{
/* map input
*
* @param $input, mixed
* @return $output, mixed
* @err return false
*/
public function map($input);
}
interface IAutoload{
/* auto load class, registered by spl_autoload_register()
*
* @param $input, string
* @return true | false
* @err return false;
*/
public function autoload($classname);
}
/* default init */
van_add_classloader(new VanStdClassLoader);
?>