Completed
Push — 6.0 ( 9c5c55...31860d )
by liu
03:11
created

Url::parseDomain()   D

Complexity

Conditions 21
Paths 49

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 462

Importance

Changes 0
Metric Value
cc 21
eloc 30
nc 49
nop 2
dl 0
loc 51
ccs 0
cts 30
cp 0
crap 462
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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\App;
16
use think\Route;
17
18
/**
19
 * 路由地址生成
20
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
21
class Url
22
{
23
    /**
24
     * 应用对象
25
     * @var App
26
     */
27
    protected $app;
28
29
    /**
30
     * 路由对象
31
     * @var Route
32
     */
33
    protected $route;
34
35
    /**
36
     * URL变量
37
     * @var array
38
     */
39
    protected $vars = [];
40
41
    /**
42
     * 路由URL
43
     * @var string
44
     */
45
    protected $url;
46
47
    /**
48
     * URL 根地址
49
     * @var string
50
     */
51
    protected $root = '';
52
53
    /**
54
     * URL后缀
55
     * @var string|bool
56
     */
57
    protected $suffix = true;
58
59
    /**
60
     * URL域名
61
     * @var string|bool
62
     */
63
    protected $domain = false;
64
65
    /**
66
     * 显示域名
67
     * @var bool
68
     */
69
    protected $showDomain;
70
71
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $route should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $app should have a doc-comment as per coding-style.
Loading history...
72
     * 架构函数
73
     * @access public
74
     * @param  string $url URL地址
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $url does not match actual variable name $route
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
75
     * @param  array  $vars 参数
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $vars does not match actual variable name $app
Loading history...
76
     */
77
    public function __construct(Route $route, App $app, string $url = '', array $vars = [])
78
    {
79
        $this->route = $route;
80
        $this->app   = $app;
81
        $this->url   = $url;
82
        $this->vars  = $vars;
83
    }
84
85
    /**
86
     * 设置URL参数
87
     * @access public
88
     * @param  array $vars URL参数
89
     * @return $this
90
     */
91
    public function vars(array $vars = [])
92
    {
93
        $this->vars = $vars;
94
        return $this;
95
    }
96
97
    /**
98
     * 设置URL后缀
99
     * @access public
100
     * @param  string|bool $suffix URL后缀
101
     * @return $this
102
     */
103
    public function suffix($suffix)
104
    {
105
        $this->suffix = $suffix;
106
        return $this;
107
    }
108
109
    /**
110
     * 设置URL域名(或者子域名)
111
     * @access public
112
     * @param  string|bool $domain URL域名
113
     * @return $this
114
     */
115
    public function domain($domain)
116
    {
117
        $this->domain = $domain;
118
        return $this;
119
    }
120
121
    /**
122
     * 设置URL 根地址
123
     * @access public
124
     * @param  string $root URL root
125
     * @return $this
126
     */
127
    public function root(string $root)
128
    {
129
        $this->root = $root;
130
        return $this;
131
    }
132
133
    /**
134
     * 检测域名
135
     * @access protected
136
     * @param  string      $url URL
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
137
     * @param  string|true $domain 域名
138
     * @return string
139
     */
140
    protected function parseDomain(string &$url, $domain): string
