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
|
45 |
|
protected function driver(string $name = null) |
46
|
|
|
{ |
47
|
45 |
|
$name = $name ?: $this->getDefaultDriver(); |
48
|
|
|
|
49
|
45 |
|
if (is_null($name)) { |
50
|
|
|
throw new InvalidArgumentException(sprintf( |
51
|
|
|
'Unable to resolve NULL driver for [%s].', |
52
|
|
|
static::class |
53
|
|
|
)); |
54
|
|
|
} |
55
|
|
|
|
56
|
45 |
|
return $this->drivers[$name] = $this->getDriver($name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* 获取驱动实例 |
61
|
|
|
* @param string $name |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
45 |
|
protected function getDriver(string $name) |
65
|
|
|
{ |
66
|
45 |
|
return $this->drivers[$name] ?? $this->createDriver($name); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* 获取驱动类型 |
71
|
|
|
* @param string $name |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
21 |
|
protected function resolveType(string $name) |
75
|
|
|
{ |
76
|
21 |
|
return $name; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 获取驱动配置 |
81
|
|
|
* @param string $name |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
protected function resolveConfig(string $name) |
85
|
|
|
{ |
86
|
|
|
return $name; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 获取驱动类 |
91
|
|
|
* @param string $type |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
45 |
|
protected function resolveClass(string $type): string |
95
|
|
|
{ |
96
|
45 |
|
if ($this->namespace || false !== strpos($type, '\\')) { |
97
|
45 |
|
$class = false !== strpos($type, '\\') ? $type : $this->namespace . Str::studly($type); |
98
|
|
|
|
99
|
45 |
|
if (class_exists($class)) { |
100
|
45 |
|
return $class; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
throw new InvalidArgumentException("Driver [$type] not supported."); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* 获取驱动参数 |
109
|
|
|
* @param $name |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
45 |
|
protected function resolveParams($name): array |
113
|
|
|
{ |
114
|
45 |
|
$config = $this->resolveConfig($name); |
115
|
45 |
|
return [$config]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* 创建驱动 |
120
|
|
|
* |
121
|
|
|
* @param string $name |
122
|
|
|
* @return mixed |
123
|
|
|
* |
124
|
|
|
*/ |
125
|
45 |
|
protected function createDriver(string $name) |
126
|
|
|
{ |
127
|
45 |
|
$type = $this->resolveType($name); |
128
|
|
|
|
129
|
45 |
|
$method = 'create' . Str::studly($type) . 'Driver'; |
130
|
|
|
|
131
|
45 |
|
$params = $this->resolveParams($name); |
132
|
|
|
|
133
|
45 |
|
if (method_exists($this, $method)) { |
134
|
|
|
return $this->$method(...$params); |
135
|
|
|
} |
136
|
|
|
|
137
|
45 |
|
$class = $this->resolveClass($type); |
138
|
|
|
|
139
|
45 |
|
return $this->app->invokeClass($class, $params); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* 移除一个驱动实例 |
144
|
|
|
* |
145
|
|
|
* @param array|string|null $name |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function forgetDriver($name = null) |
149
|
|
|
{ |
150
|
|
|
$name = $name ?? $this->getDefaultDriver(); |
151
|
|
|
|
152
|
|
|
foreach ((array) $name as $cacheName) { |
153
|
|
|
if (isset($this->drivers[$cacheName])) { |
154
|
|
|
unset($this->drivers[$cacheName]); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* 默认驱动 |
163
|
|
|
* @return string|null |
164
|
|
|
*/ |
165
|
|
|
abstract public function getDefaultDriver(); |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* 动态调用 |
169
|
|
|
* @param string $method |
170
|
|
|
* @param array $parameters |
171
|
|
|
* @return mixed |
172
|
|
|
*/ |
173
|
24 |
|
public function __call($method, $parameters) |
174
|
|
|
{ |
175
|
24 |
|
return $this->driver()->$method(...$parameters); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|