1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Traits; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use AlibabaCloud\Client\AlibabaCloud; |
7
|
|
|
use AlibabaCloud\Client\Config\Config; |
8
|
|
|
use AlibabaCloud\Client\Request\Request; |
9
|
|
|
use AlibabaCloud\Client\Filter\ApiFilter; |
10
|
|
|
use AlibabaCloud\Client\Filter\HttpFilter; |
11
|
|
|
use AlibabaCloud\Client\Filter\ClientFilter; |
12
|
|
|
use AlibabaCloud\Client\Regions\LocationService; |
13
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Help developers set up and get host. |
18
|
|
|
* |
19
|
|
|
* @package AlibabaCloud\Client\Traits |
20
|
|
|
* |
21
|
|
|
* @mixin AlibabaCloud |
22
|
|
|
*/ |
23
|
|
|
trait EndpointTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array Host cache. |
27
|
|
|
*/ |
28
|
|
|
private static $hosts = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array user customized EndpointData |
32
|
|
|
*/ |
33
|
|
|
private static $hostsByUserConfig = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Resolve host based on product name and region. |
37
|
|
|
* |
38
|
|
|
* @param string $product |
39
|
|
|
* @param string $regionId |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
* @throws ClientException |
43
|
|
|
*/ |
44
|
59 |
|
public static function resolveHost($product, $regionId = LocationService::GLOBAL_REGION) |
45
|
|
|
{ |
46
|
59 |
|
ApiFilter::product($product); |
47
|
56 |
|
ClientFilter::regionId($regionId); |
48
|
|
|
|
49
|
56 |
|
if (isset(self::$hosts[$product][$regionId])) { |
50
|
2 |
|
return self::$hosts[$product][$regionId]; |
51
|
|
|
} |
52
|
|
|
|
53
|
54 |
|
$domain = Config::get("endpoints.{$product}.{$regionId}"); |
54
|
54 |
|
if (!$domain) { |
55
|
3 |
|
$regionId = LocationService::GLOBAL_REGION; |
56
|
3 |
|
$domain = Config::get("endpoints.{$product}.{$regionId}", ''); |
57
|
3 |
|
} |
58
|
|
|
|
59
|
54 |
|
return $domain; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add host based on product name and region. |
64
|
|
|
* |
65
|
|
|
* @param string $product |
66
|
|
|
* @param string $host |
67
|
|
|
* @param string $regionId |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
* @throws ClientException |
71
|
|
|
*/ |
72
|
9 |
|
public static function addHost($product, $host, $regionId = LocationService::GLOBAL_REGION) |
73
|
|
|
{ |
74
|
9 |
|
ApiFilter::product($product); |
75
|
|
|
|
76
|
7 |
|
HttpFilter::host($host); |
77
|
|
|
|
78
|
5 |
|
ClientFilter::regionId($regionId); |
79
|
|
|
|
80
|
3 |
|
self::addHostByUserConfig($product, $regionId, $host); |
81
|
|
|
|
82
|
3 |
|
self::$hosts[$product][$regionId] = $host; |
83
|
|
|
|
84
|
3 |
|
LocationService::addHost($product, $host, $regionId); |
85
|
3 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Add user customized host. |
89
|
|
|
* |
90
|
|
|
* @param string $product |
91
|
|
|
* @param string $regionId |
92
|
|
|
* @param string $host |
93
|
|
|
*/ |
94
|
3 |
|
private static function addHostByUserConfig($product, $regionId, $host) |
95
|
|
|
{ |
96
|
3 |
|
if (empty($product) || empty($regionId) || empty($host)) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
3 |
|
$key = self::hostUserConfigKey($product, $regionId); |
100
|
3 |
|
if (false === $key) { |
|
|
|
|
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
self::$hostsByUserConfig[$key] = $host; |
105
|
3 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $product |
109
|
|
|
* @param string $regionId |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
58 |
|
public static function resolveHostByUserConfig($product, $regionId) |
114
|
|
|
{ |
115
|
58 |
|
$key = self::hostUserConfigKey($product, $regionId); |
116
|
58 |
|
if (false === $key) { |
|
|
|
|
117
|
4 |
|
return ""; |
118
|
|
|
} |
119
|
55 |
|
return isset(self::$hostsByUserConfig[$key]) ? self::$hostsByUserConfig[$key] : ""; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Request $request |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
* @throws ClientException |
127
|
|
|
*/ |
128
|
3 |
|
public static function resolveHostByRule(Request $request) |
129
|
|
|
{ |
130
|
3 |
|
$regionId = $request->realRegionId(); |
131
|
3 |
|
$network = $request->network ?: 'public'; |
132
|
3 |
|
$suffix = $request->endpointSuffix; |
133
|
3 |
|
if ($network === 'public') { |
134
|
3 |
|
$network = ''; |
135
|
3 |
|
} |
136
|
|
|
|
137
|
3 |
|
if ($request->endpointRegional === 'regional') { |
138
|
1 |
|
return "{$request->product}{$suffix}{$network}.{$regionId}.aliyuncs.com"; |
139
|
|
|
} |
140
|
|
|
|
141
|
2 |
|
if ($request->endpointRegional === 'central') { |
142
|
1 |
|
return "{$request->product}{$suffix}{$network}.aliyuncs.com"; |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
throw new InvalidArgumentException('endpointRegional is invalid.'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $product |
150
|
|
|
* @param string $regionId |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
60 |
|
private static function hostUserConfigKey($product, $regionId) |
155
|
|
|
{ |
156
|
60 |
|
if (empty($product) || empty($regionId)) { |
157
|
4 |
|
return false; |
|
|
|
|
158
|
|
|
} |
159
|
57 |
|
return $product . "_" . $regionId; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|