Completed
Push — 6.0 ( 40cdd9...30a204 )
by liu
06:40
created

Url::suffix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
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\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
    /**
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
     * 设置是否使用HTTPS
135
     * @access public
136
     * @param  bool $https
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
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
                $route_domain = array_keys($domains);
168
                foreach ($route_domain as $domain_prefix) {
169
                    if (0 === strpos($domain_prefix, '*.') && strpos($domain, ltrim($domain_prefix, '*.')) !== 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 ($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
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
228
     * @param  string|bool $domain Domain
229
     * @return string
230
     */
231
    protected function parseUrl(string $url, &$domain): string
232
    {
233
        $request = $this->app->request;
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
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
        }
245
246
        return $url;
247
    }
248
249
    /**
250
     * 分析路由规则中的变量
251
     * @access protected
252
     * @param  string $rule 路由规则
253
     * @return array
254
     */
255
    protected function parseVar(string $rule): array
256
    {
257
        // 提取路由规则中的变量
258
        $var = [];
259
260
        if (preg_match_all('/<\w+\??>/', $rule, $matches)) {
261
            foreach ($matches[0] as $name) {
262
                $optional = false;
263
264
                if (strpos($name, '?')) {
265
                    $name     = substr($name, 1, -2);
266
                    $optional = true;
267
                } else {
268
                    $name = substr($name, 1, -1);
269
                }
270
271
                $var[$name] = $optional ? 2 : 1;
272
            }
273
        }
274
275
        return $var;
276
    }
277
278
    /**
279
     * 匹配路由地址
280
     * @access protected
281
     * @param  array $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 1 found
Loading history...
282
     * @param  array $vars 路由变量
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 1 found
Loading history...
283
     * @param  mixed $allowDomain 允许域名
284
     * @return array
285
     */
286
    protected function getRuleUrl(array $rule, array &$vars = [], $allowDomain = ''): array
287
    {
288
        $request = $this->app->request;
289
        if (is_string($allowDomain) && false === strpos($allowDomain, '.')) {
290
            $allowDomain .= '.' . $request->rootDomain();
291
        }
292
293
        foreach ($rule as $item) {
294
            $url     = $item->getRule();
295
            $pattern = $this->parseVar($url);
296
            $domain  = $item->getDomain();
297
            $suffix  = $item->getSuffix();
298
299
            if ('-' == $domain) {
300
                $domain = is_string($allowDomain) ? $allowDomain : $request->host(true);
301
            }
302
303
            if (is_string($allowDomain) && $domain != $allowDomain) {
304
                continue;
305
            }
306
307
            if (!in_array($request->port(), [80, 443])) {
308
                $domain .= ':' . $request->port();
309
            }
310
311
            if (empty($pattern)) {
312
                return [rtrim($url, '?/-'), $domain, $suffix];
313
            }
314
315
            $type = $this->route->config('url_common_param');
316
317
            foreach ($pattern as $key => $val) {
318
                if (isset($vars[$key])) {
319
                    $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key, '<' . $key . '>'], $type ? $vars[$key] : urlencode((string) $vars[$key]), $url);
320
                    unset($vars[$key]);
321
                    $url    = str_replace(['/?', '-?'], ['/', '-'], $url);
322
                    $result = [rtrim($url, '?/-'), $domain, $suffix];
323
                } elseif (2 == $val) {
324
                    $url    = str_replace(['/[:' . $key . ']', '[:' . $key . ']', '<' . $key . '?>'], '', $url);
325
                    $url    = str_replace(['/?', '-?'], ['/', '-'], $url);
326
                    $result = [rtrim($url, '?/-'), $domain, $suffix];
327
                } else {
328
                    break;
329
                }
330
            }
331
332
            if (isset($result)) {
333
                return $result;
334
            }
335
        }
336
337
        return [];
338
    }
339
340
    public function build()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function build()
