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