141
    {
142
        if (!$domain) {
143
            return '';
144
        }
145
146
        $request    = $this->app->request;
147
        $rootDomain = $request->rootDomain();
148
149
        if (true === $domain) {
150
            // 自动判断域名
151
            $domain  = $request->host();
152
            $domains = $this->route->getDomains();
153
154
            if (!empty($domains)) {
155
                $route_domain = array_keys($domains);
156
                foreach ($route_domain as $domain_prefix) {
157
                    if (0 === strpos($domain_prefix, '*.') && strpos($domain, ltrim($domain_prefix, '*.')) !== false) {
158
                        foreach ($domains as $key => $rule) {
159
                            $rule = is_array($rule) ? $rule[0] : $rule;
160
                            if (is_string($rule) && false === strpos($key, '*') && 0 === strpos($url, $rule)) {
161
                                $url    = ltrim($url, $rule);
162
                                $domain = $key;
163
164
                                // 生成对应子域名
165
                                if (!empty($rootDomain)) {
166
                                    $domain .= $rootDomain;
167
                                }
168
                                break;
169
                            } elseif (false !== strpos($key, '*')) {
170
                                if (!empty($rootDomain)) {
171
                                    $domain .= $rootDomain;
172
                                }
173
174
                                break;
175
                            }
176
                        }
177
                    }
178
                }
179
            }
180
        } elseif (false === strpos($domain, '.') && 0 !== strpos($domain, $rootDomain)) {
181
            $domain .= '.' . $rootDomain;
182
        }
183
184
        if (false !== strpos($domain, '://')) {
185
            $scheme = '';
186
        } else {
187
            $scheme = $request->isSsl() ? 'https://' : 'http://';
188
        }
189
190
        return $request->host() == $domain && !$this->showDomain ? '' : $scheme . $domain;
191
    }
192
193
    /**
194
     * 解析URL后缀
195
     * @access protected
196
     * @param  string|bool $suffix 后缀
197
     * @return string
198
     */
199
    protected function parseSuffix($suffix): string
200
    {
201
        if ($suffix) {
202
            $suffix = true === $suffix ? $this->route->config('url_html_suffix') : $suffix;
203
204
            if ($pos = strpos($suffix, '|')) {
205
                $suffix = substr($suffix, 0, $pos);
206
            }
207
        }
208
209
        return (empty($suffix) || 0 === strpos($suffix, '.')) ? (string) $suffix : '.' . $suffix;
210
    }
211
212
    /**
213
     * 直接解析URL地址
214
     * @access protected
215
     * @param  string      $url URL
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
216
     * @param  string|bool $domain Domain
217
     * @return string
218
     */
219
    protected function parseUrl(string $url, &$domain): string
220
    {
221
        $request = $this->app->request;
222
223
        if (0 === strpos($url, '/')) {
224
            // 直接作为路由地址解析
225
            $url = substr($url, 1);
226
        } elseif (false !== strpos($url, '\\')) {
227
            // 解析到类
228
            $url = ltrim(str_replace('\\', '/', $url), '/');
229
        } elseif (0 === strpos($url, '@')) {
230
            // 解析到控制器
231
            $url = substr($url, 1);
232
        } else {
233
            // 解析到 应用/控制器/操作
234
            $app        = $request->app();
235
            $controller = $request->controller();
236
237
            if ('' == $url) {
238
                $action = $request->action();
239
            } else {
240
                $path       = explode('/', $url);
241
                $action     = array_pop($path);
242
                $controller = empty($path) ? $controller : array_pop($path);
243
                $app        = empty($path) ? $app : array_pop($path);
244
            }
245
246
            if ($this->route->config('url_convert')) {
247
                $action     = strtolower($action);
248
                $controller = App::parseName($controller);
249
            }
250
251
            $url = $controller . '/' . $action;
252
253
            if ($app && $this->app->config->get('app.auto_multi_app')) {
254
                $bind = $this->app->config->get('app.domain_bind', []);
255
                if ($key = array_search($app, $bind)) {
256
                    $domain = true === $domain ? $key : $domain;
257
                } else {
258
                    $map = $this->app->config->get('app.app_map', []);
259
260
                    if ($key = array_search($app, $map)) {
261
                        $url = $key . '/' . $url;
262
                    } else {
263
                        $url = $app . '/' . $url;
264
                    }
265
                }
266
            }
267
        }
268
269
        return $url;
270
    }
271
272
    /**
273
     * 分析路由规则中的变量
274
     * @access protected
275
     * @param  string $rule 路由规则
276
     * @return array
277
     */
278
    protected function parseVar(string $rule): array
