LocationService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. /**
  21. *
  22. */
  23. define('LOCATION_SERVICE_PRODUCT_NAME', 'Location');
  24. /**
  25. *
  26. */
  27. define('LOCATION_SERVICE_DOMAIN', 'location.aliyuncs.com');
  28. /**
  29. *
  30. */
  31. define('LOCATION_SERVICE_VERSION', '2015-06-12');
  32. /**
  33. *
  34. */
  35. define('LOCATION_SERVICE_DESCRIBE_ENDPOINT_ACTION', 'DescribeEndpoints');
  36. /**
  37. *
  38. */
  39. define('LOCATION_SERVICE_REGION', 'cn-hangzhou');
  40. /**
  41. *
  42. */
  43. define('CACHE_EXPIRE_TIME', 3600);
  44. /**
  45. * @deprecated See: https://github.com/aliyun/openapi-sdk-php
  46. * Class DescribeEndpointRequest
  47. */
  48. class DescribeEndpointRequest extends RpcAcsRequest
  49. {
  50. /**
  51. * DescribeEndpointRequest constructor.
  52. *
  53. * @param $id
  54. * @param $serviceCode
  55. * @param $endPointType
  56. */
  57. public function __construct($id, $serviceCode, $endPointType)
  58. {
  59. parent::__construct(LOCATION_SERVICE_PRODUCT_NAME,
  60. LOCATION_SERVICE_VERSION,
  61. LOCATION_SERVICE_DESCRIBE_ENDPOINT_ACTION);
  62. $this->queryParameters['Id'] = $id;
  63. $this->queryParameters['ServiceCode'] = $serviceCode;
  64. $this->queryParameters['Type'] = $endPointType;
  65. $this->setRegionId(LOCATION_SERVICE_REGION);
  66. $this->setAcceptFormat('JSON');
  67. }
  68. }
  69. class LocationService
  70. {
  71. /**
  72. * @var IClientProfile
  73. */
  74. private $clientProfile;
  75. /**
  76. * @var array
  77. */
  78. public static $cache = array();
  79. /**
  80. * @var array
  81. */
  82. public static $lastClearTimePerProduct = array();
  83. /**
  84. * @var string
  85. */
  86. public static $serviceDomain = LOCATION_SERVICE_DOMAIN;
  87. /**
  88. * LocationService constructor.
  89. *
  90. * @param $clientProfile
  91. */
  92. public function __construct($clientProfile)
  93. {
  94. $this->clientProfile = $clientProfile;
  95. }
  96. /**
  97. * @param $regionId
  98. * @param $serviceCode
  99. * @param $endPointType
  100. * @param $product
  101. *
  102. * @return mixed|null
  103. * @throws ClientException
  104. */
  105. public function findProductDomain($regionId, $serviceCode, $endPointType, $product)
  106. {
  107. $key = $regionId . '#' . $product;
  108. $domain = isset(self::$cache[$key]) ? self::$cache[$key] : null;
  109. if ($domain === null || $this->checkCacheIsExpire($key) == true) {
  110. $domain = $this->findProductDomainFromLocationService($regionId, $serviceCode, $endPointType);
  111. self::$cache[$key] = $domain;
  112. }
  113. return $domain;
  114. }
  115. /**
  116. * @param $regionId
  117. * @param $product
  118. * @param $domain
  119. */
  120. public static function addEndPoint($regionId, $product, $domain)
  121. {
  122. $key = $regionId . '#' . $product;
  123. self::$cache[$key] = $domain;
  124. $lastClearTime = mktime(0, 0, 0, 1, 1, 2999);
  125. self::$lastClearTimePerProduct[$key] = $lastClearTime;
  126. }
  127. /**
  128. * @param $domain
  129. */
  130. public static function modifyServiceDomain($domain)
  131. {
  132. self::$serviceDomain = $domain;
  133. }
  134. /**
  135. * @param $key
  136. *
  137. * @return bool
  138. */
  139. private function checkCacheIsExpire($key)
  140. {
  141. $lastClearTime = isset(self::$lastClearTimePerProduct[$key])
  142. ? self::$lastClearTimePerProduct[$key]
  143. : null;
  144. if ($lastClearTime === null) {
  145. $lastClearTime = time();
  146. self::$lastClearTimePerProduct[$key] = $lastClearTime;
  147. }
  148. $now = time();
  149. $elapsedTime = $now - $lastClearTime;
  150. if ($elapsedTime > CACHE_EXPIRE_TIME) {
  151. $lastClearTime = time();
  152. self::$lastClearTimePerProduct[$key] = $lastClearTime;
  153. return true;
  154. }
  155. return false;
  156. }
  157. /**
  158. * @param $regionId
  159. * @param $serviceCode
  160. * @param $endPointType
  161. *
  162. * @return string|null
  163. * @throws ClientException
  164. */
  165. private function findProductDomainFromLocationService($regionId, $serviceCode, $endPointType)
  166. {
  167. $request = new DescribeEndpointRequest($regionId, $serviceCode, $endPointType);
  168. $signer = $this->clientProfile->getSigner();
  169. $credential = $this->clientProfile->getCredential();
  170. $requestUrl = $request->composeUrl($signer, $credential, self::$serviceDomain);
  171. $httpResponse = HttpHelper::curl($requestUrl, $request->getMethod(), null, $request->getHeaders());
  172. if (!$httpResponse->isSuccess()) {
  173. return null;
  174. }
  175. $respObj = json_decode($httpResponse->getBody());
  176. return $respObj->Endpoints->Endpoint[0]->Endpoint;
  177. }
  178. }