Loading history...
341
    {
342
        // 解析URL
343
        $url     = $this->url;
344
        $suffix  = $this->suffix;
345
        $domain  = $this->domain;
346
        $request = $this->app->request;
347
        $vars    = $this->vars;
348
349
        if (0 === strpos($url, '[') && $pos = strpos($url, ']')) {
350
            // [name] 表示使用路由命名标识生成URL
351
            $name = substr($url, 1, $pos - 1);
352
            $url  = 'name' . substr($url, $pos + 1);
353
        }
354
355
        if (false === strpos($url, '://') && 0 !== strpos($url, '/')) {
356
            $info = parse_url($url);
357
            $url  = !empty($info['path']) ? $info['path'] : '';
358
359
            if (isset($info['fragment'])) {
360
                // 解析锚点
361
                $anchor = $info['fragment'];
362
363
                if (false !== strpos($anchor, '?')) {
364
                    // 解析参数
365
                    list($anchor, $info['query']) = explode('?', $anchor, 2);
366
                }
367
368
                if (false !== strpos($anchor, '@')) {
369
                    // 解析域名
370
                    list($anchor, $domain) = explode('@', $anchor, 2);
371
                }
372
            } elseif (strpos($url, '@') && false === strpos($url, '\\')) {
373
                // 解析域名
374
                list($url, $domain) = explode('@', $url, 2);
375
            }
376
        }
377
378
        if ($url) {
379
            $checkName   = isset($name) ? $name : $url . (isset($info['query']) ? '?' . $info['query'] : '');
380
            $checkDomain = $domain && is_string($domain) ? $domain : null;
381
382
            $rule = $this->route->getName($checkName, $checkDomain);
383
384
            if (empty($rule) && isset($info['query'])) {
385
                $rule = $this->route->getName($url, $checkDomain);
386
                // 解析地址里面参数 合并到vars
387
                parse_str($info['query'], $params);
388
                $vars = array_merge($params, $vars);
389
                unset($info['query']);
390
            }
391
        }
392
393
        if (!empty($rule) && $match = $this->getRuleUrl($rule, $vars, $domain)) {
394
            // 匹配路由命名标识
395
            $url = $match[0];
396
397
            if ($domain && !empty($match[1])) {
398
                $domain = $match[1];
399
            }
400
401
            if (!is_null($match[2])) {
402
                $suffix = $match[2];
403
            }
404
        } elseif (!empty($rule) && isset($name)) {
405
            throw new \InvalidArgumentException('route name not exists:' . $name);
406
        } else {
407
            // 检测URL绑定
408
            $bind = $this->route->getDomainBind($domain && is_string($domain) ? $domain : null);
409
410
            if ($bind && 0 === strpos($url, $bind)) {
411
                $url = substr($url, strlen($bind) + 1);
412
            } else {
413
                $binds = $this->route->getBind();
414
415
                foreach ($binds as $key => $val) {
416
                    if (is_string($val) && 0 === strpos($url, $val) && substr_count($val, '/') > 1) {
417
                        $url    = substr($url, strlen($val) + 1);
418
                        $domain = $key;
419
                        break;
420
                    }
421
                }
422
            }
423
424
            // 路由标识不存在 直接解析
425
            $url = $this->parseUrl($url, $domain);
426
427
            if (isset($info['query'])) {
428
                // 解析地址里面参数 合并到vars
429
                parse_str($info['query'], $params);
430
                $vars = array_merge($params, $vars);
431
            }
432
        }
433
434
        // 还原URL分隔符
435
        $depr = $this->route->config('pathinfo_depr');
436
        $url  = str_replace('/', $depr, $url);
437
438
        $file = $request->baseFile();
439
        if ($file && 0 !== strpos($request->url(), $file)) {
440
            $file = str_replace('\\', '/', dirname($file));
441
        }
442
443
        $url = rtrim($file, '/') . '/' . $url;
444
445
        // URL后缀
446
        if ('/' == substr($url, -1) || '' == $url) {
447
            $suffix = '';
448
        } else {
449
            $suffix = $this->parseSuffix($suffix);
450
        }
451
452
        // 锚点
453
        $anchor = !empty($anchor) ? '#' . $anchor : '';
454
455
        // 参数组装
456
        if (!empty($vars)) {
457
            // 添加参数
458
            if ($this->route->config('url_common_param')) {
459
                $vars = http_build_query($vars);
460
                $url .= $suffix . '?' . $vars . $anchor;
461
            } else {
462
                foreach ($vars as $var => $val) {
463
                    if ('' !== $val) {
464
                        $url .= $depr . $var . $depr . urlencode((string) $val);
465
                    }
466
                }
467
468
                $url .= $suffix . $anchor;
469
            }
470
        } else {
471
            $url .= $suffix . $anchor;
472
        }
473
474
        // 检测域名
475
        $domain = $this->parseDomain($url, $domain);
476
477
        // URL组装
478
        return $domain . rtrim($this->root, '/') . '/' . ltrim($url, '/');
479
    }
480
481
    public function __toString()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __toString()
Loading history...
482
    {
483
        return $this->build();
484
    }
485
486
    public function __debugInfo()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __debugInfo()
Loading history...
487
    {
488
        return [
489
            'url'    => $this->url,
490
            'vars'   => $this->vars,
491
            'suffix' => $this->suffix,
492
            'domain' => $this->domain,
493
        ];
494
    }
495
}
496