Passed
Push — 6.0 ( 7ca19f...7f1370 )
by liu
02:51
created

Middleware::import()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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: Slince <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use InvalidArgumentException;
16
use LogicException;
17
use think\exception\HttpResponseException;
18
19
class Middleware
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class Middleware
Loading history...
20
{
21
    /**
22
     * 中间件执行队列
23
     * @var array
24
     */
25
    protected $queue = [];
26
27
    /**
28
     * 配置
29
     * @var array
30
     */
31
    protected $config = [
32
        'default_namespace' => 'app\\http\\middleware\\',
33
    ];
34
35
    /**
36
     * 应用对象
37
     * @var App
38
     */
39
    protected $app;
40
41
    public function __construct(array $config = [])
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
42
    {
43
        $this->config = array_merge($this->config, $config);
44
    }
45
46
    public static function __make(App $app, Config $config)
2 ignored issues
show
Coding Style introduced by
Method name "Middleware::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Public method name "Middleware::__make" must not be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
47
    {
48
        return (new static($config->get('middleware')))->setApp($app);
0 ignored issues
show
Bug introduced by
It seems like $config->get('middleware') can also be of type null; however, parameter $config of think\Middleware::__construct() does only seem to accept array, 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

48
        return (new static(/** @scrutinizer ignore-type */ $config->get('middleware')))->setApp($app);
Loading history...
49
    }
50
51
    public function setConfig(array $config): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setConfig()
Loading history...
52
    {
53
        $this->config = array_merge($this->config, $config);
54
    }
55
56
    /**
57
     * 设置应用对象
58
     * @access public
59
     * @param  App  $app
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
60
     * @return $this
61
     */
62
    public function setApp(App $app)
63
    {
64
        $this->app = $app;
65
        return $this;
66
    }
67
68
    /**
69
     * 导入中间件
70
     * @access public
71
     * @param  array  $middlewares
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
72
     * @param  string $type  中间件类型
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 2 found
Loading history...
73
     * @return void
74
     */
75
    public function import(array $middlewares = [], string $type = 'route'): void
76
    {
77
        foreach ($middlewares as $middleware) {
78
            $this->add($middleware, $type);
79
        }
80
    }
81
82
    /**
83
     * 注册中间件
84
     * @access public
85
     * @param  mixed  $middleware
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
86
     * @param  string $type  中间件类型
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 2 found
Loading history...
87
     * @return void
88
     */
89
    public function add($middleware, string $type = 'route'): void
90
    {
91
        if (is_null($middleware)) {
92
            return;
93
        }
94
95
        $middleware = $this->buildMiddleware($middleware, $type);
96
97
        if ($middleware) {
98
            $this->queue[$type][] = $middleware;
99
        }
100
    }
101
102
    /**
103
     * 注册控制器中间件
104
     * @access public
105
     * @param  mixed  $middleware
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
106
     * @return void
107
     */
108
    public function controller($middleware): void
109
    {
110
        $this->add($middleware, 'controller');
111
    }
112
113
    /**
114
     * 移除中间件
115
     * @access public
116
     * @param  mixed  $middleware
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
117
     * @param  string $type  中间件类型
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 2 found
Loading history...
118
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
119
    public function unshift($middleware, string $type = 'route')
120
    {
121
        if (is_null($middleware)) {
122
            return;
123
        }
124
125
        $middleware = $this->buildMiddleware($middleware, $type);
126
127
        if (!empty($middleware)) {
128
            array_unshift($this->queue[$type], $middleware);
129
        }
130
    }
131
132
    /**
133
     * 获取注册的中间件
134
     * @access public
135
     * @param  string $type  中间件类型
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
136
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
137
    public function all(string $type = 'route'): array
138
    {
139
        return $this->queue[$type] ?? [];
140
    }
141
142
    /**
143
     * 中间件调度
144
     * @access public
145
     * @param  Request  $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
146
     * @param  string   $type  中间件类型
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 2 found
Loading history...
147
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
148
    public function dispatch(Request $request, string $type = 'route')
149
    {
150
        return call_user_func($this->resolve($type), $request);
151
    }
152
153
    /**
154
     * 解析中间件
155
     * @access protected
156
     * @param  mixed  $middleware
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
157
     * @param  string $type  中间件类型
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 2 found
Loading history...
158
     * @return array
159
     */
160
    protected function buildMiddleware($middleware, string $type = 'route'): array
161
    {
162
        if (is_array($middleware)) {
163
            list($middleware, $param) = $middleware;
164
        }
165
166
        if ($middleware instanceof \Closure) {
167
            return [$middleware, $param ?? null];
168
        }
169
170
        if (!is_string($middleware)) {
171
            throw new InvalidArgumentException('The middleware is invalid');
172
        }
173
174
        if (false === strpos($middleware, '\\')) {
175
            if (isset($this->config[$middleware])) {
176
                $middleware = $this->config[$middleware];
177
            } else {
178
                $middleware = $this->config['default_namespace'] . $middleware;
179
            }
180
        }
181
182
        if (is_array($middleware)) {
183
            $this->import($middleware, $type);
184
            return [];
185
        }
186
187
        if (strpos($middleware, ':')) {
188
            list($middleware, $param) = explode(':', $middleware, 2);
189
        }
190
191
        return [[$this->app->make($middleware), 'handle'], $param ?? null];
192
    }
193
194
    protected function resolve(string $type = 'route')
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resolve()
Loading history...
195
    {
196
        return function (Request $request) use ($type) {
197
            $middleware = array_shift($this->queue[$type]);
198
199
            if (null === $middleware) {
200
                throw new InvalidArgumentException('The queue was exhausted, with no response returned');
201
            }
202
203
            list($call, $param) = $middleware;
204
205
            try {
206
                $response = call_user_func_array($call, [$request, $this->resolve($type), $param]);
207
            } catch (HttpResponseException $exception) {
208
                $response = $exception->getResponse();
209
            }
210
211
            if (!$response instanceof Response) {
212
                throw new LogicException('The middleware must return Response instance');
213
            }
214
215
            return $response;
216
        };
217
    }
218
219
}
220