Completed
Push — 6.0 ( a4ef3c...64a49e )
by yun
04:55
created

Manager::forgetDriver()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 12
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 9
    public function __construct(App $app)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
36
    {
37 9
        $this->app = $app;
38 9
    }
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 6
    protected function driver(string $name = null)
46
    {
47 6
        $name = $name ?: $this->getDefaultDriver();
48
49 6
        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 6
        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 6
    protected function getDriver(string $name)
64
    {
65 6
        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
    protected function resolveType(string $name)
74
    {
75
        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 6
    protected function resolveClass(string $type): string
94
    {
95 6
        if ($this->namespace || false !== strpos($type, '\\')) {
96 6
            $class = false !== strpos($type, '\\') ? $type : $this->namespace . Str::studly($type);
97
98 6
            if (class_exists($class)) {
99 6
                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 6
    protected function createDriver(string $name)
114
    {
115 6
        $type   = $this->resolveType($name);
116 6
        $config = $this->resolveConfig($name);
117
118 6
        $method = 'create' . Str::studly($type) . 'Driver';
119
120 6
        if (method_exists($this, $method)) {
121
            return $this->$method($config);
122
        }
123
124 6
        $class = $this->resolveClass($type);
125
126 6
        return $this->app->invokeClass($class, [$config]);
127
    }
128
129
    /**
130
     * 移除一个驱动实例
131
     *
132
     * @param array|string|null $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
133
     * @return $this
134
     */
135
    public function forgetDriver($name = null)
136
    {
137
        $name = $name ?? $this->getDefaultDriver();
138
139
        foreach ((array) $name as $cacheName) {
140
            if (isset($this->drivers[$cacheName])) {
141
                unset($this->drivers[$cacheName]);
142
            }
143
        }
144
145
        return $this;
146
    }
147
148
    /**
149
     * 默认驱动
150
     * @return string
151
     */
152
    abstract public function getDefaultDriver(): string;
153
154
    /**
155
     * 动态调用
156
     * @param string $method
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
157
     * @param array  $parameters
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
158
     * @return mixed
159
     */
160 2
    public function __call($method, $parameters)
161
    {
162 2
        return $this->driver()->$method(...$parameters);
163
    }
164
}
165