PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backwpup/sdk/Aws/Aws/Common/Client/AbstractClient.php

https://bitbucket.org/cesarmedrano/cesarmedrano
PHP | 231 lines | 116 code | 26 blank | 89 comment | 12 complexity | 7dead22d79db61ee2bfd8c486c99848f MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Aws\Common\Client;
  17. use Aws\Common\Aws;
  18. use Aws\Common\Credentials\Credentials;
  19. use Aws\Common\Credentials\CredentialsInterface;
  20. use Aws\Common\Enum\ClientOptions as Options;
  21. use Aws\Common\Exception\InvalidArgumentException;
  22. use Aws\Common\Signature\EndpointSignatureInterface;
  23. use Aws\Common\Signature\SignatureInterface;
  24. use Aws\Common\Signature\SignatureListener;
  25. use Aws\Common\Waiter\WaiterClassFactory;
  26. use Aws\Common\Waiter\CompositeWaiterFactory;
  27. use Aws\Common\Waiter\WaiterFactoryInterface;
  28. use Aws\Common\Waiter\WaiterConfigFactory;
  29. use Guzzle\Common\Collection;
  30. use Guzzle\Service\Client;
  31. use Guzzle\Service\Description\ServiceDescriptionInterface;
  32. /**
  33. * Abstract AWS client
  34. */
  35. abstract class AbstractClient extends Client implements AwsClientInterface
  36. {
  37. /**
  38. * @var CredentialsInterface AWS credentials
  39. */
  40. protected $credentials;
  41. /**
  42. * @var SignatureInterface Signature implementation of the service
  43. */
  44. protected $signature;
  45. /**
  46. * @var WaiterFactoryInterface Factory used to create waiter classes
  47. */
  48. protected $waiterFactory;
  49. /**
  50. * @param CredentialsInterface $credentials AWS credentials
  51. * @param SignatureInterface $signature Signature implementation
  52. * @param Collection $config Configuration options
  53. *
  54. * @throws InvalidArgumentException if an endpoint provider isn't provided
  55. */
  56. public function __construct(CredentialsInterface $credentials, SignatureInterface $signature, Collection $config)
  57. {
  58. // Bootstrap with Guzzle
  59. parent::__construct($config->get(Options::BASE_URL), $config);
  60. $this->credentials = $credentials;
  61. $this->signature = $signature;
  62. // Make sure the user agent is prefixed by the SDK version
  63. $this->setUserAgent('aws-sdk-php2/' . Aws::VERSION, true);
  64. // Add the event listener so that requests are signed before they are sent
  65. $dispatcher = $this->getEventDispatcher();
  66. $dispatcher->addSubscriber(new SignatureListener($credentials, $signature));
  67. if ($backoff = $config->get(Options::BACKOFF)) {
  68. $dispatcher->addSubscriber($backoff, -255);
  69. }
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function __call($method, $args = null)
  75. {
  76. if (substr($method, 0, 9) == 'waitUntil') {
  77. // Allow magic method calls for waiters (e.g. $client->waitUntil<WaiterName>($resource, $options))
  78. array_unshift($args, substr($method, 9));
  79. return call_user_func_array(array($this, 'waitUntil'), $args);
  80. } else {
  81. return parent::__call(ucfirst($method), $args);
  82. }
  83. }
  84. /**
  85. * Get an endpoint for a specific region from a service description
  86. *
  87. * @param ServiceDescriptionInterface $description Service description
  88. * @param string $region Region of the endpoint
  89. * @param string $scheme URL scheme
  90. *
  91. * @return string
  92. * @throws InvalidArgumentException
  93. */
  94. public static function getEndpoint(ServiceDescriptionInterface $description, $region, $scheme)
  95. {
  96. $service = $description->getData('serviceFullName');
  97. // Lookup the region in the service description
  98. if (!($regions = $description->getData('regions'))) {
  99. throw new InvalidArgumentException("No regions found in the {$service} description");
  100. }
  101. // Ensure that the region exists for the service
  102. if (!isset($regions[$region])) {
  103. throw new InvalidArgumentException("{$region} is not a valid region for {$service}");
  104. }
  105. // Ensure that the scheme is valid
  106. if ($regions[$region][$scheme] == false) {
  107. throw new InvalidArgumentException("{$scheme} is not a valid URI scheme for {$service} in {$region}");
  108. }
  109. return $scheme . '://' . $regions[$region]['hostname'];
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function getCredentials()
  115. {
  116. return $this->credentials;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function getSignature()
  122. {
  123. return $this->signature;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function getRegions()
  129. {
  130. return $this->serviceDescription->getData('regions');
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function getRegion()
  136. {
  137. return $this->getConfig(Options::REGION);
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function setRegion($region)
  143. {
  144. $config = $this->getConfig();
  145. $baseUrl = self::getEndpoint($this->serviceDescription, $region, $config->get(Options::SCHEME));
  146. $this->setBaseUrl($baseUrl);
  147. $config->set(Options::BASE_URL, $baseUrl)->set(Options::REGION, $region);
  148. // Update the signature if necessary
  149. $signature = $this->getSignature();
  150. if ($signature instanceof EndpointSignatureInterface) {
  151. /** @var $signature EndpointSignatureInterface */
  152. $signature->setRegionName($region);
  153. }
  154. return $this;
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function waitUntil($waiter, array $input = array())
  160. {
  161. $this->getWaiter($waiter, $input)->wait();
  162. return $this;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function getWaiter($waiter, array $input = array())
  168. {
  169. return $this->getWaiterFactory()->build($waiter)
  170. ->setClient($this)
  171. ->setConfig($input);
  172. }
  173. /**
  174. * Set the waiter factory to use with the client
  175. *
  176. * @param WaiterFactoryInterface $waiterFactory Factory used to create waiters
  177. *
  178. * @return self
  179. */
  180. public function setWaiterFactory(WaiterFactoryInterface $waiterFactory)
  181. {
  182. $this->waiterFactory = $waiterFactory;
  183. return $this;
  184. }
  185. /**
  186. * Get the waiter factory used with the class
  187. *
  188. * @return WaiterFactoryInterface
  189. */
  190. public function getWaiterFactory()
  191. {
  192. if (!$this->waiterFactory) {
  193. $clientClass = get_class($this);
  194. // Use a composite factory that checks for classes first, then config waiters
  195. $this->waiterFactory = new CompositeWaiterFactory(array(
  196. new WaiterClassFactory(substr($clientClass, 0, strrpos($clientClass, '\\')) . '\\Waiter')
  197. ));
  198. if ($this->getDescription()) {
  199. $this->waiterFactory->addFactory(new WaiterConfigFactory($this->getDescription()->getData('waiters')));
  200. }
  201. }
  202. return $this->waiterFactory;
  203. }
  204. }