Completed
Branch 6.0 (d30585)
by yun
06:27
created

Domain::bindToNamespace()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 8
nop 3
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 20
rs 10
c 0
b 0
f 0
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   路由对象
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
30
     * @param  string      $name     路由域名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 5 found
Loading history...
31
     * @param  mixed       $rule     域名路由
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 5 found
Loading history...
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  请求对象
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 2 found
Loading history...
44
     * @param  string       $url      访问地址
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter name; 6 found
Loading history...
45
     * @param  bool         $completeMatch   路由是否完全匹配
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
82
     * @param  string    $url URL地址
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
83
     * @return Dispatch|false
84
     */
85 6
    private function checkUrlBind(Request $request, string $url)
0 ignored issues
show
Coding Style introduced by
Private method name "Domain::checkUrlBind" must be prefixed with an underscore
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function parseBindAppendParam()
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
123
     * @param  string    $url URL地址
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
124
     * @param  string    $class 类名(带命名空间)
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
144
     * @param  string    $url URL地址
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 1 found
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type null; however, parameter $value of think\helper\Str::studly() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
        return new CallbackDispatch($request, $this, [$namespace . '\\' . Str::studly(/** @scrutinizer ignore-type */ $class), $method], $param);
Loading history...
160
    }
161
162
    /**
163
     * 绑定到控制器
164
     * @access protected
165
     * @param  Request   $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
166
     * @param  string    $url URL地址
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 1 found
Loading history...
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