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
|
59 |
|
* @param string $product |
39
|
|
|
* @param string $regionId |
40
|
59 |
|
* |
41
|
56 |
|
* @return string |
42
|
|
|
* @throws ClientException |
43
|
56 |
|
*/ |
44
|
2 |
|
public static function resolveHost($product, $regionId = LocationService::GLOBAL_REGION) |
45
|
|
|
{ |
46
|
|
|
ApiFilter::product($product); |
47
|
54 |
|
ClientFilter::regionId($regionId); |
48
|
54 |
|
|
49
|
3 |
|
if (isset(self::$hosts[$product][$regionId])) { |
50
|
3 |
|
return self::$hosts[$product][$regionId]; |
51
|
3 |
|
} |
52
|
|
|
|
53
|
54 |
|
$domain = Config::get("endpoints.{$product}.{$regionId}"); |
54
|
|
|
if (!$domain) { |
55
|
|
|
$regionId = LocationService::GLOBAL_REGION; |
56
|
|
|
$domain = Config::get("endpoints.{$product}.{$regionId}", ''); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $domain; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add host based on product name and region. |
64
|
|
|
* |
65
|
|
|
* @param string $product |
66
|
8 |
|
* @param string $host |
67
|
|
|
* @param string $regionId |
68
|
8 |
|
* |
69
|
|
|
* @return void |
70
|
6 |
|
* @throws ClientException |
71
|
|
|
*/ |
72
|
4 |
|
public static function addHost($product, $host, $regionId = LocationService::GLOBAL_REGION) |
73
|
|
|
{ |
74
|
2 |
|
ApiFilter::product($product); |
75
|
|
|
|
76
|
2 |
|
HttpFilter::host($host); |
77
|
2 |
|
|
78
|
|
|
ClientFilter::regionId($regionId); |
79
|
|
|
|
80
|
|
|
self::addHostByUserConfig($product, $regionId, $host); |
81
|
|
|
|
82
|
|
|
self::$hosts[$product][$regionId] = $host; |
83
|
|
|
|
84
|
|
|
LocationService::addHost($product, $host, $regionId); |
85
|
3 |
|
} |
86
|
|
|
|
87
|
3 |
|
/** |
88
|
3 |
|
* Add user customized host. |
89
|
3 |
|
* |
90
|
3 |
|
* @param string $product |
91
|
3 |
|
* @param string $regionId |
92
|
3 |
|
* @param string $host |
93
|
|
|
*/ |
94
|
3 |
|
private static function addHostByUserConfig($product, $regionId, $host) |
95
|
1 |
|
{ |
96
|
|
|
if (empty($product) || empty($regionId) || empty($host)) { |
97
|
|
|
return; |
98
|
2 |
|
} |
99
|
1 |
|
$key = self::hostUserConfigKey($product, $regionId); |
100
|
|
|
|
101
|
|
|
self::$hostsByUserConfig[$key] = $host; |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $product |
106
|
|
|
* @param string $regionId |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public static function resolveHostByUserConfig($product, $regionId) |
111
|
|
|
{ |
112
|
|
|
$key = self::hostUserConfigKey($product, $regionId); |
113
|
|
|
return isset(self::$hostsByUserConfig[$key]) ? self::$hostsByUserConfig[$key] : ""; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param Request $request |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
* @throws ClientException |
121
|
|
|
*/ |
122
|
|
|
public static function resolveHostByRule(Request $request) |
123
|
|
|
{ |
124
|
|
|
$regionId = $request->realRegionId(); |
125
|
|
|
$network = $request->network ?: 'public'; |
126
|
|
|
$suffix = $request->endpointSuffix; |
127
|
|
|
if ($network === 'public') { |
128
|
|
|
$network = ''; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($request->endpointRegional === 'regional') { |
132
|
|
|
return "{$request->product}{$suffix}{$network}.{$regionId}.aliyuncs.com"; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($request->endpointRegional === 'central') { |
136
|
|
|
return "{$request->product}{$suffix}{$network}.aliyuncs.com"; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
throw new InvalidArgumentException('endpointRegional is invalid.'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $product |
144
|
|
|
* @param string $regionId |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
private static function hostUserConfigKey($product, $regionId) |
149
|
|
|
{ |
150
|
|
|
if (empty($product) || empty($regionId)) { |
151
|
|
|
throw new \InvalidArgumentException('InvalidArgument : $product or $regionId is empty'); |
152
|
|
|
} |
153
|
|
|
return $product . "_" . $regionId; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|