Url::build()   F
last analyzed

Complexity

Conditions 44
Paths > 20000

Size

Total Lines 140
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 1980

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 44
eloc 76
c 2
b 0
f 0
nc 330330
nop 0
dl 0
loc 140
ccs 0
cts 72
cp 0
crap 1980
rs 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
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2021 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
 */
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
     * HTTPS
55
     * @var bool
56
     */
57
    protected $https;
58
59
    /**
60
     * URL后缀
61
     * @var string|bool
62
     */
63
    protected $suffix = true;
64
65
    /**
66
     * URL域名
67
     * @var string|bool
68
     */
69
    protected $domain = false;
70
71
    /**
72
     * 架构函数
73
     * @access public
74
     * @param  string $url URL地址
75
     * @param  array  $vars 参数
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
     * 设置是否使用HTTPS
135
     * @access public
136
     * @param  bool $https
137
     * @return $this
138
     */
139
    public function https(bool $https = true)
140
    {
141
        $this->https = $https;
142
        return $this;
143
    }
144
145
    /**
146
     * 检测域名
147
     * @access protected
148
     * @param  string      $url URL
149
     * @param  string|true $domain 域名
150
     * @return string
151
     */
152
    protected function parseDomain(string &$url, $domain): string
153
    {
154
        if (!$domain) {
155
            return '';
156
        }
157
158
        $request    = $this->app->request;
159
        $rootDomain = $request->rootDomain();
160
161
        if (true === $domain) {
162
            // 自动判断域名
163
            $domain  = $request->host();
164
            $domains = $this->route->getDomains();
165
166
            if (!empty($domains)) {
167
                $routeDomain = array_keys($domains);
168
                foreach ($routeDomain as $domainPrefix) {
169
                    if (0 === strpos($domainPrefix, '*.') && strpos($domain, ltrim($domainPrefix, '*.')) !== false) {
170
                        foreach ($domains as $key => $rule) {
171
                            $rule = is_array($rule) ? $rule[0] : $rule;
172
                            if (is_string($rule) && false === strpos($key, '*') && 0 === strpos($url, $rule)) {
173
                                $url    = ltrim($url, $rule);
174
                                $domain = $key;
175
176
                                // 生成对应子域名
177
                                if (!empty($rootDomain)) {
178
                                    $domain .= $rootDomain;
179
                                }
180
                                break;
181
                            } elseif (false !== strpos($key, '*')) {
182
                                if (!empty($rootDomain)) {
183
                                    $domain .= $rootDomain;
184
                                }
185
186
                                break;
187
                            }
188
                        }
189
                    }
190
                }
191
            }
192
        } elseif (false === strpos($domain, '.') && 0 !== strpos($domain, $rootDomain)) {
193
            $domain .= '.' . $rootDomain;
194
        }
195
196
        if (false !== strpos($domain, '://')) {
197
            $scheme = '';
198
        } else {
199
            $scheme = $this->https || $request->isSsl() ? 'https://' : 'http://';
200
        }
201
202
        return $scheme . $domain;
203
    }
204
205
    /**
206
     * 解析URL后缀
207
     * @access protected
208
     * @param  string|bool $suffix 后缀
209
     * @return string
210
     */
211
    protected function parseSuffix($suffix): string
212
    {
213
        if ($suffix) {
214
            $suffix = true === $suffix ? $this->route->config('url_html_suffix') : $suffix;
215
216
            if (is_string($suffix) && $pos = strpos($suffix, '|')) {
217
                $suffix = substr($suffix, 0, $pos);
218
            }
219
        }
220
221
        return (empty($suffix) || 0 === strpos($suffix, '.')) ? (string) $suffix : '.' . $suffix;
222
    }
223
224
    /**
225
     * 直接解析URL地址
226
     * @access protected
227
     * @param  string      $url URL
228
     * @param  string|bool $domain Domain
229
     * @return string
230
     */
231
    protected function parseUrl(string $url, &$domain): string
232
    {
233
        $request = $this->app->request;
234
235
        if (0 === strpos($url, '/')) {
236
            // 直接作为路由地址解析
237
            $url = substr($url, 1);
238
        } elseif (false !== strpos($url, '\\')) {
239
            // 解析到类
240
            $url = ltrim(str_replace('\\', '/', $url), '/');
241
        } elseif (0 === strpos($url, '@')) {
242
            // 解析到控制器
243
            $url = substr($url, 1);
244
        } elseif ('' === $url) {
245
            $url = $request->controller() . '/' . $request->action();
246
        } else {
247
            $controller = $request->controller();
248
249
            $path       = explode('/', $url);
250
            $action     = array_pop($path);
251
            $controller = empty($path) ? $controller : array_pop($path);
252
253
            $url = $controller . '/' . $action;
254
        }
255
256
        return $url;
257
    }
