1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Regions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use AlibabaCloud\Client\SDK; |
8
|
|
|
use AlibabaCloud\Client\Config\Config; |
9
|
|
|
use AlibabaCloud\Client\Request\Request; |
10
|
|
|
use AlibabaCloud\Client\Filter\ApiFilter; |
11
|
|
|
use AlibabaCloud\Client\Filter\HttpFilter; |
12
|
|
|
use AlibabaCloud\Client\Filter\ClientFilter; |
13
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
14
|
|
|
use AlibabaCloud\Client\Exception\ServerException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class LocationService |
18
|
|
|
* |
19
|
|
|
* @package AlibabaCloud\Client\Regions |
20
|
|
|
*/ |
21
|
|
|
class LocationService |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Global Region Name |
25
|
|
|
*/ |
26
|
|
|
const GLOBAL_REGION = 'global'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected static $hosts = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Request |
35
|
|
|
*/ |
36
|
|
|
protected $request; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* LocationService constructor. |
40
|
|
|
* |
41
|
|
|
* @param Request $request |
42
|
|
|
*/ |
43
|
14 |
|
private function __construct(Request $request) |
44
|
|
|
{ |
45
|
14 |
|
$this->request = $request; |
46
|
14 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Request $request |
50
|
|
|
* @param string $domain |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
* @throws ClientException |
54
|
|
|
* @throws ServerException |
55
|
|
|
* @deprecated |
56
|
|
|
* @codeCoverageIgnore |
57
|
|
|
*/ |
58
|
|
|
public static function findProductDomain(Request $request, $domain = 'location.aliyuncs.com') |
59
|
|
|
{ |
60
|
|
|
return self::resolveHost($request, $domain); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $regionId |
65
|
|
|
* @param $product |
66
|
|
|
* @param $domain |
67
|
|
|
* |
68
|
|
|
* @throws ClientException |
69
|
|
|
* @deprecated |
70
|
|
|
* @codeCoverageIgnore |
71
|
|
|
*/ |
72
|
|
|
public static function addEndPoint($regionId, $product, $domain) |
73
|
|
|
{ |
74
|
|
|
self::addHost($product, $domain, $regionId); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Request $request |
80
|
|
|
* @param string $domain |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
* @throws ClientException |
84
|
|
|
* @throws ServerException |
85
|
|
|
*/ |
86
|
14 |
|
public static function resolveHost(Request $request, $domain = 'location.aliyuncs.com') |
87
|
|
|
{ |
88
|
14 |
|
$locationService = new static($request); |
89
|
14 |
|
$product = $locationService->request->product; |
90
|
14 |
|
$regionId = $locationService->request->realRegionId(); |
91
|
|
|
|
92
|
14 |
|
if (!isset(self::$hosts[$product][$regionId])) { |
93
|
11 |
|
self::$hosts[$product][$regionId] = self::getResult($locationService, $domain); |
94
|
6 |
|
} |
95
|
|
|
|
96
|
9 |
|
return self::$hosts[$product][$regionId]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param static $locationService |
101
|
|
|
* @param string $domain |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
* @throws ClientException |
105
|
|
|
* @throws ServerException |
106
|
|
|
*/ |
107
|
11 |
|
private static function getResult($locationService, $domain) |
108
|
|
|
{ |
109
|
11 |
|
$locationRequest = new LocationServiceRequest($locationService->request, $domain); |
110
|
|
|
|
111
|
11 |
|
$result = $locationRequest->request(); |
112
|
|
|
|
113
|
8 |
|
if (!isset($result['Endpoints']['Endpoint'][0]['Endpoint'])) { |
114
|
2 |
|
throw new ClientException( |
115
|
2 |
|
'Not found Region ID in ' . $domain, |
116
|
|
|
SDK::INVALID_REGION_ID |
117
|
2 |
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
6 |
|
return $result['Endpoints']['Endpoint'][0]['Endpoint']; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $product |
125
|
|
|
* @param string $host |
126
|
|
|
* @param string $regionId |
127
|
|
|
* |
128
|
|
|
* @throws ClientException |
129
|
|
|
*/ |
130
|
9 |
|
public static function addHost($product, $host, $regionId = self::GLOBAL_REGION) |
131
|
|
|
{ |
132
|
9 |
|
ApiFilter::product($product); |
133
|
|
|
|
134
|
7 |
|
HttpFilter::host($host); |
135
|
|
|
|
136
|
5 |
|
ClientFilter::regionId($regionId); |
137
|
|
|
|
138
|
3 |
|
self::$hosts[$product][$regionId] = $host; |
139
|
3 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Update endpoints from OSS. |
143
|
|
|
* |
144
|
|
|
* @codeCoverageIgnore |
145
|
|
|
* @throws Exception |
146
|
|
|
*/ |
147
|
|
|
public static function updateEndpoints() |
148
|
|
|
{ |
149
|
|
|
$ossUrl = 'https://openapi-endpoints.oss-cn-hangzhou.aliyuncs.com/endpoints.json'; |
150
|
|
|
$json = \file_get_contents($ossUrl); |
151
|
|
|
$list = \json_decode($json, true); |
152
|
|
|
|
153
|
|
|
foreach ($list['endpoints'] as $endpoint) { |
154
|
|
|
Config::set( |
155
|
|
|
"endpoints.{$endpoint['service']}.{$endpoint['regionid']}", |
156
|
|
|
\strtolower($endpoint['endpoint']) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|