1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: liu21st <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace think\route; |
14
|
|
|
|
15
|
|
|
use think\Container; |
16
|
|
|
use think\Request; |
17
|
|
|
use think\Response; |
18
|
|
|
use think\Route; |
19
|
|
|
use think\route\dispatch\Callback as CallbackDispatch; |
20
|
|
|
use think\route\dispatch\Controller as ControllerDispatch; |
21
|
|
|
use think\route\dispatch\Redirect as RedirectDispatch; |
22
|
|
|
use think\route\dispatch\Response as ResponseDispatch; |
23
|
|
|
use think\route\dispatch\View as ViewDispatch; |
24
|
|
|
|
25
|
|
|
abstract class Rule |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* 路由标识 |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $name; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 路由对象 |
35
|
|
|
* @var Route |
36
|
|
|
*/ |
37
|
|
|
protected $router; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 路由所属分组 |
41
|
|
|
* @var RuleGroup |
42
|
|
|
*/ |
43
|
|
|
protected $parent; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* 路由规则 |
47
|
|
|
* @var mixed |
48
|
|
|
*/ |
49
|
|
|
protected $rule; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* 路由地址 |
53
|
|
|
* @var string|\Closure |
54
|
|
|
*/ |
55
|
|
|
protected $route; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* 请求类型 |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $method; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* 路由变量 |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $vars = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* 路由参数 |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $option = []; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* 路由变量规则 |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $pattern = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* 需要和分组合并的路由参数 |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
protected $mergeOptions = ['after', 'model', 'append', 'middleware']; |
86
|
|
|
|
87
|
|
|
abstract public function check(Request $request, string $url, bool $completeMatch = false); |
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 设置路由参数 |
91
|
|
|
* @access public |
92
|
|
|
* @param array $option 参数 |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function option(array $option) |
96
|
|
|
{ |
97
|
|
|
$this->option = array_merge($this->option, $option); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 设置单个路由参数 |
104
|
|
|
* @access public |
105
|
|
|
* @param string $name 参数名 |
106
|
|
|
* @param mixed $value 值 |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function setOption(string $name, $value) |
110
|
|
|
{ |
111
|
|
|
$this->option[$name] = $value; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* 注册变量规则 |
118
|
|
|
* @access public |
119
|
|
|
* @param array $pattern 变量规则 |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function pattern(array $pattern) |
123
|
|
|
{ |
124
|
|
|
$this->pattern = array_merge($this->pattern, $pattern); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* 设置标识 |
131
|
|
|
* @access public |
132
|
|
|
* @param string $name 标识名 |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function name(string $name) |
136
|
|
|
{ |
137
|
|
|
$this->name = $name; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* 获取路由对象 |
144
|
|
|
* @access public |
145
|
|
|
* @return Route |
146
|
|
|
*/ |
147
|
|
|
public function getRouter(): Route |
148
|
|
|
{ |
149
|
|
|
return $this->router; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* 获取Name |
154
|
|
|
* @access public |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getName(): string |
158
|
|
|
{ |
159
|
|
|
return $this->name; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* 获取当前路由规则 |
164
|
|
|
* @access public |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
public function getRule() |
168
|
|
|
{ |
169
|
|
|
return $this->rule; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* 获取当前路由地址 |
174
|
|
|
* @access public |
175
|
|
|
* @return mixed |
176
|
|
|
*/ |
177
|
|
|
public function getRoute() |
178
|
|
|
{ |
179
|
|
|
return $this->route; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* 获取当前路由的变量 |
184
|
|
|
* @access public |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
|
|
public function getVars(): array |
188
|
|
|
{ |
189
|
|
|
return $this->vars; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* 获取Parent对象 |
194
|
|
|
* @access public |
195
|
|
|
* @return $this|null |
196
|
|
|
*/ |
197
|
|
|
public function getParent() |
198
|
|
|
{ |
199
|
|
|
return $this->parent; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* 获取路由所在域名 |
204
|
|
|
* @access public |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function getDomain(): string |
208
|
|
|
{ |
209
|
|
|
return $this->parent->getDomain(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* 获取路由参数 |
214
|
|
|
* @access public |
215
|
|
|
* @param string $name 变量名 |
216
|
|
|
* @return mixed |
217
|
|
|
*/ |
218
|
|
|
public function config(string $name = '') |
219
|
|
|
{ |
220
|
|
|
return $this->router->config($name); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* 获取变量规则定义 |
225
|
|
|
* @access public |
226
|
|
|
* @param string $name 变量名 |
227
|
|
|
* @return mixed |
228
|
|
|
*/ |
229
|
|
|
public function getPattern(string $name = '') |
230
|
|
|
{ |
231
|
|
|
if ('' === $name) { |
232
|
|
|
return $this->pattern; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $this->pattern[$name] ?? null; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* 获取路由参数定义 |
240
|
|
|
* @access public |
241
|
|
|
* @param string $name 参数名 |
|
|
|
|
242
|
|
|
* @param mixed $default 默认值 |
243
|
|
|
* @return mixed |
244
|
|
|
*/ |
245
|
|
|
public function getOption(string $name = '', $default = null) |
246
|
|
|
{ |
247
|
|
|
if ('' === $name) { |
248
|
|
|
return $this->option; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $this->option[$name] ?? $default; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* 获取当前路由的请求类型 |
256
|
|
|
* @access public |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
public function getMethod(): string |
260
|
|
|
{ |
261
|
|
|
return strtolower($this->method); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* 设置路由请求类型 |
266
|
|
|
* @access public |
267
|
|
|
* @param string $method 请求类型 |
268
|
|
|
* @return $this |
269
|
|
|
*/ |
270
|
|
|
public function method(string $method) |
271
|
|
|
{ |
272
|
|
|
return $this->setOption('method', strtolower($method)); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* 检查后缀 |
277
|
|
|
* @access public |
278
|
|
|
* @param string $ext URL后缀 |
279
|
|
|
* @return $this |
280
|
|
|
*/ |
281
|
|
|
public function ext(string $ext = '') |
282
|
|
|
{ |
283
|
|
|
return $this->setOption('ext', $ext); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* 检查禁止后缀 |
288
|
|
|
* @access public |
289
|
|
|
* @param string $ext URL后缀 |
290
|
|
|
* @return $this |
291
|
|
|
*/ |
292
|
|
|
public function denyExt(string $ext = '') |
293
|
|
|
{ |
294
|
|
|
return $this->setOption('deny_ext', $ext); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* 检查域名 |
299
|
|
|
* @access public |
300
|
|
|
* @param string $domain 域名 |
301
|
|
|
* @return $this |
302
|
|
|
*/ |
303
|
|
|
public function domain(string $domain) |
304
|
|
|
{ |
305
|
|
|
return $this->setOption('domain', $domain); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* 设置参数过滤检查 |
310
|
|
|
* @access public |
311
|
|
|
* @param array $filter 参数过滤 |
312
|
|
|
* @return $this |
313
|
|
|
*/ |
314
|
|
|
public function filter(array $filter) |
315
|
|
|
{ |
316
|
|
|
$this->option['filter'] = $filter; |
317
|
|
|
|
318
|
|
|
return $this; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* 绑定模型 |
323
|
|
|
* @access public |
324
|
|
|
* @param array|string|\Closure $var 路由变量名 多个使用 & 分割 |
|
|
|
|
325
|
|
|
* @param string|\Closure $model 绑定模型类 |
|
|
|
|
326
|
|
|
* @param bool $exception 是否抛出异常 |
327
|
|
|
* @return $this |
328
|
|
|
*/ |
329
|
|
|
public function model($var, $model = null, bool $exception = true) |
330
|
|
|
{ |
331
|
|
|
if ($var instanceof \Closure) { |
332
|
|
|
$this->option['model'][] = $var; |
333
|
|
|
} elseif (is_array($var)) { |
334
|
|
|
$this->option['model'] = $var; |
335
|
|
|
} elseif (is_null($model)) { |
336
|
|
|
$this->option['model']['id'] = [$var, true]; |
337
|
|
|
} else { |
338
|
|
|
$this->option['model'][$var] = [$model, $exception]; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return $this; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* 附加路由隐式参数 |
346
|
|
|
* @access public |
347
|
|
|
* @param array $append 追加参数 |
348
|
|
|
* @return $this |
349
|
|
|
*/ |
350
|
|
|
public function append(array $append = []) |
351
|
|
|
{ |
352
|
|
|
$this->option['append'] = $append; |
353
|
|
|
|
354
|
|
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* 绑定验证 |
359
|
|
|
* @access public |
360
|
|
|
* @param mixed $validate 验证器类 |
361
|
|
|
* @param string $scene 验证场景 |
|
|
|
|
362
|
|
|
* @param array $message 验证提示 |
|
|
|
|
363
|
|
|
* @param bool $batch 批量验证 |
|
|
|
|
364
|
|
|
* @return $this |
365
|
|
|
*/ |
366
|
|
|
public function validate($validate, string $scene = null, array $message = [], bool $batch = false) |
367
|
|
|
{ |
368
|
|
|
$this->option['validate'] = [$validate, $scene, $message, $batch]; |
369
|
|
|
|
370
|
|
|
return $this; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* 指定路由中间件 |
375
|
|
|
* @access public |
376
|
|
|
* @param string|array|\Closure $middleware 中间件 |
377
|
|
|
* @param mixed $param 参数 |
|
|
|
|
378
|
|
|
* @return $this |
379
|
|
|
*/ |
380
|
|
|
public function middleware($middleware, $param = null) |
381
|
|
|
{ |
382
|
|
|
if (is_null($param) && is_array($middleware)) { |
383
|
|
|
$this->option['middleware'] = $middleware; |
384
|
|
|
} else { |
385
|
|
|
foreach ((array) $middleware as $item) { |
386
|
|
|
$this->option['middleware'][] = [$item, $param]; |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
return $this; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* 允许跨域 |
395
|
|
|
* @access public |
396
|
|
|
* @param array $header 自定义Header |
397
|
|
|
* @return $this |
398
|
|
|
*/ |
399
|
|
|
public function allowCrossDomain(array $header = []) |
400
|
|
|
{ |
401
|
|
|
return $this->middleware('\think\middleware\AllowCrossDomain', $header); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* 设置路由缓存 |
406
|
|
|
* @access public |
407
|
|
|
* @param array|string $cache 缓存 |
408
|
|
|
* @return $this |
409
|
|
|
*/ |
410
|
|
|
public function cache($cache) |
411
|
|
|
{ |
412
|
|
|
return $this->setOption('cache', $cache); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* 检查URL分隔符 |
417
|
|
|
* @access public |
418
|
|
|
* @param string $depr URL分隔符 |
419
|
|
|
* @return $this |
420
|
|
|
*/ |
421
|
|
|
public function depr(string $depr) |
422
|
|
|
{ |
423
|
|
|
return $this->setOption('param_depr', $depr); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* 设置需要合并的路由参数 |
428
|
|
|
* @access public |
429
|
|
|
* @param array $option 路由参数 |
430
|
|
|
* @return $this |
431
|
|
|
*/ |
432
|
|
|
public function mergeOptions(array $option = []) |
433
|
|
|
{ |
434
|
|
|
$this->mergeOptions = array_merge($this->mergeOptions, $option); |
435
|
|
|
return $this; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* 检查是否为HTTPS请求 |
440
|
|
|
* @access public |
441
|
|
|
* @param bool $https 是否为HTTPS |
442
|
|
|
* @return $this |
443
|
|
|
*/ |
444
|
|
|
public function https(bool $https = true) |
445
|
|
|
{ |
446
|
|
|
return $this->setOption('https', $https); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* 检查是否为JSON请求 |
451
|
|
|
* @access public |
452
|
|
|
* @param bool $json 是否为JSON |
453
|
|
|
* @return $this |
454
|
|
|
*/ |
455
|
|
|
public function json(bool $json = true) |
456
|
|
|
{ |
457
|
|
|
return $this->setOption('json', $json); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* 检查是否为AJAX请求 |
462
|
|
|
* @access public |
463
|
|
|
* @param bool $ajax 是否为AJAX |
464
|
|
|
* @return $this |
465
|
|
|
*/ |
466
|
|
|
public function ajax(bool $ajax = true) |
467
|
|
|
{ |
468
|
|
|
return $this->setOption('ajax', $ajax); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* 检查是否为PJAX请求 |
473
|
|
|
* @access public |
474
|
|
|
* @param bool $pjax 是否为PJAX |
475
|
|
|
* @return $this |
476
|
|
|
*/ |
477
|
|
|
public function pjax(bool $pjax = true) |
478
|
|
|
{ |
479
|
|
|
return $this->setOption('pjax', $pjax); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* 当前路由到一个模板地址 当使用数组的时候可以传入模板变量 |
484
|
|
|
* @access public |
485
|
|
|
* @param bool|array $view 视图 |
486
|
|
|
* @return $this |
487
|
|
|
*/ |
488
|
|
|
public function view($view = true) |
489
|
|
|
{ |
490
|
|
|
return $this->setOption('view', $view); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* 当前路由为重定向 |
495
|
|
|
* @access public |
496
|
|
|
* @param bool $redirect 是否为重定向 |
497
|
|
|
* @return $this |
498
|
|
|
*/ |
499
|
|
|
public function redirect(bool $redirect = true) |
500
|
|
|
{ |
501
|
|
|
return $this->setOption('redirect', $redirect); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* 设置status |
506
|
|
|
* @access public |
507
|
|
|
* @param int $status 状态码 |
508
|
|
|
* @return $this |
509
|
|
|
*/ |
510
|
|
|
public function status(int $status) |
511
|
|
|
{ |
512
|
|
|
return $this->setOption('status', $status); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* 设置路由完整匹配 |
517
|
|
|
* @access public |
518
|
|
|
* @param bool $match 是否完整匹配 |
519
|
|
|
* @return $this |
520
|
|
|
*/ |
521
|
|
|
public function completeMatch(bool $match = true) |
522
|
|
|
{ |
523
|
|
|
return $this->setOption('complete_match', $match); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* 是否去除URL最后的斜线 |
528
|
|
|
* @access public |
529
|
|
|
* @param bool $remove 是否去除最后斜线 |
530
|
|
|
* @return $this |
531
|
|
|
*/ |
532
|
|
|
public function removeSlash(bool $remove = true) |
533
|
|
|
{ |
534
|
|
|
return $this->setOption('remove_slash', $remove); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* 设置路由规则全局有效 |
539
|
|
|
* @access public |
540
|
|
|
* @return $this |
541
|
|
|
*/ |
542
|
|
|
public function crossDomainRule() |
543
|
|
|
{ |
544
|
|
|
if ($this instanceof RuleGroup) { |
545
|
|
|
$method = '*'; |
546
|
|
|
} else { |
547
|
|
|
$method = $this->method; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
$this->router->setCrossDomainRule($this, $method); |
551
|
|
|
|
552
|
|
|
return $this; |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* 合并分组参数 |
557
|
|
|
* @access public |
558
|
|
|
* @return array |
559
|
|
|
*/ |
560
|
|
|
public function mergeGroupOptions(): array |
561
|
|
|
{ |
562
|
|
|
$parentOption = $this->parent->getOption(); |
563
|
|
|
// 合并分组参数 |
564
|
|
|
foreach ($this->mergeOptions as $item) { |
565
|
|
|
if (isset($parentOption[$item]) && isset($this->option[$item])) { |
566
|
|
|
$this->option[$item] = array_merge($parentOption[$item], $this->option[$item]); |
567
|
|
|
} |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
$this->option = array_merge($parentOption, $this->option); |
571
|
|
|
|
572
|
|
|
return $this->option; |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* 解析匹配到的规则路由 |
577
|
|
|
* @access public |
578
|
|
|
* @param Request $request 请求对象 |
579
|
|
|
* @param string $rule 路由规则 |
|
|
|
|
580
|
|
|
* @param mixed $route 路由地址 |
|
|
|
|
581
|
|
|
* @param string $url URL地址 |
|
|
|
|
582
|
|
|
* @param array $option 路由参数 |
|
|
|
|
583
|
|
|
* @param array $matches 匹配的变量 |
584
|
|
|
* @return Dispatch |
585
|
|
|
*/ |
586
|
|
|
public function parseRule(Request $request, string $rule, $route, string $url, array $option = [], array $matches = []): Dispatch |
587
|
|
|
{ |
588
|
|
|
if (is_string($route) && isset($option['prefix'])) { |
589
|
|
|
// 路由地址前缀 |
590
|
|
|
$route = $option['prefix'] . $route; |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
// 替换路由地址中的变量 |
594
|
|
|
if (is_string($route) && !empty($matches)) { |
595
|
|
|
$search = $replace = []; |
596
|
|
|
|
597
|
|
|
foreach ($matches as $key => $value) { |
598
|
|
|
$search[] = '<' . $key . '>'; |
599
|
|
|
$replace[] = $value; |
600
|
|
|
|
601
|
|
|
$search[] = ':' . $key; |
602
|
|
|
$replace[] = $value; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
$route = str_replace($search, $replace, $route); |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
// 解析额外参数 |
609
|
|
|
$count = substr_count($rule, '/'); |
610
|
|
|
$url = array_slice(explode('|', $url), $count + 1); |
611
|
|
|
$this->parseUrlParams(implode('|', $url), $matches); |
612
|
|
|
|
613
|
|
|
$request->setRoute($matches); |
614
|
|
|
|
615
|
|
|
// 发起路由调度 |
616
|
|
|
return $this->dispatch($request, $route, $option); |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* 发起路由调度 |
621
|
|
|
* @access protected |
622
|
|
|
* @param Request $request Request对象 |
623
|
|
|
* @param mixed $route 路由地址 |
|
|
|
|
624
|
|
|
* @param array $option 路由参数 |
|
|
|
|
625
|
|
|
* @return Dispatch |
626
|
|
|
*/ |
627
|
|
|
protected function dispatch(Request $request, $route, array $option): Dispatch |
628
|
|
|
{ |
629
|
|
|
if ($route instanceof Dispatch) { |
630
|
|
|
$result = $route; |
631
|
|
|
} elseif ($route instanceof \Closure) { |
632
|
|
|
// 执行闭包 |
633
|
|
|
$result = new CallbackDispatch($request, $this, $route); |
634
|
|
|
} elseif ($route instanceof Response) { |
635
|
|
|
$result = new ResponseDispatch($request, $this, $route); |
636
|
|
|
} elseif (isset($option['view']) && false !== $option['view']) { |
637
|
|
|
$result = new ViewDispatch($request, $this, $route, is_array($option['view']) ? $option['view'] : []); |
638
|
|
|
} elseif (!empty($option['redirect']) || 0 === strpos($route, '/') || strpos($route, '://')) { |
639
|
|
|
// 路由到重定向地址 |
640
|
|
|
$result = new RedirectDispatch($request, $this, $route, [], $option['status'] ?? 301); |
641
|
|
|
} elseif (false !== strpos($route, '\\')) { |
642
|
|
|
// 路由到类的方法 |
643
|
|
|
$result = $this->dispatchMethod($request, $route); |
644
|
|
|
} else { |
645
|
|
|
// 路由到控制器/操作 |
646
|
|
|
$result = $this->dispatchController($request, $route); |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
return $result; |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* 解析URL地址为 模块/控制器/操作 |
654
|
|
|
* @access protected |
655
|
|
|
* @param Request $request Request对象 |
656
|
|
|
* @param string $route 路由地址 |
|
|
|
|
657
|
|
|
* @return CallbackDispatch |
658
|
|
|
*/ |
659
|
|
|
protected function dispatchMethod(Request $request, string $route): CallbackDispatch |
660
|
|
|
{ |
661
|
|
|
list($path, $var) = $this->parseUrlPath($route); |
662
|
|
|
|
663
|
|
|
$route = str_replace('/', '@', implode('/', $path)); |
664
|
|
|
$method = strpos($route, '@') ? explode('@', $route) : $route; |
665
|
|
|
|
666
|
|
|
return new CallbackDispatch($request, $this, $method, $var); |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* 解析URL地址为 模块/控制器/操作 |
671
|
|
|
* @access protected |
672
|
|
|
* @param Request $request Request对象 |
673
|
|
|
* @param string $route 路由地址 |
|
|
|
|
674
|
|
|
* @return ControllerDispatch |
675
|
|
|
*/ |
676
|
|
|
protected function dispatchController(Request $request, string $route): ControllerDispatch |
677
|
|
|
{ |
678
|
|
|
list($path, $var) = $this->parseUrlPath($route); |
679
|
|
|
|
680
|
|
|
$action = array_pop($path); |
681
|
|
|
$controller = !empty($path) ? array_pop($path) : null; |
682
|
|
|
|
683
|
|
|
// 路由到模块/控制器/操作 |
684
|
|
|
return new ControllerDispatch($request, $this, [$controller, $action], $var); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* 路由检查 |
689
|
|
|
* @access protected |
690
|
|
|
* @param array $option 路由参数 |
|
|
|
|
691
|
|
|
* @param Request $request Request对象 |
692
|
|
|
* @return bool |
693
|
|
|
*/ |
694
|
|
|
protected function checkOption(array $option, Request $request): bool |
695
|
|
|
{ |
696
|
|
|
// 请求类型检测 |
697
|
|
|
if (!empty($option['method'])) { |
698
|
|
|
if (is_string($option['method']) && false === stripos($option['method'], $request->method())) { |
699
|
|
|
return false; |
700
|
|
|
} |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
// AJAX PJAX 请求检查 |
704
|
|
|
foreach (['ajax', 'pjax', 'json'] as $item) { |
705
|
|
|
if (isset($option[$item])) { |
706
|
|
|
$call = 'is' . $item; |
707
|
|
|
if ($option[$item] && !$request->$call() || !$option[$item] && $request->$call()) { |
|
|
|
|
708
|
|
|
return false; |
709
|
|
|
} |
710
|
|
|
} |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
// 伪静态后缀检测 |
714
|
|
|
if ($request->url() != '/' && ((isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', '|' . $request->ext() . '|')) |
715
|
|
|
|| (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', '|' . $request->ext() . '|')))) { |
|
|
|
|
716
|
|
|
return false; |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
// 域名检查 |
720
|
|
|
if ((isset($option['domain']) && !in_array($option['domain'], [$request->host(true), $request->subDomain()]))) { |
721
|
|
|
return false; |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
// HTTPS检查 |
725
|
|
|
if ((isset($option['https']) && $option['https'] && !$request->isSsl()) |
726
|
|
|
|| (isset($option['https']) && !$option['https'] && $request->isSsl())) { |
|
|
|
|
727
|
|
|
return false; |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
// 请求参数检查 |
731
|
|
|
if (isset($option['filter'])) { |
732
|
|
|
foreach ($option['filter'] as $name => $value) { |
733
|
|
|
if ($request->param($name, '', null) != $value) { |
734
|
|
|
return false; |
735
|
|
|
} |
736
|
|
|
} |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
return true; |
740
|
|
|
} |
741
|
|
|
|
742
|
|
|
/** |
|
|
|
|
743
|
|
|
* 解析URL地址中的参数Request对象 |
744
|
|
|
* @access protected |
745
|
|
|
* @param string $rule 路由规则 |
|
|
|
|
746
|
|
|
* @param array $var 变量 |
|
|
|
|
747
|
|
|
* @return void |
748
|
|
|
*/ |
749
|
|
|
protected function parseUrlParams(string $url, array &$var = []): void |
750
|
|
|
{ |
751
|
|
|
if ($url) { |
752
|
|
|
preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) { |
|
|
|
|
753
|
|
|
$var[$match[1]] = strip_tags($match[2]); |
754
|
|
|
}, $url); |
|
|
|
|
755
|
|
|
} |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
/** |
759
|
|
|
* 解析URL的pathinfo参数和变量 |
760
|
|
|
* @access public |
761
|
|
|
* @param string $url URL地址 |
762
|
|
|
* @return array |
763
|
|
|
*/ |
764
|
|
|
public function parseUrlPath(string $url): array |
765
|
|
|
{ |
766
|
|
|
// 分隔符替换 确保路由定义使用统一的分隔符 |
767
|
|
|
$url = str_replace('|', '/', $url); |
768
|
|
|
$url = trim($url, '/'); |
769
|
|
|
$var = []; |
770
|
|
|
|
771
|
|
|
if (false !== strpos($url, '?')) { |
772
|
|
|
// [控制器/操作?]参数1=值1&参数2=值2... |
773
|
|
|
$info = parse_url($url); |
774
|
|
|
$path = explode('/', $info['path']); |
775
|
|
|
parse_str($info['query'], $var); |
776
|
|
|
} elseif (strpos($url, '/')) { |
777
|
|
|
// [控制器/操作] |
778
|
|
|
$path = explode('/', $url); |
779
|
|
|
} elseif (false !== strpos($url, '=')) { |
780
|
|
|
// 参数1=值1&参数2=值2... |
781
|
|
|
parse_str($url, $var); |
782
|
|
|
$path = []; |
783
|
|
|
} else { |
784
|
|
|
$path = [$url]; |
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
return [$path, $var]; |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
/** |
791
|
|
|
* 生成路由的正则规则 |
792
|
|
|
* @access protected |
793
|
|
|
* @param string $rule 路由规则 |
|
|
|
|
794
|
|
|
* @param array $match 匹配的变量 |
|
|
|
|
795
|
|
|
* @param array $pattern 路由变量规则 |
|
|
|
|
796
|
|
|
* @param array $option 路由参数 |
|
|
|
|
797
|
|
|
* @param bool $completeMatch 路由是否完全匹配 |
|
|
|
|
798
|
|
|
* @param string $suffix 路由正则变量后缀 |
|
|
|
|
799
|
|
|
* @return string |
800
|
|
|
*/ |
801
|
|
|
protected function buildRuleRegex(string $rule, array $match, array $pattern = [], array $option = [], bool $completeMatch = false, string $suffix = ''): string |
802
|
|
|
{ |
803
|
|
|
foreach ($match as $name) { |
804
|
|
|
$replace[] = $this->buildNameRegex($name, $pattern, $suffix); |
805
|
|
|
} |
806
|
|
|
|
807
|
|
|
// 是否区分 / 地址访问 |
808
|
|
|
if ('/' != $rule) { |
809
|
|
|
if (!empty($option['remove_slash'])) { |
810
|
|
|
$rule = rtrim($rule, '/'); |
811
|
|
|
} elseif (substr($rule, -1) == '/') { |
812
|
|
|
$rule = rtrim($rule, '/'); |
813
|
|
|
$hasSlash = true; |
814
|
|
|
} |
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
$regex = str_replace(array_unique($match), array_unique($replace), $rule); |
|
|
|
|
818
|
|
|
$regex = str_replace([')?/', ')/', ')?-', ')-', '\\\\/'], [')\/', ')\/', ')\-', ')\-', '\/'], $regex); |
819
|
|
|
|
820
|
|
|
if (isset($hasSlash)) { |
821
|
|
|
$regex .= '\/'; |
822
|
|
|
} |
823
|
|
|
|
824
|
|
|
return $regex . ($completeMatch ? '$' : ''); |
825
|
|
|
} |
826
|
|
|
|
827
|
|
|
/** |
828
|
|
|
* 生成路由变量的正则规则 |
829
|
|
|
* @access protected |
830
|
|
|
* @param string $name 路由变量 |
831
|
|
|
* @param array $pattern 变量规则 |
832
|
|
|
* @param string $suffix 路由正则变量后缀 |
833
|
|
|
* @return string |
834
|
|
|
*/ |
835
|
|
|
protected function buildNameRegex(string $name, array $pattern, string $suffix): string |
836
|
|
|
{ |
837
|
|
|
$optional = ''; |
838
|
|
|
$slash = substr($name, 0, 1); |
839
|
|
|
|
840
|
|
|
if (in_array($slash, ['/', '-'])) { |
841
|
|
|
$prefix = '\\' . $slash; |
842
|
|
|
$name = substr($name, 1); |
843
|
|
|
$slash = substr($name, 0, 1); |
844
|
|
|
} else { |
845
|
|
|
$prefix = ''; |
846
|
|
|
} |
847
|
|
|
|
848
|
|
|
if ('<' != $slash) { |
849
|
|
|
return $prefix . preg_quote($name, '/'); |
850
|
|
|
} |
851
|
|
|
|
852
|
|
|
if (strpos($name, '?')) { |
853
|
|
|
$name = substr($name, 1, -2); |
854
|
|
|
$optional = '?'; |
855
|
|
|
} elseif (strpos($name, '>')) { |
856
|
|
|
$name = substr($name, 1, -1); |
857
|
|
|
} |
858
|
|
|
|
859
|
|
|
if (isset($pattern[$name])) { |
860
|
|
|
$nameRule = $pattern[$name]; |
861
|
|
|
if (0 === strpos($nameRule, '/') && '/' == substr($nameRule, -1)) { |
862
|
|
|
$nameRule = substr($nameRule, 1, -1); |
863
|
|
|
} |
864
|
|
|
} else { |
865
|
|
|
$nameRule = $this->router->config('default_route_pattern'); |
866
|
|
|
} |
867
|
|
|
|
868
|
|
|
return '(' . $prefix . '(?<' . $name . $suffix . '>' . $nameRule . '))' . $optional; |
869
|
|
|
} |
870
|
|
|
|
871
|
|
|
/** |
872
|
|
|
* 分析路由规则中的变量 |
873
|
|
|
* @access protected |
874
|
|
|
* @param string $rule 路由规则 |
875
|
|
|
* @return array |
876
|
|
|
*/ |
877
|
|
|
protected function parseVar(string $rule): array |
878
|
|
|
{ |
879
|
|
|
// 提取路由规则中的变量 |
880
|
|
|
$var = []; |
881
|
|
|
|
882
|
|
|
if (preg_match_all('/<\w+\??>/', $rule, $matches)) { |
883
|
|
|
foreach ($matches[0] as $name) { |
884
|
|
|
$optional = false; |
885
|
|
|
|
886
|
|
|
if (strpos($name, '?')) { |
887
|
|
|
$name = substr($name, 1, -2); |
888
|
|
|
$optional = true; |
889
|
|
|
} else { |
890
|
|
|
$name = substr($name, 1, -1); |
891
|
|
|
} |
892
|
|
|
|
893
|
|
|
$var[$name] = $optional ? 2 : 1; |
894
|
|
|
} |
895
|
|
|
} |
896
|
|
|
|
897
|
|
|
return $var; |
898
|
|
|
} |
899
|
|
|
|
900
|
|
|
/** |
901
|
|
|
* 设置路由参数 |
902
|
|
|
* @access public |
903
|
|
|
* @param string $method 方法名 |
904
|
|
|
* @param array $args 调用参数 |
905
|
|
|
* @return $this |
906
|
|
|
*/ |
907
|
|
|
public function __call($method, $args) |
908
|
|
|
{ |
909
|
|
|
if (count($args) > 1) { |
910
|
|
|
$args[0] = $args; |
911
|
|
|
} |
912
|
|
|
array_unshift($args, $method); |
913
|
|
|
|
914
|
|
|
return call_user_func_array([$this, 'option'], $args); |
915
|
|
|
} |
916
|
|
|
|
917
|
|
|
public function __sleep() |
|
|
|
|
918
|
|
|
{ |
919
|
|
|
return ['name', 'rule', 'route', 'method', 'vars', 'option', 'pattern']; |
920
|
|
|
} |
921
|
|
|
|
922
|
|
|
public function __wakeup() |
|
|
|
|
923
|
|
|
{ |
924
|
|
|
$this->router = Container::pull('route'); |
925
|
|
|
} |
926
|
|
|
|
927
|
|
|
public function __debugInfo() |
|
|
|
|
928
|
|
|
{ |
929
|
|
|
return [ |
930
|
|
|
'name' => $this->name, |
931
|
|
|
'rule' => $this->rule, |
932
|
|
|
'route' => $this->route, |
933
|
|
|
'method' => $this->method, |
934
|
|
|
'vars' => $this->vars, |
935
|
|
|
'option' => $this->option, |
936
|
|
|
'pattern' => $this->pattern, |
937
|
|
|
]; |
938
|
|
|
} |
939
|
|
|
} |
940
|
|
|
|