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