Completed
Push — 6.0 ( a95596...daee83 )
by liu
05:21
created

Resource::vars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
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\Route;
16
17
/**
18
 * 资源路由类
19
 */
20
class Resource extends RuleGroup
21
{
22
    /**
23
     * 资源路由名称
24
     * @var string
25
     */
26
    protected $resource;
27
28
    /**
29
     * 资源路由地址
30
     * @var string
31
     */
32
    protected $route;
33
34
    /**
35
     * REST方法定义
36
     * @var array
37
     */
38
    protected $rest = [];
39
40
    /**
41
     * 架构函数
42
     * @access public
43
     * @param  Route         $router     路由对象
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
44
     * @param  RuleGroup     $parent     上级对象
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
45
     * @param  string        $name       资源名称
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 7 found
Loading history...
46
     * @param  string        $route      路由地址
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 6 found
Loading history...
47
     * @param  array         $rest       资源定义
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 7 found
Loading history...
48
     */
49
    public function __construct(Route $router, RuleGroup $parent = null, string $name = '', string $route = '', array $rest = [])
50
    {
51
        $name           = ltrim($name, '/');
52
        $this->router   = $router;
53
        $this->parent   = $parent;
54
        $this->resource = $name;
55
        $this->route    = $route;
56
        $this->name     = strpos($name, '.') ? strstr($name, '.', true) : $name;
57
58
        $this->setFullName();
59
60
        // 资源路由默认为完整匹配
61
        $this->option['complete_match'] = true;
62
63
        $this->rest = $rest;
64
65
        if ($this->parent) {
66
            $this->domain = $this->parent->getDomain();
67
            $this->parent->addRuleItem($this);
68
        }
69
70
        if ($router->isTest()) {
71
            $this->buildResourceRule();
72
        }
73
    }
74
75
    /**
76
     * 生成资源路由规则
77
     * @access protected
78
     * @return void
79
     */
80
    protected function buildResourceRule(): void
81
    {
82
        $rule   = $this->resource;
83
        $option = $this->option;
84
        $origin = $this->router->getGroup();
85
        $this->router->setGroup($this);
86
87
        if (strpos($rule, '.')) {
88
            // 注册嵌套资源路由
89
            $array = explode('.', $rule);
90
            $last  = array_pop($array);
91
            $item  = [];
92
93
            foreach ($array as $val) {
94
                $item[] = $val . '/<' . ($option['var'][$val] ?? $val . '_id') . '>';
95
            }
96
97
            $rule = implode('/', $item) . '/' . $last;
98
        }
99
100
        $prefix = substr($rule, strlen($this->name) + 1);
101
102
        // 注册资源路由
103
        foreach ($this->rest as $key => $val) {
104
            if ((isset($option['only']) && !in_array($key, $option['only']))
105
                || (isset($option['except']) && in_array($key, $option['except']))) {
0 ignored issues
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
106
                continue;
107
            }
108
109
            if (isset($last) && strpos($val[1], '<id>') && isset($option['var'][$last])) {
110
                $val[1] = str_replace('<id>', '<' . $option['var'][$last] . '>', $val[1]);
111
            } elseif (strpos($val[1], '<id>') && isset($option['var'][$rule])) {
112
                $val[1] = str_replace('<id>', '<' . $option['var'][$rule] . '>', $val[1]);
113
            }
114
115
            $ruleItem = $this->addRule(trim($prefix . $val[1], '/'), $this->route . '/' . $val[2], $val[0]);
116
117
            if (isset($option['resource_model'][$key])) {
118
                call_user_func_array([$ruleItem, 'model'], (array) $option['resource_model'][$key]);
119
            }
120
121
            if (isset($option['resource_validate'][$key])) {
122
                call_user_func_array([$ruleItem, 'validate'], (array) $option['resource_validate'][$key]);
123
            }
124
        }
125
126
        $this->router->setGroup($origin);
127
    }
128
129
    /**
130
     * 设置资源允许
131
     * @access public
132
     * @param  array $only 资源允许
133
     * @return $this
134
     */
135
    public function only(array $only)
136
    {
137
        return $this->setOption('only', $only);
138
    }
139
140
    /**
141
     * 设置资源排除
142
     * @access public
143
     * @param  array $except 排除资源
144
     * @return $this
145
     */
146
    public function except(array $except)
147
    {
148
        return $this->setOption('except', $except);
149
    }
150
151
    /**
152
     * 设置资源路由的变量
153
     * @access public
154
     * @param  array $vars 资源变量
155
     * @return $this
156
     */
157
    public function vars(array $vars)
158
    {
159
        return $this->setOption('var', $vars);
160
    }
161
162
    /**
163
     * 绑定资源验证
164
     * @access public
165
     * @param  array $validate 验证信息
166
     * @return $this
167
     */
168
    public function withValidate(array $validate)
169
    {
170
        return $this->setOption('resource_validate', $validate);
171
    }
172
173
    /**
174
     * 绑定资源模型
175
     * @access public
176
     * @param  array $model 模型绑定
177
     * @return $this
178
     */
179
    public function withModel(array $model)
180
    {
181
        return $this->setOption('resource_model', $model);
182
    }
183
184
    /**
185
     * rest方法定义和修改
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
186
     * @access public
187
     * @param  array|string  $name 方法名称
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
188
     * @param  array|bool    $resource 资源
189
     * @return $this
190
     */
191
    public function rest($name, $resource = [])
192
    {
193
        if (is_array($name)) {
194
            $this->rest = $resource ? $name : array_merge($this->rest, $name);
195
        } else {
196
            $this->rest[$name] = $resource;
197
        }
198
199
        return $this;
200
    }
201
202
}
203