258
259
    /**
260
     * 分析路由规则中的变量
261
     * @access protected
262
     * @param  string $rule 路由规则
263
     * @return array
264
     */
265
    protected function parseVar(string $rule): array
266
    {
267
        // 提取路由规则中的变量
268
        $var = [];
269
270
        if (preg_match_all('/<\w+\??>/', $rule, $matches)) {
271
            foreach ($matches[0] as $name) {
272
                $optional = false;
273
274
                if (strpos($name, '?')) {
275
                    $name     = substr($name, 1, -2);
276
                    $optional = true;
277
                } else {
278
                    $name = substr($name, 1, -1);
279
                }
280
281
                $var[$name] = $optional ? 2 : 1;
282
            }
283
        }
284
285
        return $var;
286
    }
287
288
    /**
289
     * 匹配路由地址
290
     * @access protected
291
     * @param  array $rule 路由规则
292
     * @param  array $vars 路由变量
293
     * @param  mixed $allowDomain 允许域名
294
     * @return array
295
     */
296
    protected function getRuleUrl(array $rule, array &$vars = [], $allowDomain = ''): array
297
    {
298
        $request = $this->app->request;
299
        if (is_string($allowDomain) && false === strpos($allowDomain, '.')) {
300
            $allowDomain .= '.' . $request->rootDomain();
301
        }
302
        $port = $request->port();
303
304
        foreach ($rule as $item) {
305
            $url     = $item['rule'];
306
            $pattern = $this->parseVar($url);
307
            $domain  = $item['domain'];
308
            $suffix  = $item['suffix'];
309
310
            if ('-' == $domain) {
311
                $domain = is_string($allowDomain) ? $allowDomain : $request->host(true);
312
            }
313
314
            if (is_string($allowDomain) && $domain != $allowDomain) {
315
                continue;
316
            }
317
318
            if ($port && !in_array($port, [80, 443])) {
319
                $domain .= ':' . $port;
320
            }
321
322
            if (empty($pattern)) {
323
                return [rtrim($url, '?-'), $domain, $suffix];
324
            }
325
326
            $type = $this->route->config('url_common_param');
327
            $keys = [];
328
329
            foreach ($pattern as $key => $val) {
330
                if (isset($vars[$key])) {
331
                    $url    = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key, '<' . $key . '>'], $type ? (string) $vars[$key] : urlencode((string) $vars[$key]), $url);
332
                    $keys[] = $key;
333
                    $url    = str_replace(['/?', '-?'], ['/', '-'], $url);
334
                    $result = [rtrim($url, '?-'), $domain, $suffix];
335
                } elseif (2 == $val) {
336
                    $url    = str_replace(['/[:' . $key . ']', '[:' . $key . ']', '<' . $key . '?>'], '', $url);
337
                    $url    = str_replace(['/?', '-?'], ['/', '-'], $url);
338
                    $result = [rtrim($url, '?-'), $domain, $suffix];
339
                } else {
340
                    $result = null;
341
                    $keys   = [];
342
                    break;
343
                }
344
            }
345
346
            $vars = array_diff_key($vars, array_flip($keys));
347
348
            if (isset($result)) {
349
                return $result;
350
            }
351
        }
352
353
        return [];
354
    }
355
356
    /**
357
     * 生成URL地址
358
     * @access public
359
     * @return string
360
     */
361
    public function build(): string
