Domain.php
6.95 KB
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
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\route;
use think\Container;
use think\Loader;
use think\Route;
use think\route\dispatch\Callback as CallbackDispatch;
use think\route\dispatch\Controller as ControllerDispatch;
use think\route\dispatch\Module as ModuleDispatch;
class Domain extends RuleGroup
{
protected $bind;
/**
* 架构函数
* @access public
* @param Route $router 路由对象
* @param string $name 路由域名
* @param mixed $rule 域名路由
* @param array $option 路由参数
* @param array $pattern 变量规则
*/
public function __construct(Route $router, $name = '', $rule = null, $option = [], $pattern = [])
{
$this->router = $router;
$this->domain = $name;
$this->option = $option;
$this->rule = $rule;
$this->pattern = $pattern;
}
/**
* 检测域名路由
* @access public
* @param Request $request 请求对象
* @param string $url 访问地址
* @param bool $completeMatch 路由是否完全匹配
* @return Dispatch|false
*/
public function check($request, $url, $completeMatch = false)
{
// 检测别名路由
$result = $this->checkRouteAlias($request, $url);
if (false !== $result) {
return $result;
}
// 检测URL绑定
$result = $this->checkUrlBind($request, $url);
if (!empty($this->option['append'])) {
$request->setRouteVars($this->option['append']);
unset($this->option['append']);
}
if (false !== $result) {
return $result;
}
// 添加域名中间件
if (!empty($this->option['middleware'])) {
Container::get('middleware')->import($this->option['middleware']);
unset($this->option['middleware']);
}
return parent::check($request, $url, $completeMatch);
}
/**
* 设置路由绑定
* @access public
* @param string $bind 绑定信息
* @return $this
*/
public function bind($bind)
{
$this->bind = $bind;
$this->router->bind($bind, $this->domain);
return $this;
}
/**
* 检测路由别名
* @access private
* @param Request $request
* @param string $url URL地址
* @return Dispatch|false
*/
private function checkRouteAlias($request, $url)
{
$alias = strpos($url, '|') ? strstr($url, '|', true) : $url;
$item = $this->router->getAlias($alias);
return $item ? $item->check($request, $url) : false;
}
/**
* 检测URL绑定
* @access private
* @param Request $request
* @param string $url URL地址
* @return Dispatch|false
*/
private function checkUrlBind($request, $url)
{
if (!empty($this->bind)) {
$bind = $this->bind;
$this->parseBindAppendParam($bind);
// 记录绑定信息
Container::get('app')->log('[ BIND ] ' . var_export($bind, true));
// 如果有URL绑定 则进行绑定检测
$type = substr($bind, 0, 1);
$bind = substr($bind, 1);
$bindTo = [
'\\' => 'bindToClass',
'@' => 'bindToController',
':' => 'bindToNamespace',
];
if (isset($bindTo[$type])) {
return $this->{$bindTo[$type]}($request, $url, $bind);
}
}
return false;
}
protected function parseBindAppendParam(&$bind)
{
if (false !== strpos($bind, '?')) {
list($bind, $query) = explode('?', $bind);
parse_str($query, $vars);
$this->append($vars);
}
}
/**
* 绑定到类
* @access protected
* @param Request $request
* @param string $url URL地址
* @param string $class 类名(带命名空间)
* @return CallbackDispatch
*/
protected function bindToClass($request, $url, $class)
{
$array = explode('|', $url, 2);
$action = !empty($array[0]) ? $array[0] : $this->router->config('default_action');
if (!empty($array[1])) {
$this->parseUrlParams($request, $array[1]);
}
return new CallbackDispatch($request, $this, [$class, $action]);
}
/**
* 绑定到命名空间
* @access protected
* @param Request $request
* @param string $url URL地址
* @param string $namespace 命名空间
* @return CallbackDispatch
*/
protected function bindToNamespace($request, $url, $namespace)
{
$array = explode('|', $url, 3);
$class = !empty($array[0]) ? $array[0] : $this->router->config('default_controller');
$method = !empty($array[1]) ? $array[1] : $this->router->config('default_action');
if (!empty($array[2])) {
$this->parseUrlParams($request, $array[2]);
}
return new CallbackDispatch($request, $this, [$namespace . '\\' . Loader::parseName($class, 1), $method]);
}
/**
* 绑定到控制器类
* @access protected
* @param Request $request
* @param string $url URL地址
* @param string $controller 控制器名 (支持带模块名 index/user )
* @return ControllerDispatch
*/
protected function bindToController($request, $url, $controller)
{
$array = explode('|', $url, 2);
$action = !empty($array[0]) ? $array[0] : $this->router->config('default_action');
if (!empty($array[1])) {
$this->parseUrlParams($request, $array[1]);
}
return new ControllerDispatch($request, $this, $controller . '/' . $action);
}
/**
* 绑定到模块/控制器
* @access protected
* @param Request $request
* @param string $url URL地址
* @param string $controller 控制器类名(带命名空间)
* @return ModuleDispatch
*/
protected function bindToModule($request, $url, $controller)
{
$array = explode('|', $url, 2);
$action = !empty($array[0]) ? $array[0] : $this->router->config('default_action');
if (!empty($array[1])) {
$this->parseUrlParams($request, $array[1]);
}
return new ModuleDispatch($request, $this, $controller . '/' . $action);
}
}