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
|
9 |
|
public function __construct(App $app) |
|
|
|
|
36
|
|
|
{ |
37
|
9 |
|
$this->app = $app; |
38
|
9 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* 获取驱动实例 |
42
|
|
|
* @param null|string $name |
|
|
|
|
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)) { |
|
|
|
|
50
|
|
|
throw new InvalidArgumentException(sprintf( |
|
|
|
|
51
|
|
|
'Unable to resolve NULL driver for [%s].', static::class |
52
|
|
|
)); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
6 |
|
return $this->drivers[$name] = $this->getDriver($name); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* 获取驱动实例 |
60
|
|
|
* @param string $name |
|
|
|
|
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 |
|
|
|
|
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
protected function resolveType(string $name) |
74
|
|
|
{ |
75
|
|
|
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
|
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 |
|
|
|
|
110
|
|
|
* @return mixed |
111
|
|
|
* |
112
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
157
|
|
|
* @param array $parameters |
|
|
|
|
158
|
|
|
* @return mixed |
159
|
|
|
*/ |
160
|
2 |
|
public function __call($method, $parameters) |
161
|
|
|
{ |
162
|
2 |
|
return $this->driver()->$method(...$parameters); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|