LocationService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. class DescribeEndpointRequest extends RpcAcsRequest
  45. {
  46. /**
  47. * DescribeEndpointRequest constructor.
  48. *
  49. * @param $id
  50. * @param $serviceCode
  51. * @param $endPointType
  52. */
  53. public function __construct($id, $serviceCode, $endPointType)
  54. {
  55. parent::__construct(LOCATION_SERVICE_PRODUCT_NAME,
  56. LOCATION_SERVICE_VERSION,
  57. LOCATION_SERVICE_DESCRIBE_ENDPOINT_ACTION);
  58. $this->queryParameters['Id'] = $id;
  59. $this->queryParameters['ServiceCode'] = $serviceCode;
  60. $this->queryParameters['Type'] = $endPointType;
  61. $this->setRegionId(LOCATION_SERVICE_REGION);
  62. $this->setAcceptFormat('JSON');
  63. }
  64. }
  65. class LocationService
  66. {
  67. /**
  68. * @var IClientProfile
  69. */
  70. private $clientProfile;
  71. /**
  72. * @var array
  73. */
  74. public static $cache = array();
  75. /**
  76. * @var array
  77. */
  78. public static $lastClearTimePerProduct = array();
  79. /**
  80. * @var string
  81. */
  82. public static $serviceDomain = LOCATION_SERVICE_DOMAIN;
  83. /**
  84. * LocationService constructor.
  85. *
  86. * @param $clientProfile
  87. */
  88. public function __construct($clientProfile)
  89. {
  90. $this->clientProfile = $clientProfile;
  91. }
  92. /**
  93. * @param $regionId
  94. * @param $serviceCode
  95. * @param $endPointType
  96. * @param $product
  97. *
  98. * @return mixed|null
  99. * @throws ClientException
  100. */
  101. public function findProductDomain($regionId, $serviceCode, $endPointType, $product)
  102. {
  103. $key = $regionId . '#' . $product;
  104. $domain = isset(self::$cache[$key]) ? self::$cache[$key] : null;
  105. if ($domain === null || $this->checkCacheIsExpire($key) == true) {
  106. $domain = $this->findProductDomainFromLocationService($regionId, $serviceCode, $endPointType);
  107. self::$cache[$key] = $domain;
  108. }
  109. return $domain;
  110. }
  111. /**
  112. * @param $regionId
  113. * @param $product
  114. * @param $domain
  115. */
  116. public static function addEndPoint($regionId, $product, $domain)
  117. {
  118. $key = $regionId . '#' . $product;
  119. self::$cache[$key] = $domain;
  120. $lastClearTime = mktime(0, 0, 0, 1, 1, 2999);
  121. self::$lastClearTimePerProduct[$key] = $lastClearTime;
  122. }
  123. /**
  124. * @param $domain
  125. */
  126. public static function modifyServiceDomain($domain)
  127. {
  128. self::$serviceDomain = $domain;
  129. }
  130. /**
  131. * @param $key
  132. *
  133. * @return bool
  134. */
  135. private function checkCacheIsExpire($key)
  136. {
  137. $lastClearTime = isset(self::$lastClearTimePerProduct[$key])
  138. ? self::$lastClearTimePerProduct[$key]
  139. : null;
  140. if ($lastClearTime === null) {
  141. $lastClearTime = time();
  142. self::$lastClearTimePerProduct[$key] = $lastClearTime;
  143. }
  144. $now = time();
  145. $elapsedTime = $now - $lastClearTime;
  146. if ($elapsedTime > CACHE_EXPIRE_TIME) {
  147. $lastClearTime = time();
  148. self::$lastClearTimePerProduct[$key] = $lastClearTime;
  149. return true;
  150. }
  151. return false;
  152. }
  153. /**
  154. * @param $regionId
  155. * @param $serviceCode
  156. * @param $endPointType
  157. *
  158. * @return string|null
  159. * @throws ClientException
  160. */
  161. private function findProductDomainFromLocationService($regionId, $serviceCode, $endPointType)
  162. {
  163. $request = new DescribeEndpointRequest($regionId, $serviceCode, $endPointType);
  164. $signer = $this->clientProfile->getSigner();
  165. $credential = $this->clientProfile->getCredential();
  166. $requestUrl = $request->composeUrl($signer, $credential, self::$serviceDomain);
  167. $httpResponse = HttpHelper::curl($requestUrl, $request->getMethod(), null, $request->getHeaders());
  168. if (!$httpResponse->isSuccess()) {
  169. return null;
  170. }
  171. $respObj = json_decode($httpResponse->getBody());
  172. return $respObj->Endpoints->Endpoint[0]->Endpoint;
  173. }
  174. }