EndpointConfig.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Mrgoon\AliyunSmsSdk\Regions;
  3. /*
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. */
  21. $endpoint_filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . "endpoints.xml";
  22. $xml = simplexml_load_string(file_get_contents($endpoint_filename));
  23. $json = json_encode($xml);
  24. $json_array = json_decode($json, TRUE);
  25. $endpoints = array();
  26. foreach ($json_array["Endpoint"] as $json_endpoint) {
  27. # pre-process RegionId & Product
  28. if (!array_key_exists("RegionId", $json_endpoint["RegionIds"])) {
  29. $region_ids = array();
  30. } else {
  31. $json_region_ids = $json_endpoint['RegionIds']['RegionId'];
  32. if (!is_array($json_region_ids)) {
  33. $region_ids = array($json_region_ids);
  34. } else {
  35. $region_ids = $json_region_ids;
  36. }
  37. }
  38. if (!array_key_exists("Product", $json_endpoint["Products"])) {
  39. $products = array();
  40. } else {
  41. $json_products = $json_endpoint["Products"]["Product"];
  42. if (array() === $json_products or !is_array($json_products)) {
  43. $products = array();
  44. } else if (array_keys($json_products) !== range(0, count($json_products) - 1)) {
  45. # array is not sequential
  46. $products = array($json_products);
  47. } else {
  48. $products = $json_products;
  49. }
  50. }
  51. $product_domains = array();
  52. foreach ($products as $product) {
  53. $product_domain = new ProductDomain($product['ProductName'], $product['DomainName']);
  54. array_push($product_domains, $product_domain);
  55. }
  56. $endpoint = new Endpoint($region_ids[0], $region_ids, $product_domains);
  57. array_push($endpoints, $endpoint);
  58. }
  59. EndpointProvider::setEndpoints($endpoints);