Passed
Pull Request — 8.0 (#3044)
by wj
02:20
created

Manager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 1
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
5
// +----------------------------------------------------------------------
6
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
7
// +----------------------------------------------------------------------
8
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
9
// +----------------------------------------------------------------------
10
// | Author: yunwuxin <[email protected]>
11
// +----------------------------------------------------------------------
12
declare(strict_types=1);
13
14
namespace think;
15
16
use InvalidArgumentException;
17
use think\helper\Str;
18
19
abstract class Manager
20
{
21
    /**
22
     * 驱动
23
     * @var array
24
     */
25
    protected $drivers = [];
26
27
    /**
28
     * 驱动的命名空间
29
     * @var string
30
     */
31
    protected $namespace = null;
32
33
    public function __construct(protected App $app) {}
34
35
    /**
36
     * 获取驱动实例
37
     * @param null|string $name
38
     * @return mixed
39
     */
40 36
    protected function driver(string $name = null)
41
    {
42 36
        $name = $name ?: $this->getDefaultDriver();
43
44 36
        if (is_null($name)) {
45
            throw new InvalidArgumentException(sprintf(
46
                'Unable to resolve NULL driver for [%s].',
47
                static::class,
48
            ));
49
        }
50
51 36
        return $this->drivers[$name] = $this->getDriver($name);
52
    }
53
54
    /**
55
     * 获取驱动实例
56
     * @param string $name
57
     * @return mixed
58
     */
59 36
    protected function getDriver(string $name)
60
    {
61 36
        return $this->drivers[$name] ?? $this->createDriver($name);
62
    }
63
64
    /**
65
     * 获取驱动类型
66
     * @param string $name
67
     * @return mixed
68
     */
69 21
    protected function resolveType(string $name)
70
    {
71 21
        return $name;
72
    }
73
74
    /**
75
     * 获取驱动配置
76
     * @param string $name
77
     * @return mixed
78
     */
79
    protected function resolveConfig(string $name)
80
    {
81
        return $name;
82
    }
83
84
    /**
85
     * 获取驱动类
86
     * @param string $type
87
     * @return string
88
     */
89 36
    protected function resolveClass(string $type): string
90
    {
91 36
        if ($this->namespace || str_contains($type, '\\')) {
92 36
            $class = str_contains($type, '\\') ? $type : $this->namespace . Str::studly($type);
93
94 36
            if (class_exists($class)) {
95 36
                return $class;
96
            }
97
        }
98
99
        throw new InvalidArgumentException("Driver [$type] not supported.");
100
    }
101
102
    /**
103
     * 获取驱动参数
104
     * @param $name
105
     * @return array
106
     */
107 36
    protected function resolveParams($name): array
108
    {
109 36
        $config = $this->resolveConfig($name);
110 36
        return [$config];
111
    }
112
113
    /**
114
     * 创建驱动
115
     *
116
     * @param string $name
117
     * @return mixed
118
     *
119
     */
120 36
    protected function createDriver(string $name)
121
    {
122 36
        $type = $this->resolveType($name);
123
124 36
        $method = 'create' . Str::studly($type) . 'Driver';
125
126 36
        $params = $this->resolveParams($name);
127
128 36
        if (method_exists($this, $method)) {
129
            return $this->$method(...$params);
130
        }
131
132 36
        $class = $this->resolveClass($type);
133
134 36
        return $this->app->invokeClass($class, $params);
135
    }
136
137
    /**
138
     * 移除一个驱动实例
139
     *
140
     * @param array|string|null $name
141
     * @return $this
142
     */
143
    public function forgetDriver($name = null)
144
    {
145
        $name ??= $this->getDefaultDriver();
146
147
        foreach ((array) $name as $cacheName) {
148
            if (isset($this->drivers[$cacheName])) {
149
                unset($this->drivers[$cacheName]);
150
            }
151
        }
152
153
        return $this;
154
    }
155
156
    /**
157
     * 默认驱动
158
     * @return string|null
159
     */
160
    abstract public function getDefaultDriver();
161
162
    /**
163
     * 动态调用
164
     * @param string $method
165
     * @param array  $parameters
166
     * @return mixed
167
     */
168 21
    public function __call($method, $parameters)
169
    {
170 21
        return $this->driver()->$method(...$parameters);
171
    }
172
}
173