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
|
|
|
$global = LocationService::GLOBAL_REGION; |
64
|
57 |
|
if (self::resolveHostByStatic($productCode, $global)) { |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
$productCode = null !== $productCode? strtolower($productCode) : null; |
68
|
57 |
|
return (bool)Config::get("endpoints.{$productCode}.{$global}"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $product |
73
|
|
|
* @param string $regionId |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public static function resolveHostByStatic($product, $regionId) |
78
|
|
|
{ |
79
|
|
|
if (isset(self::$hosts[$product][$regionId])) { |
80
|
|
|
return self::$hosts[$product][$regionId]; |
81
|
8 |
|
} |
82
|
|
|
|
83
|
8 |
|
return ''; |
84
|
|
|
} |
85
|
6 |
|
|
86
|
|
|
/** |
87
|
4 |
|
* Add host based on product name and region. |
88
|
|
|
* |
89
|
2 |
|
* @param string $product |
90
|
|
|
* @param string $host |
91
|
2 |
|
* @param string $regionId |
92
|
2 |
|
* |
93
|
|
|
* @return void |
94
|
|
|
* @throws ClientException |
95
|
|
|
*/ |
96
|
|
|
public static function addHost($product, $host, $regionId = LocationService::GLOBAL_REGION) |
97
|
|
|
{ |
98
|
|
|
ApiFilter::product($product); |
99
|
|
|
|
100
|
3 |
|
HttpFilter::host($host); |
101
|
|
|
|
102
|
3 |
|
ClientFilter::regionId($regionId); |
103
|
3 |
|
|
104
|
3 |
|
self::$hosts[$product][$regionId] = $host; |
105
|
3 |
|
|
106
|
3 |
|
LocationService::addHost($product, $host, $regionId); |
107
|
3 |
|
} |
108
|
|
|
|
109
|
3 |
|
/** |
110
|
1 |
|
* @param Request $request |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
2 |
|
* @throws ClientException |
114
|
1 |
|
*/ |
115
|
|
|
public static function resolveHostByRule(Request $request) |
116
|
|
|
{ |
117
|
1 |
|
$network = $request->network ?: 'public'; |
118
|
|
|
$suffix = $request->endpointSuffix; |
119
|
|
|
if ($network === 'public') { |
120
|
|
|
$network = ''; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($request->endpointRegional === 'regional') { |
124
|
|
|
$regionId = $request->realRegionId(); |
125
|
|
|
return "{$request->product}{$suffix}{$network}.{$regionId}.aliyuncs.com"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($request->endpointRegional === 'central') { |
129
|
|
|
return "{$request->product}{$suffix}{$network}.aliyuncs.com"; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
throw new InvalidArgumentException('endpointRegional is invalid.'); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|