EndpointV2SerializerTrait.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Aws\EndpointV2;
  3. use Aws\Api\Serializer\RestSerializer;
  4. use Aws\EndpointV2\Ruleset\RulesetEndpoint;
  5. use GuzzleHttp\Psr7\Uri;
  6. /**
  7. * Set of helper functions used to set endpoints and endpoint
  8. * properties derived from dynamic endpoint resolution.
  9. *
  10. * @internal
  11. */
  12. trait EndpointV2SerializerTrait
  13. {
  14. /**
  15. * Applies a resolved endpoint, headers and any custom HTTP schemes provided
  16. * in client configuration to options which are applied to the serialized request.
  17. *
  18. * @param $endpoint
  19. * @param $headers
  20. *
  21. * @return void
  22. */
  23. private function setEndpointV2RequestOptions(
  24. RulesetEndpoint $endpoint,
  25. array &$headers
  26. ): void
  27. {
  28. $this->applyHeaders($endpoint, $headers);
  29. $resolvedUrl = $endpoint->getUrl();
  30. $this->applyScheme($resolvedUrl);
  31. $this->endpoint = $this instanceof RestSerializer
  32. ? new Uri($resolvedUrl)
  33. : $resolvedUrl;
  34. }
  35. /**
  36. * Combines modeled headers and headers resolved from an endpoint object.
  37. *
  38. * @param $endpoint
  39. * @param $headers
  40. * @return void
  41. */
  42. private function applyHeaders(RulesetEndpoint $endpoint, array &$headers): void
  43. {
  44. if (!is_null($endpoint->getHeaders())) {
  45. $headers = array_merge(
  46. $headers,
  47. $endpoint->getHeaders()
  48. );
  49. }
  50. }
  51. /**
  52. * Applies custom HTTP schemes provided in client configuration.
  53. *
  54. * @param $resolvedUrl
  55. * @return void
  56. */
  57. private function applyScheme(&$resolvedUrl): void
  58. {
  59. $resolvedEndpointScheme = parse_url($resolvedUrl, PHP_URL_SCHEME);
  60. $scheme = $this->endpoint instanceof Uri
  61. ? $this->endpoint->getScheme()
  62. : parse_url($this->endpoint, PHP_URL_SCHEME);
  63. if (!empty($scheme) && $scheme !== $resolvedEndpointScheme) {
  64. $resolvedUrl = str_replace(
  65. $resolvedEndpointScheme,
  66. $scheme,
  67. $resolvedUrl
  68. );
  69. }
  70. }
  71. }