279
    {
280
        // 提取路由规则中的变量
281
        $var = [];
282
283
        if (preg_match_all('/<\w+\??>/', $rule, $matches)) {
284
            foreach ($matches[0] as $name) {
285
                $optional = false;
286
287
                if (strpos($name, '?')) {
288
                    $name     = substr($name, 1, -2);
289
                    $optional = true;
290
                } else {
291
                    $name = substr($name, 1, -1);
292
                }
293
294
                $var[$name] = $optional ? 2 : 1;
295
            }
296
        }
297
298
        return $var;
299
    }
300
301
    /**
302
     * 匹配路由地址
303
     * @access protected
304
     * @param  array $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 1 found
Loading history...
305
     * @param  array $vars 路由变量
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 1 found
Loading history...
306
     * @param  mixed $allowDomain 允许域名
307
     * @return array
308
     */
309
    protected function getRuleUrl(array $rule, array &$vars = [], $allowDomain = ''): array
310
    {
311
        $request = $this->app->request;
312
313
        foreach ($rule as $item) {
314
            $url     = $item->getRule();
315
            $pattern = $this->parseVar($url);
316
            $domain  = $item->getDomain();
317
            $suffix  = $item->getSuffix();
318
319
            if ('-' == $domain) {
320
                $domain = $request->host(true);
321
            }
322
323
            if (is_string($allowDomain) && $domain != $allowDomain) {
324
                continue;
325
            }
326
327
            if (!in_array($request->port(), [80, 443])) {
328
                $domain .= ':' . $request->port();
329
            }
330
331
            if (empty($pattern)) {
332
                return [rtrim($url, '?/-'), $domain, $suffix];
333
            }
334
335
            $type = $this->route->config('url_common_param');
336
337
            foreach ($pattern as $key => $val) {
338
                if (isset($vars[$key])) {
339
                    $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key, '<' . $key . '>'], $type ? $vars[$key] : urlencode((string) $vars[$key]), $url);
340
                    unset($vars[$key]);
341
                    $url    = str_replace(['/?', '-?'], ['/', '-'], $url);
342
                    $result = [rtrim($url, '?/-'), $domain, $suffix];
343
                } elseif (2 == $val) {
344
                    $url    = str_replace(['/[:' . $key . ']', '[:' . $key . ']', '<' . $key . '?>'], '', $url);
345
                    $url    = str_replace(['/?', '-?'], ['/', '-'], $url);
346
                    $result = [rtrim($url, '?/-'), $domain, $suffix];
347
                } else {
348
                    break;
349
                }
350
            }
351
352
            if (isset($result)) {
353
                return $result;
354
            }
355
        }
356
357
        return [];
358
    }
359
360
    public function build()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function build()
