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\helper\Str; |
16
|
|
|
use think\Request; |
17
|
|
|
use think\Route; |
18
|
|
|
use think\route\dispatch\Callback as CallbackDispatch; |
19
|
|
|
use think\route\dispatch\Controller as ControllerDispatch; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* 域名路由 |
23
|
|
|
*/ |
24
|
|
|
class Domain extends RuleGroup |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* 架构函数 |
28
|
|
|
* @access public |
29
|
|
|
* @param Route $router 路由对象 |
|
|
|
|
30
|
|
|
* @param string $name 路由域名 |
|
|
|
|
31
|
|
|
* @param mixed $rule 域名路由 |
|
|
|
|
32
|
|
|
*/ |
33
|
6 |
|
public function __construct(Route $router, string $name = null, $rule = null) |
34
|
|
|
{ |
35
|
6 |
|
$this->router = $router; |
36
|
6 |
|
$this->domain = $name; |
37
|
6 |
|
$this->rule = $rule; |
38
|
6 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* 检测域名路由 |
42
|
|
|
* @access public |
43
|
|
|
* @param Request $request 请求对象 |
|
|
|
|
44
|
|
|
* @param string $url 访问地址 |
|
|
|
|
45
|
|
|
* @param bool $completeMatch 路由是否完全匹配 |
|
|
|
|
46
|
|
|
* @return Dispatch|false |
47
|
|
|
*/ |
48
|
6 |
|
public function check(Request $request, string $url, bool $completeMatch = false) |
49
|
|
|
{ |
50
|
|
|
// 检测URL绑定 |
51
|
6 |
|
$result = $this->checkUrlBind($request, $url); |
52
|
|
|
|
53
|
6 |
|
if (!empty($this->option['append'])) { |
54
|
|
|
$request->setRoute($this->option['append']); |
55
|
|
|
unset($this->option['append']); |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
if (false !== $result) { |
59
|
|
|
return $result; |
60
|
|
|
} |
61
|
|
|
|
62
|
6 |
|
return parent::check($request, $url, $completeMatch); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* 设置路由绑定 |
67
|
|
|
* @access public |
68
|
|
|
* @param string $bind 绑定信息 |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function bind(string $bind) |
72
|
|
|
{ |
73
|
|
|
$this->router->bind($bind, $this->domain); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* 检测URL绑定 |
80
|
|
|
* @access private |
81
|
|
|
* @param Request $request |
|
|
|
|
82
|
|
|
* @param string $url URL地址 |
|
|
|
|
83
|
|
|
* @return Dispatch|false |
84
|
|
|
*/ |
85
|
6 |
|
private function checkUrlBind(Request $request, string $url) |
|
|
|
|
86
|
|
|
{ |
87
|
6 |
|
$bind = $this->router->getDomainBind($this->domain); |
88
|
|
|
|
89
|
6 |
|
if ($bind) { |
90
|
|
|
$this->parseBindAppendParam($bind); |
91
|
|
|
|
92
|
|
|
// 如果有URL绑定 则进行绑定检测 |
93
|
|
|
$type = substr($bind, 0, 1); |
94
|
|
|
$bind = substr($bind, 1); |
95
|
|
|
|
96
|
|
|
$bindTo = [ |
97
|
|
|
'\\' => 'bindToClass', |
98
|
|
|
'@' => 'bindToController', |
99
|
|
|
':' => 'bindToNamespace', |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
if (isset($bindTo[$type])) { |
103
|
|
|
return $this->{$bindTo[$type]}($request, $url, $bind); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
6 |
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function parseBindAppendParam(string &$bind): void |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
if (false !== strpos($bind, '?')) { |
113
|
|
|
[$bind, $query] = explode('?', $bind); |
114
|
|
|
parse_str($query, $vars); |
115
|
|
|
$this->append($vars); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* 绑定到类 |
121
|
|
|
* @access protected |
122
|
|
|
* @param Request $request |
|
|
|
|
123
|
|
|
* @param string $url URL地址 |
|
|
|
|
124
|
|
|
* @param string $class 类名(带命名空间) |
|
|
|
|
125
|
|
|
* @return CallbackDispatch |
126
|
|
|
*/ |
127
|
|
|
protected function bindToClass(Request $request, string $url, string $class): CallbackDispatch |
128
|
|
|
{ |
129
|
|
|
$array = explode('|', $url, 2); |
130
|
|
|
$action = !empty($array[0]) ? $array[0] : $this->router->config('default_action'); |
131
|
|
|
$param = []; |
132
|
|
|
|
133
|
|
|
if (!empty($array[1])) { |
134
|
|
|
$this->parseUrlParams($array[1], $param); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return new CallbackDispatch($request, $this, [$class, $action], $param); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* 绑定到命名空间 |
142
|
|
|
* @access protected |
143
|
|
|
* @param Request $request |
|
|
|
|
144
|
|
|
* @param string $url URL地址 |
|
|
|
|
145
|
|
|
* @param string $namespace 命名空间 |
146
|
|
|
* @return CallbackDispatch |
147
|
|
|
*/ |
148
|
|
|
protected function bindToNamespace(Request $request, string $url, string $namespace): CallbackDispatch |
149
|
|
|
{ |
150
|
|
|
$array = explode('|', $url, 3); |
151
|
|
|
$class = !empty($array[0]) ? $array[0] : $this->router->config('default_controller'); |
152
|
|
|
$method = !empty($array[1]) ? $array[1] : $this->router->config('default_action'); |
153
|
|
|
$param = []; |
154
|
|
|
|
155
|
|
|
if (!empty($array[2])) { |
156
|
|
|
$this->parseUrlParams($array[2], $param); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return new CallbackDispatch($request, $this, [$namespace . '\\' . Str::studly($class), $method], $param); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* 绑定到控制器 |
164
|
|
|
* @access protected |
165
|
|
|
* @param Request $request |
|
|
|
|
166
|
|
|
* @param string $url URL地址 |
|
|
|
|
167
|
|
|
* @param string $controller 控制器名 |
168
|
|
|
* @return ControllerDispatch |
169
|
|
|
*/ |
170
|
|
|
protected function bindToController(Request $request, string $url, string $controller): ControllerDispatch |
171
|
|
|
{ |
172
|
|
|
$array = explode('|', $url, 2); |
173
|
|
|
$action = !empty($array[0]) ? $array[0] : $this->router->config('default_action'); |
174
|
|
|
$param = []; |
175
|
|
|
|
176
|
|
|
if (!empty($array[1])) { |
177
|
|
|
$this->parseUrlParams($array[1], $param); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return new ControllerDispatch($request, $this, $controller . '/' . $action, $param); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|