DefaultProfile.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. class DefaultProfile implements IClientProfile
  21. {
  22. private static $profile;
  23. private static $endpoints;
  24. private static $credential;
  25. private static $regionId;
  26. private static $acceptFormat;
  27. private static $isigner;
  28. private static $iCredential;
  29. private function __construct($regionId,$credential)
  30. {
  31. self::$regionId = $regionId;
  32. self::$credential = $credential;
  33. }
  34. public static function getProfile($regionId, $accessKeyId, $accessSecret)
  35. {
  36. $credential =new Credential($accessKeyId, $accessSecret);
  37. self::$profile = new DefaultProfile($regionId, $credential);
  38. return self::$profile;
  39. }
  40. public function getSigner()
  41. {
  42. if(null == self::$isigner)
  43. {
  44. self::$isigner = new ShaHmac1Signer();
  45. }
  46. return self::$isigner;
  47. }
  48. public function getRegionId()
  49. {
  50. return self::$regionId;
  51. }
  52. public function getFormat()
  53. {
  54. return self::$acceptFormat;
  55. }
  56. public function getCredential()
  57. {
  58. if(null == self::$credential && null != self::$iCredential)
  59. {
  60. self::$credential = self::$iCredential;
  61. }
  62. return self::$credential;
  63. }
  64. public static function getEndpoints()
  65. {
  66. if(null == self::$endpoints)
  67. {
  68. self::$endpoints = EndpointProvider::getEndpoints();
  69. }
  70. return self::$endpoints;
  71. }
  72. public static function addEndpoint($endpointName, $regionId, $product, $domain)
  73. {
  74. if(null == self::$endpoints)
  75. {
  76. self::$endpoints = self::getEndpoints();
  77. }
  78. $endpoint = self::findEndpointByName($endpointName);
  79. if(null == $endpoint)
  80. {
  81. self::addEndpoint_($endpointName, $regionId, $product, $domain);
  82. }
  83. else
  84. {
  85. self::updateEndpoint($regionId, $product, $domain, $endpoint);
  86. }
  87. }
  88. public static function findEndpointByName($endpointName)
  89. {
  90. foreach (self::$endpoints as $key => $endpoint)
  91. {
  92. if($endpoint->getName() == $endpointName)
  93. {
  94. return $endpoint;
  95. }
  96. }
  97. }
  98. private static function addEndpoint_($endpointName,$regionId, $product, $domain)
  99. {
  100. $regionIds = array($regionId);
  101. $productsDomains = array(new ProductDomain($product, $domain));
  102. $endpoint = new Endpoint($endpointName, $regionIds, $productDomains);
  103. array_push(self::$endpoints, $endpoint);
  104. }
  105. private static function updateEndpoint($regionId, $product, $domain, $endpoint)
  106. {
  107. $regionIds = $endpoint->getRegionIds();
  108. if(!in_array($regionId,$regionIds))
  109. {
  110. array_push($regionIds, $regionId);
  111. $endpoint->setRegionIds($regionIds);
  112. }
  113. $productDomains = $endpoint->getProductDomains();
  114. if(null == self::findProductDomain($productDomains, $product, $domain))
  115. {
  116. array_push($productDomains, new ProductDomain($product, $domain));
  117. }
  118. $endpoint->setProductDomains($productDomains);
  119. }
  120. private static function findProductDomain($productDomains, $product, $domain)
  121. {
  122. foreach ($productDomains as $key => $productDomain)
  123. {
  124. if($productDomain->getProductName() == $product && $productDomain->getDomainName() == $domain)
  125. {
  126. return $productDomain;
  127. }
  128. }
  129. return null;
  130. }
  131. }