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