LocationService.php 3.9 KB

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