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