Completed
Push — 6.0 ( cb8a0b...bb9f45 )
by yun
06:00
created

Manager::driver()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 4
cts 6
cp 0.6667
crap 3.3332
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: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use InvalidArgumentException;
16
use think\helper\Str;
17
18
abstract class Manager
19
{
20
    /** @var App */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
21
    protected $app;
22
23
    /**
24
     * 驱动
25
     * @var array
26
     */
27
    protected $drivers = [];
28
29
    /**
30
     * 驱动的命名空间
31
     * @var string
32
     */
33
    protected $namespace = "";
34
35 5
    public function __construct(App $app)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
36
    {
37 5
        $this->app = $app;
38 5
    }
39
40
    /**
41
     * 获取驱动实例
42
     * @param null|string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
43
     * @return mixed
44
     */
45 3
    public function driver($name = null)
46
    {
47 3
        $name = $name ?: $this->getDefaultDriver();
48
49 3
        if (is_null($name)) {
0 ignored issues
show
introduced by
The condition is_null($name) is always false.
Loading history...
50
            throw new InvalidArgumentException(sprintf(
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...
51
                'Unable to resolve NULL driver for [%s].', static::class
52
            ));
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...
53
        }
54
55 3
        return $this->drivers[$name] = $this->get($name);
56
    }
57
58
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
59
     * 获取驱动实例
60
     * @param $name
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
61
     * @return mixed
62
     */
63 3
    protected function get($name)
64
    {
65 3
        return $this->drivers[$name] ?? $this->createDriver($name);
66
    }
67
68
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
69
     * 获取驱动类型
70
     * @param $name
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
71
     * @return mixed
72
     */
73
    protected function resolveType($name)
74
    {
75
        return $name;
76
    }
77
78
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
79
     * 获取驱动配置
80
     * @param $name
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
81
     * @return array
82
     */
83
    protected function resolveConfig($name)
84
    {
85
        return $name;
86
    }
87
88
    /**
89
     * 获取驱动类
90
     * @param string $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
91
     * @return string
92
     */
93 3
    protected function resolveClass($type)
94
    {
95 3
        if ($this->namespace || false !== strpos($type, '\\')) {
96 3
            $class = false !== strpos($type, '\\') ? $type : $this->namespace . Str::studly($type);
97
98 3
            if (class_exists($class)) {
99 3
                return $class;
100
            }
101
        }
102
103
        throw new InvalidArgumentException("Driver [$type] not supported.");
104
    }
105
106
    /**
107
     * 创建驱动
108
     *
109
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
110
     * @return mixed
111
     *
112
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
113 3
    protected function createDriver(string $name)
114
    {
115 3
        $type   = $this->resolveType($name);
116 3
        $config = $this->resolveConfig($name);
117
118 3
        $method = 'create' . Str::studly($type) . 'Driver';
119
120 3
        if (method_exists($this, $method)) {
121
            return $this->$method($config);
122
        }
123
124 3
        $class = $this->resolveClass($type);
125
126 3
        return $this->app->make($class, [$config]);
127
    }
128
129
    /**
130
     * 默认驱动
131
     * @return string
132
     */
133
    abstract public function getDefaultDriver();
134
135
    /**
136
     * 动态调用
137
     * @param string $method
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
138
     * @param array  $parameters
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
139
     * @return mixed
140
     */
141
    public function __call($method, $parameters)
142
    {
143
        return $this->driver()->$method(...$parameters);
144
    }
145
}
146