Passed
Push — 5.2 ( 94f706...1b9ee1 )
by liu
03:53
created

Controller   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 24 6
A exec() 0 32 3
B controller() 0 16 7
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: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\route\dispatch;
14
15
use ReflectionMethod;
16
use think\App;
17
use think\exception\ClassNotFoundException;
18
use think\exception\HttpException;
19
use think\Request;
20
use think\route\Dispatch;
21
22
class Controller extends Dispatch
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
23
{
24
    protected $controller;
25
    protected $actionName;
26
27
    public function init()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
28
    {
29
        parent::init();
30
31
        $result = $this->dispatch;
32
33
        if (is_string($result)) {
34
            $result = explode('/', $result);
35
        }
36
37
        // 是否自动转换控制器和操作名
38
        $convert = is_bool($this->convert) ? $this->convert : $this->rule->config('url_convert');
0 ignored issues
show
introduced by
The condition is_bool($this->convert) is always true.
Loading history...
39
        // 获取控制器名
40
        $controller = strip_tags($result[0] ?: $this->rule->config('default_controller'));
41
42
        $this->controller = $convert ? strtolower($controller) : $controller;
43
44
        // 获取操作名
45
        $this->actionName = strip_tags($result[1] ?: $this->rule->config('default_action'));
46
47
        // 设置当前请求的控制器、操作
48
        $this->request
49
            ->setController(App::parseName($this->controller, 1))
50
            ->setAction($this->actionName);
51
    }
52
53
    public function exec()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
54
    {
55
        try {
56
            // 实例化控制器
57
            $instance = $this->controller($this->controller);
58
        } catch (ClassNotFoundException $e) {
59
            throw new HttpException(404, 'controller not exists:' . $e->getClass());
60
        }
61
62
        $this->app['middleware']->controller(function (Request $request, $next) use ($instance) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
63
            // 获取当前操作名
64
            $action = $this->actionName . $this->rule->config('action_suffix');
65
66
            if (is_callable([$instance, $action])) {
67
                // 严格获取当前操作方法名
68
                $reflect    = new ReflectionMethod($instance, $action);
69
                $actionName = $reflect->getName();
70
                $this->request->setAction($actionName);
71
72
                // 自动获取请求变量
73
                $vars = array_merge($this->request->param(), $this->param);
0 ignored issues
show
Bug introduced by
$this->request->param() of type null|object is incompatible with the type array expected by parameter $array1 of array_merge(). ( Ignorable by Annotation )

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

73
                $vars = array_merge(/** @scrutinizer ignore-type */ $this->request->param(), $this->param);
Loading history...
74
            } else {
75
                // 操作不存在
76
                throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()');
77
            }
78
79
            $data = $this->app->invokeReflectMethod($instance, $reflect, $vars);
80
81
            return $this->autoResponse($data);
82
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
83
84
        return $this->app['middleware']->dispatch($this->request, 'controller');
85
    }
86
87
    /**
88
     * 实例化访问控制器
89
     * @access public
90
     * @param  string $name 资源地址
91
     * @return object
92
     * @throws ClassNotFoundException
93
     */
94
    public function controller(string $name)
95
    {
96
        $suffix = $this->rule->config('controller_suffix') ? 'Controller' : '';
97
98
        $controllerLayer = $this->rule->config('controller_layer') ?: 'controller';
99
        $emptyController = $this->rule->config('empty_controller') ?: 'Error';
100
101
        $class = $this->app->parseClass($controllerLayer, $name . $suffix);
102
103
        if (class_exists($class)) {
104
            return $this->app->make($class, [], true);
105
        } elseif ($emptyController && class_exists($emptyClass = $this->app->parseClass($controllerLayer, $emptyController . $suffix))) {
106
            return $this->app->make($emptyClass, [], true);
107
        }
108
109
        throw new ClassNotFoundException('class not exists:' . $class, $class);
110
    }
111
}
112