1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: hugh.li |
5
|
|
|
* Date: 2021/6/8 |
6
|
|
|
* Time: 5:47 下午. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace HughCube\Laravel\OTS; |
10
|
|
|
|
11
|
|
|
use Aliyun\OTS\OTSClient; |
12
|
|
|
use HughCube\Laravel\AlibabaCloud\AlibabaCloud; |
13
|
|
|
use HughCube\Laravel\AlibabaCloud\Client as AlibabaCloudClient; |
14
|
|
|
use Illuminate\Database\Connection as IlluminateConnection; |
15
|
|
|
use Illuminate\Support\Arr; |
16
|
|
|
|
17
|
|
|
class Connection extends IlluminateConnection |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var OTSClient |
21
|
|
|
*/ |
22
|
|
|
protected $ots; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new database connection instance. |
26
|
|
|
* |
27
|
|
|
* @param array $config |
28
|
|
|
*/ |
29
|
|
|
public function __construct(array $config) |
30
|
|
|
{ |
31
|
|
|
$this->config = $this->formatConfig($config); |
32
|
|
|
|
33
|
|
|
// Create the otsClient |
34
|
|
|
$this->ots = $this->createConnection($config); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array|string $config |
39
|
|
|
* @return mixed|string[] |
40
|
|
|
*/ |
41
|
|
|
protected function formatConfig($config) |
42
|
|
|
{ |
43
|
|
|
if (is_string($config)) { |
44
|
|
|
$config = ['alibabaCloud' => $config]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$alibabaCloud = null; |
48
|
|
|
if (Arr::has($config, 'alibabaCloud') && $config['alibabaCloud'] instanceof AlibabaCloudClient) { |
49
|
|
|
$alibabaCloud = $config['alibabaCloud']; |
50
|
|
|
} elseif (Arr::has($config, 'alibabaCloud')) { |
51
|
|
|
$alibabaCloud = AlibabaCloud::client($config['alibabaCloud']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** AccessKeyID */ |
55
|
|
|
if (empty($config['AccessKeyID']) && null !== $alibabaCloud) { |
56
|
|
|
$config['AccessKeyID'] = $alibabaCloud->getAccessKeyId(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** AccessKeySecret */ |
60
|
|
|
if (empty($config['AccessKeySecret']) && null !== $alibabaCloud) { |
61
|
|
|
$config['AccessKeySecret'] = $alibabaCloud->getAccessKeySecret(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $config; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return OtsClient |
69
|
|
|
*/ |
70
|
|
|
public function getOts() |
71
|
|
|
{ |
72
|
|
|
return $this->ots; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function getDatabaseName() |
79
|
|
|
{ |
80
|
|
|
return $this->getOts()->getClientConfig()->getInstanceName(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create a new OTSClient connection. |
85
|
|
|
* |
86
|
|
|
* @param array $config |
87
|
|
|
* |
88
|
|
|
* @return OTSClient |
89
|
|
|
*/ |
90
|
|
|
protected function createConnection(array $config) |
91
|
|
|
{ |
92
|
|
|
$config['ErrorLogHandler'] = Arr::get($config, 'ErrorLogHandler', false); |
93
|
|
|
$config['DebugLogHandler'] = Arr::get($config, 'DebugLogHandler', false); |
94
|
|
|
|
95
|
|
|
return new OTSClient($config); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
|
|
public function disconnect() |
102
|
|
|
{ |
103
|
|
|
unset($this->ots); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
|
|
public function getDriverName() |
110
|
|
|
{ |
111
|
|
|
return 'ots'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Dynamically pass methods to the connection. |
116
|
|
|
* |
117
|
|
|
* @param string $method |
118
|
|
|
* @param array $parameters |
119
|
|
|
* |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
public function __call($method, $parameters) |
123
|
|
|
{ |
124
|
|
|
return call_user_func_array([$this->getOts(), $method], $parameters); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|