Completed
Push — 6.0 ( 1e69d2...64aa8b )
by yun
02:11
created

Manager::driver()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

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 6
cts 6
cp 1
crap 3
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 */
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 72
    public function __construct(App $app)
36
    {
37 72
        $this->app = $app;
38 72
    }
39
40
    /**
41
     * 获取驱动实例
42
     * @param null|string $name
43
     * @return mixed
44
     */
45 48
    protected function driver(string $name = null)
46
    {
47 48
        $name = $name ?: $this->getDefaultDriver();
48
49 48
        if (is_null($name)) {
50 3
            throw new InvalidArgumentException(sprintf(
51 3
                'Unable to resolve NULL driver for [%s].', static::class
52
            ));
53
        }
54
55 45
        return $this->drivers[$name] = $this->getDriver($name);
56
    }
57
58
    /**
59
     * 获取驱动实例
60
     * @param string $name
61
     * @return mixed
62
     */
63 45
    protected function getDriver(string $name)
64
    {
65 45
        return $this->drivers[$name] ?? $this->createDriver($name);
66
    }
67
68
    /**
69
     * 获取驱动类型
70
     * @param string $name
71
     * @return mixed
72
     */
73 21
    protected function resolveType(string $name)
74
    {
75 21
        return $name;
76
    }
77
78
    /**
79
     * 获取驱动配置
80
     * @param string $name
81
     * @return mixed
82
     */
83
    protected function resolveConfig(string $name)
84
    {
85
        return $name;
86
    }
87
88
    /**
89
     * 获取驱动类
90
     * @param string $type
91
     * @return string
92
     */
93 45
    protected function resolveClass(string $type): string
94
    {
95 45
        if ($this->namespace || false !== strpos($type, '\\')) {
96 45
            $class = false !== strpos($type, '\\') ? $type : $this->namespace . Str::studly($type);
97
98 45
            if (class_exists($class)) {
99 45
                return $class;
100
            }
101
        }
102
103
        throw new InvalidArgumentException("Driver [$type] not supported.");
104
    }
105
106
    /**
107
     * 获取驱动参数
108
     * @param $name
109
     * @return array
110
     */
111 45
    protected function resolveParams($name): array
112
    {
113 45
        $config = $this->resolveConfig($name);
114 45
        return [$config];
115
    }
116
117
    /**
118
     * 创建驱动
119
     *
120
     * @param string $name
121
     * @return mixed
122
     *
123
     */
124 45
    protected function createDriver(string $name)
125
    {
126 45
        $type = $this->resolveType($name);
127
128 45
        $method = 'create' . Str::studly($type) . 'Driver';
129
130 45
        $params = $this->resolveParams($name);
131
132 45
        if (method_exists($this, $method)) {
133
            return $this->$method(...$params);
134
        }
135
136 45
        $class = $this->resolveClass($type);
137
138 45
        return $this->app->invokeClass($class, $params);
139
    }
140
141
    /**
142
     * 移除一个驱动实例
143
     *
144
     * @param array|string|null $name
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
169
     * @param array  $parameters
170
     * @return mixed
171
     */
172 24
    public function __call($method, $parameters)
173
    {
174 24
        return $this->driver()->$method(...$parameters);
175
    }
176
}
177