Credential.php
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace AlibabaCloud\Credentials;
use AlibabaCloud\Credentials\Credential\Config;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionException;
use ReflectionParameter;
/**
* Class Credential
*
* @package AlibabaCloud\Credentials
*
* @mixin AccessKeyCredential
* @mixin BearerTokenCredential
* @mixin EcsRamRoleCredential
* @mixin RamRoleArnCredential
* @mixin RsaKeyPairCredential
*/
class Credential
{
/**
* @var array
*/
protected $config = [];
/**
* @var array
*/
protected $types = [
'access_key' => AccessKeyCredential::class,
'sts' => StsCredential::class,
'ecs_ram_role' => EcsRamRoleCredential::class,
'ram_role_arn' => RamRoleArnCredential::class,
'rsa_key_pair' => RsaKeyPairCredential::class,
];
/**
* @var AccessKeyCredential|BearerTokenCredential|EcsRamRoleCredential|RamRoleArnCredential|RsaKeyPairCredential
*/
protected $credential;
/**
* @var string
*/
protected $type;
/**
* Credential constructor.
*
* @param array|Config $config
*
* @throws ReflectionException
*/
public function __construct($config = [])
{
if ($config instanceof Config) {
$config = $this->parse($config);
}
if ($config !== []) {
$this->config = array_change_key_case($config);
$this->parseConfig();
} else {
$this->credential = Credentials::get()->getCredential();
}
}
/**
* @param Config $config
*
* @return array
*/
private function parse($config)
{
$config = get_object_vars($config);
$res = [];
foreach ($config as $key => $value) {
$res[$this->toUnderScore($key)] = $value;
}
return $res;
}
private function toUnderScore($str)
{
$dstr = preg_replace_callback('/([A-Z]+)/', function ($matchs) {
return '_' . strtolower($matchs[0]);
}, $str);
return trim(preg_replace('/_{2,}/', '_', $dstr), '_');
}
/**
* @throws ReflectionException
*/
private function parseConfig()
{
if (!isset($this->config['type'])) {
throw new InvalidArgumentException('Missing required type option');
}
$this->type = $this->config['type'];
if (!isset($this->types[$this->type])) {
throw new InvalidArgumentException(
'Invalid type option, support: ' .
implode(', ', array_keys($this->types))
);
}
$class = new ReflectionClass($this->types[$this->type]);
$parameters = [];
/**
* @var $parameter ReflectionParameter
*/
foreach ($class->getConstructor()->getParameters() as $parameter) {
$parameters[] = $this->getValue($parameter);
}
$this->credential = $class->newInstance(...$parameters);
}
/**
* @param ReflectionParameter $parameter
*
* @return string|array
* @throws ReflectionException
*/
protected function getValue(ReflectionParameter $parameter)
{
if ($parameter->name === 'config' || $parameter->name === 'credential') {
return $this->config;
}
foreach ($this->config as $key => $value) {
if (strtolower($parameter->name) === $key) {
return $value;
}
}
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}
throw new InvalidArgumentException("Missing required {$parameter->name} option in config for {$this->type}");
}
/**
* @return AccessKeyCredential|BearerTokenCredential|EcsRamRoleCredential|RamRoleArnCredential|RsaKeyPairCredential
*/
public function getCredential()
{
return $this->credential;
}
/**
* @return array
*/
public function getConfig()
{
return $this->config;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public function __call($name, $arguments)
{
return $this->credential->$name($arguments);
}
}