362
    {
363
        // 解析URL
364
        $url     = $this->url;
365
        $suffix  = $this->suffix;
366
        $domain  = $this->domain;
367
        $request = $this->app->request;
368
        $vars    = $this->vars;
369
370
        if (0 === strpos($url, '[') && $pos = strpos($url, ']')) {
371
            // [name] 表示使用路由命名标识生成URL
372
            $name = substr($url, 1, $pos - 1);
373
            $url  = 'name' . substr($url, $pos + 1);
374
        }
375
376
        if (false === strpos($url, '://') && 0 !== strpos($url, '/')) {
377
            $info = parse_url($url);
378
            $url  = !empty($info['path']) ? $info['path'] : '';
379
380
            if (isset($info['fragment'])) {
381
                // 解析锚点
382
                $anchor = $info['fragment'];
383
384
                if (false !== strpos($anchor, '?')) {
385
                    // 解析参数
386
                    [$anchor, $info['query']] = explode('?', $anchor, 2);
387
                }
388
389
                if (false !== strpos($anchor, '@')) {
390
                    // 解析域名
391
                    [$anchor, $domain] = explode('@', $anchor, 2);
392
                }
393
            } elseif (strpos($url, '@') && false === strpos($url, '\\')) {
394
                // 解析域名
395
                [$url, $domain] = explode('@', $url, 2);
396
            }
397
        }
398
399
        if ($url) {
400
            $checkName   = isset($name) ? $name : $url . (isset($info['query']) ? '?' . $info['query'] : '');
401
            $checkDomain = $domain && is_string($domain) ? $domain : null;
402
403
            $rule = $this->route->getName($checkName, $checkDomain);
404
405
            if (empty($rule) && isset($info['query'])) {
406
                $rule = $this->route->getName($url, $checkDomain);
407
                // 解析地址里面参数 合并到vars
408
                parse_str($info['query'], $params);
409
                $vars = array_merge($params, $vars);
410
                unset($info['query']);
411
            }
412
        }
413
414
        if (!empty($rule) && $match = $this->getRuleUrl($rule, $vars, $domain)) {
415
            // 匹配路由命名标识
416
            $url = $match[0];
417
418
            if ($domain && !empty($match[1])) {
419
                $domain = $match[1];
420
            }
421
422
            if (!is_null($match[2])) {
423
                $suffix = $match[2];
424
            }
425
        } elseif (!empty($rule) && isset($name)) {
426
            throw new \InvalidArgumentException('route name not exists:' . $name);
427
        } else {
428
            // 检测URL绑定
429
            $bind = $this->route->getDomainBind($domain && is_string($domain) ? $domain : null);
430
431
            if ($bind && 0 === strpos($url, $bind)) {
432
                $url = substr($url, strlen($bind) + 1);
433
            } else {
434
                $binds = $this->route->getBind();
435
436
                foreach ($binds as $key => $val) {
437
                    if (is_string($val) && 0 === strpos($url, $val) && substr_count($val, '/') > 1) {
438
                        $url    = substr($url, strlen($val) + 1);
439
                        $domain = $key;
440
                        break;
441
                    }
442
                }
443
            }
444
445
            // 路由标识不存在 直接解析
446
            $url = $this->parseUrl($url, $domain);
447
448
            if (isset($info['query'])) {
449
                // 解析地址里面参数 合并到vars
450
                parse_str($info['query'], $params);
451
                $vars = array_merge($params, $vars);
452
            }
453
        }
454
455
        // 还原URL分隔符
456
        $depr = $this->route->config('pathinfo_depr');
457
        $url  = str_replace('/', $depr, $url);
458
459
        $file = $request->baseFile();
460
        if ($file && 0 !== strpos($request->url(), $file)) {
461
            $file = str_replace('\\', '/', dirname($file));
462
        }
463
464
        $url = rtrim($file, '/') . '/' . $url;
465
466
        // URL后缀
467
        if ('/' == substr($url, -1) || '' == $url) {
468
            $suffix = '';
469
        } else {
470
            $suffix = $this->parseSuffix($suffix);
471
        }
472
473
        // 锚点
474
        $anchor = !empty($anchor) ? '#' . $anchor : '';
475
476
        // 参数组装
477
        if (!empty($vars)) {
478
            // 添加参数
479
            if ($this->route->config('url_common_param')) {
480
                $vars = http_build_query($vars);
481
                $url .= $suffix . ($vars ? '?' . $vars : '') . $anchor;
482
            } else {
483
                foreach ($vars as $var => $val) {
484
                    $val = (string) $val;
485
                    if ('' !== $val) {
486
                        $url .= $depr . $var . $depr . urlencode($val);
487
                    }
488
                }
489
490
                $url .= $suffix . $anchor;
491
            }
492
        } else {
493
            $url .= $suffix . $anchor;
494
        }
495
496
        // 检测域名
497
        $domain = $this->parseDomain($url, $domain);
498
499
        // URL组装
500
        return $domain . rtrim($this->root, '/') . '/' . ltrim($url, '/');
501
    }
502
503
    public function __toString()
504
    {
505
        return $this->build();
506
    }
507
508
    public function __debugInfo()
509
    {
510
        return [
511
            'url'    => $this->url,
512
            'vars'   => $this->vars,
513
            'suffix' => $this->suffix,
514
            'domain' => $this->domain,
515
        ];
516
    }
517
}
518