Completed
Push — 6.0 ( 775d81...4cc48f )
by yun
08:02 queued 01:04
created

Manager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 = null;
34
35 22
    public function __construct(App $app)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
36
    {
37 22
        $this->app = $app;
38 22
    }
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 15
    protected function driver(string $name = null)
46
    {
47 15
        $name = $name ?: $this->getDefaultDriver();
48
49 15
        if (is_null($name)) {
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 15
        return $this->drivers[$name] = $this->getDriver($name);
56
    }
57
58
    /**
59
     * 获取驱动实例
60
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     * @return mixed
62
     */
63 15
    protected function getDriver(string $name)
64
    {
65 15
        return $this->drivers[$name] ?? $this->createDriver($name);
66
    }
67
68
    /**
69
     * 获取驱动类型
70
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
71
     * @return mixed
72
     */
73 7
    protected function resolveType(string $name)
74
    {
75 7
        return $name;
76
    }
77
78
    /**
79
     * 获取驱动配置
80
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
81
     * @return mixed
82
     */
83
    protected function resolveConfig(string $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 15
    protected function resolveClass(string $type): string
94
    {
95 15
        if ($this->namespace || false !== strpos($type, '\\')) {
96 15
            $class = false !== strpos($type, '\\') ? $type : $this->namespace . Str::studly($type);
97
98 15
            if (class_exists($class)) {
99 15
                return $class;
100
            }
101
        }
102
103
        throw new InvalidArgumentException("Driver [$type] not supported.");
104
    }
105
106
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
107
     * 获取驱动参数
108
     * @param $name
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
109
     * @return array
110
     */
111 15
    protected function resolveParams($name): array
112
    {
113 15
        $config = $this->resolveConfig($name);
114 15
        return [$config];
115
    }
116
117
    /**
118
     * 创建驱动
119
     *
120
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
121
     * @return mixed
122
     *
123
     */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
124 15
    protected function createDriver(string $name)
125
    {
126 15
        $type = $this->resolveType($name);
127
128 15
        $method = 'create' . Str::studly($type) . 'Driver';
129
130 15
        $params = $this->resolveParams($name);
131
132 15
        if (method_exists($this, $method)) {
133
            return $this->$method(...$params);
134
        }
135
136 15
        $class = $this->resolveClass($type);
137
138 15
        return $this->app->invokeClass($class, $params);
139
    }
140
141
    /**
142
     * 移除一个驱动实例
143
     *
144
     * @param array|string|null $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
145
     * @return $this
146
     */
147
    public function forgetDriver($name = null)
148
    {
149
        $name = $name ?? $this->getDefaultDriver();
150
151
        foreach ((array) $name as $cacheName) {
152
            if (isset($this->drivers[$cacheName])) {
153
                unset($this->drivers[$cacheName]);
154
            }
155
        }
156
157
        return $this;
158
    }
159
160
    /**
161
     * 默认驱动
162
     * @return string|null
163
     */
164
    abstract public function getDefaultDriver();
165
166
    /**
167
     * 动态调用
168
     * @param string $method
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
169
     * @param array  $parameters
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
170
     * @return mixed
171
     */
172 8
    public function __call($method, $parameters)
173
    {
174 8
        return $this->driver()->$method(...$parameters);
175
    }
176
}
177