-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRouteBase.php
483 lines (479 loc) · 12.9 KB
/
IRouteBase.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
/**
* 路由
* @since 2014
*/
class IRouteBase
{
public static $pre = '';
//基础URL
public $base_url;
protected $method;
public static $router;
public $match = '/<(\w+):([^>]+)?>/';
public static $app = [];
//相对URL
public static $index;
/**
*默认路由模块namespace为module
*/
public static $r = ['core','app','modules'];
//当前正则的URL 如 aa
protected $_url;
//当前URL的function 如 function(){}
protected $_value;
public $host;
public $class = [];
public static $obj;
//当前使用的CLASS
public static $current_class;
//当前域名
public static $current_domain;
public static $err;
public static $status;
/**
* 控制器名称
* strtolower
* ucfirst
*/
public static $controller_name = 'strtolower';
/**
* 初始化
*/
public static function init()
{
if(!isset(static::$obj)) {
static::$obj = new static();
}
return static::$obj;
}
/**
* 执行路由
*/
public static function do($ok = null, $not_find = null)
{
$IRoute = IRoute::run();
$err = IRoute::$err;
if(self::$status == 'ok') {
echo $IRoute;
$ok();
} else {
if($err) {
//未找到路由
$not_find();
}
}
}
/**
* 处理对象
*/
public static function do_object($res) {}
/**
* uri
*/
public static function uri()
{
$uri = static::_uri();
if($uri != '/') {
$uri = substr($uri, 1);
}
return $uri;
}
/**
*内部函数
*/
public static function _uri()
{
//解析URL $uri 返回 /app/public/ 或 /
$uri = $_SERVER['REQUEST_URI'];
$uri = str_replace("//", '/', $uri);
if(self::$pre){
$uri = substr($uri,strlen(self::$pre)+1);
}
if(strpos($uri, '?') !== false) {
$uri = substr($uri, 0, strpos($uri, '?'));
}
return $uri;
}
/**
* 构造函数
*/
public function __construct()
{
//请求方式 GET POST
$this->method = $_SERVER['REQUEST_METHOD'];
$this->host = static::host();
}
/**
* server_name
* @return
*/
public static function server_name()
{
return $_SERVER['SERVER_NAME'];
}
/**
* host自动加http://或https://
* @return string
*/
public static function host()
{
$top = 'http';
if(isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) {
$top = 'https';
} elseif(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 1 || $_SERVER['HTTPS'] == 'on')) {
$top = 'https';
}
return $top . "://" . static::server_name();
}
/**
* domain路由
*
* @param string $domain
* @param string $fun
* @return call_user_func
*/
public static function domain($domain, $fun)
{
if($domain != static::server_name()) {
return;
}
call_user_func($fun);
}
/**
* 取得控制器的 model id action
* [action] => login
* [module] => admin
* [package] => core
* [controller] => site
*/
public static function get_action()
{
$ar = static::init()->class;
$id = str_replace('\\', '/', $ar[0]);
$arr = explode("/", $id);
$vo['action'] = $ar[1];
$vo['package'] = $arr[0];
$vo['module'] = $arr[1];
$vo['controller'] = strtolower($arr[3]);
return $vo;
}
/**
* 对GET POST all 设置router
*/
protected function set_router($url, $do, $method = 'GET', $name = null)
{
if(is_string($do)) {
$do = str_replace("/", "\\", $do);
if(!$name && strpos($do, '@') !== false) {
$name = str_replace("\\controller", "", $do);
$name = str_replace("\\", "/", $name);
$name = substr($name, strpos($name, '/') + 1);
$name = str_replace("@", "/", $name);
}
}
if(strpos($url, '|') !== false) {
$arr = explode('|', $url);
if(strpos($name, '|') !== false) {
$names = explode('|', $name);
} else {
$names[0] = $name;
}
$i = 0;
foreach($arr as $v) {
$this->set_router($v, $do, $method, $names[$i] ?? '');
$i++;
}
return;
}
if(strpos($url, '<') !== false) {
$url = "#^\/{$url}\$#" ;
} elseif(substr($url, 0, 1) != '/') {
$url = '/' . $url;
}
static::$router[$method][$url] = $do;
if($name) {
static::$router['__#named#__'][$name] = $url;
}
} /** * 生成URL */
public static function url($url, $par = [])
{
return static::init()->create_url($url, $par);
}
/**
* 生成URL
*/
protected function create_url($url, $par = [])
{
$url = str_replace('.', '/', $url);
$id = 'route_url' . $url . json_encode($par);
if(isset(static::$app[$id]) && static::$app[$id]) {
return static::$app[$id];
}
if(isset(static::$router['__#named#__'][$url])) {
$r = static::$router['__#named#__'][$url];
preg_match_all($this->match, $r, $out);
$a = $out[0];
$b = $out[1];
} else {
$a = array();
$b = array();
}
if($b) {
$i = 0;
foreach($b as $v) {
if(isset($a[$i]) && isset($par[$v])) {
$r = str_replace($a[$i], $par[$v], $r);
unset($par[$v]);
}
$i++;
}
}
if(isset($r) && $r == '/') {
goto GT;
}
if(isset($r) && substr($r, 0, 2) == '#^') {
$r = substr($r, 4, -2);
}
if(isset($r) && substr($r, -1) == '/') {
$r = substr($r, 0, -1);
}
if(!isset($r) || !$r) {
$r = $url;
}
GT:
if($par) {
$r = $r . "?" . http_build_query($par);
}
$url = $this->base_url . $r;
$url = str_replace("//", '/', $url);
static::$app[$id] = $url;
return $url;
}
/**
* get request
*/
public static function get($url, $do, $name = null)
{
static::init()->set_router($url, $do, 'GET', $name);
}
/**
* post request
*/
public static function post($url, $do, $name = null)
{
static::init()->set_router($url, $do, 'POST', $name);
}
/**
* put request
*/
public static function put($url, $do, $name = null)
{
static::init()->set_router($url, $do, 'PUT', $name);
}
/**
* put request
*/
public static function delete($url, $do, $name = null)
{
static::init()->set_router($url, $do, 'DELETE', $name);
}
/**
* get/post request
*/
public static function all($url, $do, $name = null)
{
static::init()->set_router($url, $do, 'POST', $name);
static::init()->set_router($url, $do, 'GET', $name);
}
/**
* 执行路由
*/
public static function run()
{
return static::init()->exec();
}
/**
* 内部函数, 执行解析URL 到对应namespace 或 closure
*/
protected function exec()
{
//解析URL $uri 返回 /app/public/ 或 /
$uri = static::_uri();
//取得入口路径
$index = static::server_name();
$index = substr($index, 0, strrpos($index, '/'));
$action = substr($uri, strlen($index));
$this->base_url = $index ? $index . '/' : '/';
/**
* 对于未使用正则的路由匹配到直接goto
*/
if(isset(static::$router[$this->method][$action])) {
$this->_value = static::$router[$this->method][$action];
} else {
$this->_value = false;
}
$data = [];
if($this->_value) {
goto TODO;
}
if(!isset(static::$router[$this->method])) {
goto NEXT;
}
foreach(static::$router[$this->method] as $pre => $class) {
if(preg_match_all($this->match, $pre, $out)) {
//转成正则
foreach($out[0] as $k => $v) {
$pre = str_replace($v, "(" . $out[2][$k] . ")", $pre);
}
$pregs[$pre] = ['class' => $class,'par' => $out[1]];
}
}
NEXT:
/**
* 匹配当前URL是否存在路由
*/
if(isset($pregs) && $pregs) {
foreach($pregs as $p => $par) {
$class = $par['class'];
if(preg_match($p, $action, $new)) {
unset($new[0]);
//根据请求设置值 $_POST $_GET
$data = $this->set_request_value($this->array_combine($par['par'], $new));
$this->_url = $pre;
$this->_value = $class;
goto TODO;
}
}
}
if($this->_value) {
TODO:
// 如果是 closure
if(is_object($this->_value) || ($this->_value instanceof Closure)) {
$res = call_user_func_array($this->_value, $data);
return $this->output($res);
}
// 对 namespace 进行路由
$this->_value = str_replace('/', '\\', $this->_value);
$cls = explode('@', $this->_value);
$class = $cls[0];
if($data) {
foreach($data as $k => $v) {
$class = str_replace("$" . $k, $v, $class);
}
}
$ac = $cls[1];
return $this->load_route($class, $ac, $data);
}
//加载app\admin\login.php 这类的自动router
$action = trim(str_replace('/', ' ', $action));
$a = explode(' ', $action);
$classes = [];
if(isset($a[0])) {
foreach(static::$r as $r) {
$class = $r . "\\" . $a[0];
if(isset($a[1])) {
$class = $class . "\\controller\\" . $a[1];
}
$classes[] = $class;
}
}
if(isset($a[2]) && $a[2]) {
$ac = $a[2];
} else {
$ac = 'index';
}
foreach($classes as $class) {
$res = $this->load_route($class, $ac, $data);
if($res !== false) {
self::$status = 'ok';
return $res;
}
}
}
/**
* 建议内部使用
*/
public static function get_class()
{
return static::$current_class;
}
/**
* 内部函数,支持框架内部框架
*/
protected function load_route($class, $ac, $data)
{
$a = substr($class, 0, strrpos($class, '\\'));
$b = substr($class, strrpos($class, '\\') + 1);
$fun = IRoute::$controller_name;
$b = $fun($b);
$class = $a . "\\" . $b;
$this->class = [$class, $ac];
static::$current_class = $class;
if(!class_exists($class)) {
self::$err[] = "class 【" . $class . "】 not exists ";
return false;
}
$obj = new $class();
if(method_exists($class, 'before')) {
$obj->before();
}
$res = '';
if(method_exists($class, "action_" . $ac)) {
$action = "action_" . $ac;
$res = $obj->$action();
self::$err = [];
if($res) {
return $this->output($res);
}
} else {
self::$err[] = "action 【action_" . $ac . "】 not exists ";
return false;
}
if(method_exists($class, 'after')) {
$obj->after();
}
}
/**
* 输出
*/
protected function output($res)
{
if(is_array($res)) {
header('content-type:application/json');
echo json_encode($res, JSON_UNESCAPED_UNICODE);
exit;
} elseif(is_string($res)) {
echo $res;
} elseif(is_object($res)) {
return static::do_object($res);
}
}
/**
* 内部函数 ,对array_combine优化
*/
protected function array_combine($a = [], $b = [])
{
$i = 0;
foreach($b as $v) {
$out[$a[$i]] = $v;
$i++;
}
return $out;
}
/**
* 内部函数 ,根据请求设置值
*/
protected function set_request_value($data)
{
switch($this->method) {
case 'GET':
$_GET = array_merge($data, $_GET);
break;
case 'POST':
$_POST = array_merge($data, $_POST);
break;
}
return $data;
}
}