-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvan.class.php
More file actions
172 lines (143 loc) · 5.66 KB
/
Copy pathvan.class.php
File metadata and controls
172 lines (143 loc) · 5.66 KB
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
<?php
/************** base classes **********/
class VanStdClassLoader implements IAutoload {
public $class_roots = array(VAN_ROOT);
/* auto load class
*
* @param $classname, string
* @return $obj, object
* @err return false;
*/
public function autoload($classname){
$name = $classname;
van_map_class2path($name, $path, $classfile);
$path = trim($path, "/");
foreach ($this->class_roots as $dir) {
$file = $dir."/".$path.".php"; //try path "a/b.php" for class ABC
if (file_exists($file)) {
include $file;
if (class_exists($name) || interface_exists($name)) {
return ;
}
}
$file = $dir."/".$path."/".$classfile.".php"; //try path "a/b/c.php" for class ABC
if (file_exists($file)) {
include $file;
if (class_exists($name) || interface_exists($name)) {
return ;
}
}
}
}
}
class VanStdObjLoader implements IMap{
public $ce = null;
public function __construct(){
$this->ce = new ClassExplainer();
}
/*fetch io class from the "path"
*
*@param $path, see formate in ClassExplainer::fetch();
*/
public function map($path){
$class_arr = $this->ce->explain($path);
if ($class_arr == false) {
throw new Exception($this->ce->err, CORE_ERR_IO_ROUTING);
} else {
return $this->_fetch($class_arr);
}
}
private function _fetch($class_arr){
$classname = $class_arr['CLASS'];
if (!class_exists($classname, true)){
throw new Exception ("faild to load ioclass ,class=".$classname, CORE_ERR_IO_ROUTING);
}
$params = array();
foreach ($class_arr['ARGS'] as $i => $c) {
if (is_array($c)) {
$params[$i] = $this->_fetch($c);
} else {
$params[$i] = trim($c);
}
}
return new $classname($params);
}
}
/*pares an string into an array
*
* string like " a(1, b(1,2), c()) " into $obj = new a(1, new b(1,2), new c());
*/
class VanStdClassExplainer{
private $_tmp_stack = array();
private $pos = 0;
private $str_len = null;
public $open_tag = "(";
public $close_tag = ")";
public $arg_split = ",";
public $err = '';
private function _clean(){
$this->_tmp_stack = array();
$this->pos = 0;
$this->str_len = null;
$this->err = '';
}
public function explain($str){
if ($this->str_len === null) {
$this->str_len = strlen($str);
}
$class = '';
// find class name
for ( ;
$this->pos < $this->str_len && $str[$this->pos] != $this->open_tag ;
$this->pos++ ){
$class .= $str[ $this->pos ];
}
$class = trim($class);
if (!preg_match('/^[a-zA-Z]\w*$/', $class)) {
$this->err = "faile while fetching obj, class name wrong on str[".$this->pos."] ,class=".$class.",pos=[".$this->pos."]";
return false;
}
array_push($this->_tmp_stack, $class);
// find args
$res = array("CLASS"=>$class, "ARGS"=>array());
$arg = '';
for ( $j = 0, $this->pos ++ ; $this->pos < $this->str_len; $this->pos ++ ) {
$arg .= $str[$this->pos];
if ($str[$this->pos] == $this->arg_split){ //normal args like "1, 2, awen"
$arg = trim($arg, $this->arg_split);
if ($arg) {
$res["ARGS"][$j++] = $arg;
}
$arg = '';
} else if ($str[$this->pos] == $this->open_tag) { //find another class
$arg = trim($arg, $this->open_tag);
if (!preg_match('/^[a-zA-Z]\w*$/', trim($arg))) {
$this->err = "wrong class name,class=".trim($arg).",pos=[".$this->pos."]";
return false;
}
$this->pos -= strlen($arg);
if (!($arg = $this->explain($str))) {
return false;
}
if (($top_class = array_pop($this->_tmp_stack)) != $class) { //check if it's match
return false;
} else {
array_push($this->_tmp_stack, $class);
}
$res["ARGS"][$j++] = $arg;
$arg = '';
} else if ($str[$this->pos] == $this->close_tag) { //match the closing tag
array_pop($this->_tmp_stack);
if ($arg = trim($arg, $this->close_tag)) {
$res["ARGS"][$j++] = trim($arg);
}
if (empty($this->_tmp_stack)) {
$this->_clean();
}
return $res;
}
}
$this->err = "failed to metch the closing tag,class=".$class.",pos=[".$this->pos."]";
return false;
}
}