-
Notifications
You must be signed in to change notification settings - Fork 38
/
SlightPHP.php
371 lines (341 loc) · 7.54 KB
/
SlightPHP.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/*{{{LICENSE
+-----------------------------------------------------------------------+
| SlightPHP Framework |
+-----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation. You should have received a copy of the |
| GNU General Public License along with this program. If not, see |
| http://www.gnu.org/licenses/. |
| Copyright (C) 2008-2009. All Rights Reserved. |
+-----------------------------------------------------------------------+
| Supports: http://www.slightphp.com |
+-----------------------------------------------------------------------+
}}}*/
/**
* @package SlightPHP
*/
if(!class_exists("SlightPHP",false)):
final class SlightPHP{
/**
* @var string
*/
public static $appDir=".";
/**
* @var string
*/
public static $pathInfo="";
/**
* current zone
* @var string
*/
public static $zone;
/**
* @var string
*/
public static $defaultZone="zone";
/**
* current page
* @var string
*/
public static $page;
/**
* @var string
*/
public static $defaultPage="page";
/**
* current type
* @var string
*/
public static $type;
/**
* default file type, like $defaultZone.$defaultType.php
* @var string
*/
public static $defaultType="page";
/**
* current entry
* @var string
*/
public static $entry;
/**
* @var string
*/
public static $defaultEntry="entry";
/**
* split flag of zone,classs,method
*
* @var string
*/
public static $splitFlag="/";
/**
* zoneAlias
*
* @var array
*/
public static $zoneAlias;
/**
* @param string $zone
* @param string $alias
* @return boolean
*/
public static function setZoneAlias($zone,$alias){
self::$zoneAlias[$zone]=$alias;
return true;
}
/**
* @param string $zone
* @return string | boolean
*/
public static function getZoneAlias($zone){
return isset(self::$zoneAlias[$zone]) ? self::$zoneAlias[$zone] : false;
}
/**
* pageAlias
*
* @var array
*/
public static $pageAlias;
/**
* @param string $page
* @param string $alias
* @return boolean
*/
public static function setPageAlias($page,$alias){
self::$pageAlias[$page]=$alias;
return true;
}
/**
* @param string $page
* @return string | boolean
*/
public static function getPageAlias($page){
return isset(self::$pageAlias[$page]) ? self::$pageAlias[$page] : false;
}
/**
* defaultZone set
*
* @param string $zone
* @return boolean
*/
public static function setDefaultZone($zone){
self::$defaultZone = $zone;
return true;
}
/**
* defaultZone get
*
* @return string
*/
public static function getDefaultZone(){
return self::$defaultZone;
}
/**
* defaultClass set
*
* @param string $page
* @return boolean
*/
public static function setDefaultPage($page){
self::$defaultPage = $page;
return true;
}
/**
* defaultPage get
*
* @return string
*/
public static function getDefaultPage(){
return self::$defaultPage;
}
/**
* defaultType set
*
* @param string $page
* @return boolean
*/
public static function setDefaultType($type){
self::$defaultType= $type;
return true;
}
/**
* defaultType get
*
* @return string
*/
public static function getDefaultType(){
return self::$defaultType;
}
/**
* defaultMethod set
*
* @param string $entry
* @return boolean
*/
public static function setDefaultEntry($entry){
self::$defaultEntry = $entry;
return true;
}
/**
* defaultMethod get
*
* @return string $method
*/
public static function getDefaultEntry(){
return self::$defaultEntry;
}
/**
* splitFlag set
*
* @param string $flag
* @return boolean
*/
public static function setSplitFlag($flag){
self::$splitFlag = $flag;
return true;
}
/**
* defaultMethod get
*
* @return string
*/
public static function getSplitFlag(){
return self::$splitFlag;
}
/**
* appDir set && get
* IMPORTANT: you must set absolute path if you use extension mode(extension=SlightPHP.so)
*
* @param string $dir
* @return boolean
*/
public static function setAppDir($dir){
self::$appDir = $dir;
return true;
}
public static function setPathInfo($pathInfo){
self::$pathInfo = $pathInfo;
return true;
}
/**
* appDir get
*
* @return string
*/
public static function getAppDir(){
return self::$appDir;
}
/**
* debug status set
*
* @param boolean $debug
* @return boolean
*/
public static function setDebug($debug){
self::$_debug = $debug;
return true;
}
/**
* debug status get
*
* @return boolean
*/
public static function getDebug(){
return self::$_debug;
}
/**
* main method!
*
* @param string $path
* @return boolean
*/
public static function run($path=""){
$splitFlag = preg_quote(self::$splitFlag,"/");
$path_array = array();
if(!empty($path)){
$isPart = true;
}else{
$isPart = false;
if(!empty(self::$pathInfo)){
$path= self::$pathInfo;
}elseif(!empty($_SERVER['PATH_INFO'])){
$path= $_SERVER["PATH_INFO"];
}elseif(!empty($_SERVER['REQUEST_URI'])){
$path= $_SERVER["REQUEST_URI"];
}else{
self::debug("path not set in params or server.path_info, server.request_uri");
return false;
}
}
/* Skip leading / */
$len = strlen($path);
$start=0;
for($start=0;$start<$len;$start++){
if($path[$start]!='/')break;
}
$url_parsed = parse_url(substr($path,$start), PHP_URL_PATH);
if(!empty($url_parsed)){
$url = $url_parsed;
}else{
$url="";
}
/* file type */
$type = !empty($_SERVER['HTTP_REQUEST_TYPE'])? strtolower(trim($_SERVER['HTTP_REQUEST_TYPE'])) : self::$defaultType;
$path_array = preg_split("/[$splitFlag\/]/",$url,-1,PREG_SPLIT_NO_EMPTY);
$zone = !empty($path_array[0]) ? $path_array[0] : self::$defaultZone ;
$page = !empty($path_array[1]) ? $path_array[1] : self::$defaultPage ;
$entry = !empty($path_array[2]) ? $path_array[2] : self::$defaultEntry ;
if(self::$zoneAlias && ($key = array_search($zone,self::$zoneAlias))!==false){
$zone = $key;
}
if(self::$pageAlias && ($key = array_search($page,self::$pageAlias))!==false){
$page = $key;
}
if(!$isPart){
self::$type = $type;
self::$zone = $zone;
self::$page = $page;
self::$entry = $entry;
}else{
if($zone == self::$zone && $page == self::$page && $entry == self::$entry){
self::debug("part ignored [$path]");
return false;
}
}
$app_file = self::$appDir . DIRECTORY_SEPARATOR . $zone . DIRECTORY_SEPARATOR . $page . ".page.php";
if(!is_file($app_file)){
self::debug("file[$app_file] does not exists");
return false;
}else{
require_once(realpath($app_file));
}
$method = $type.$entry;
$classname = $zone ."_". $page;
if(!class_exists($classname,false)){
self::debug("class[$classname] does not exists");
return false;
}
$path_array[0] = $zone;
$path_array[1] = $page;
$path_array[2] = $entry;
$classInstance = new $classname($path_array);
if(!method_exists($classInstance,$method)){
self::debug("method[$method] does not exists in class[$classname]");
return false;
}
return call_user_func(array(&$classInstance,$method),$path_array);
}
/**
* @var boolean
*/
private static $_debug=0;
/*private*/
private static function debug($debugmsg){
if(self::$_debug){
error_log($debugmsg);
echo "<!--slightphp debug: ".$debugmsg."-->";
}
}
}
endif;