Loading history...
361
    {
362
        // 解析URL
363
        $url     = $this->url;
364
        $suffix  = $this->suffix;
365
        $domain  = $this->domain;
366
        $request = $this->app->request;
367
        $vars    = $this->vars;
368
369
        if (0 === strpos($url, '[') && $pos = strpos($url, ']')) {
370
            // [name] 表示使用路由命名标识生成URL
371
            $name = substr($url, 1, $pos - 1);
372
            $url  = 'name' . substr($url, $pos + 1);
373
        }
374
375
        if (false === strpos($url, '://') && 0 !== strpos($url, '/')) {
376
            $info = parse_url($url);
377
            $url  = !empty($info['path']) ? $info['path'] : '';
378
379
            if (isset($info['fragment'])) {
380
                // 解析锚点
381
                $anchor = $info['fragment'];
382
383
                if (false !== strpos($anchor, '?')) {
384
                    // 解析参数
385
                    list($anchor, $info['query']) = explode('?', $anchor, 2);
386
                }
387
388
                if (false !== strpos($anchor, '@')) {
389
                    // 解析域名
390
                    list($anchor, $domain) = explode('@', $anchor, 2);
391
                }
392
            } elseif (strpos($url, '@') && false === strpos($url, '\\')) {
393
                // 解析域名
394
                list($url, $domain) = explode('@', $url, 2);
395
            }
396
        }
397
398
        $this->showDomain = false === $domain ? false : true;
399
400
        if ($url) {
401
            $checkName   = isset($name) ? $name : $url . (isset($info['query']) ? '?' . $info['query'] : '');
402
            $checkDomain = $domain && is_string($domain) ? $domain : null;
403
404
            $rule = $this->route->getName($checkName, $checkDomain);
405
406
            if (empty($rule) && isset($info['query'])) {
407
                $rule = $this->route->getName($url, $checkDomain);
408
                // 解析地址里面参数 合并到vars
409
                parse_str($info['query'], $params);
410
                $vars = array_merge($params, $vars);
411
                unset($info['query']);
412
            }
413
        }
414
415
        if (!empty($rule) && $match = $this->getRuleUrl($rule, $vars, $domain)) {
416
            // 匹配路由命名标识
417
            $url = $match[0];
418
419
            if (!empty($match[1])) {
420
                $domain = $match[1];
421
            }
422
423
            if (!is_null($match[2])) {
424
                $suffix = $match[2];
425
            }
426
427
            if ($request->app() && $this->app->config->get('app.auto_multi_app') && !$this->app->http->isBindDomain()) {
428
                $url = $request->app() . '/' . $url;
429
            }
430
        } elseif (!empty($rule) && isset($name)) {
431
            throw new \InvalidArgumentException('route name not exists:' . $name);
432
        } else {
433
            // 检测URL绑定
434
            $bind = $this->route->getDomainBind($domain && is_string($domain) ? $domain : null);
435
436
            if ($bind && 0 === strpos($url, $bind)) {
437
                $url = substr($url, strlen($bind) + 1);
438
            } else {
439
                $binds = $this->route->getBind();
440
441
                foreach ($binds as $key => $val) {
442
                    if (is_string($val) && 0 === strpos($url, $val) && substr_count($val, '/') > 1) {
443
                        $url    = substr($url, strlen($val) + 1);
444
                        $domain = $key;
445
                        break;
446
                    }
447
                }
448
            }
449
450
            // 路由标识不存在 直接解析
451
            $url = $this->parseUrl($url, $domain);
452
453
            if (isset($info['query'])) {
454
                // 解析地址里面参数 合并到vars
455
                parse_str($info['query'], $params);
456
                $vars = array_merge($params, $vars);
457
            }
458
        }
459
460
        // 还原URL分隔符
461
        $depr = $this->route->config('pathinfo_depr');
462
        $url  = str_replace('/', $depr, $url);
463
464
        $file = $request->baseFile();
465
        if ($file && 0 !== strpos($request->url(), $file)) {
466
            $file = str_replace('\\', '/', dirname($file));
467
        }
468
469
        $url = rtrim($file, '/') . '/' . $url;
470
471
        // URL后缀
472
        if ('/' == substr($url, -1) || '' == $url) {
473
            $suffix = '';
474
        } else {
475
            $suffix = $this->parseSuffix($suffix);
476
        }
477
478
        // 锚点
479
        $anchor = !empty($anchor) ? '#' . $anchor : '';
480
481
        // 参数组装
482
        if (!empty($vars)) {
483
            // 添加参数
484
            if ($this->route->config('url_common_param')) {
485
                $vars = http_build_query($vars);
486
                $url .= $suffix . '?' . $vars . $anchor;
487
            } else {
488
                foreach ($vars as $var => $val) {
489
                    if ('' !== $val) {
490
                        $url .= $depr . $var . $depr . urlencode((string) $val);
491
                    }
492
                }
493
494
                $url .= $suffix . $anchor;
495
            }
496
        } else {
497
            $url .= $suffix . $anchor;
498
        }
499
500
        // 检测域名
501
        $domain = $this->parseDomain($url, $domain);
502
503
        // URL组装
504
        return $domain . rtrim($this->root, '/') . '/' . ltrim($url, '/');
505
    }
506
507
    public function __toString()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __toString()
Loading history...
508
    {
509
        return $this->build();
510
    }
511
}
512