PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/amazon-web-services/vendor/aws/Guzzle/Service/Client.php

https://github.com/mhoofman/wordpress-heroku
PHP | 297 lines | 186 code | 39 blank | 72 comment | 12 complexity | 68cbfb7e8ca06778d823b07ddf7f75c9 MD5 | raw file
  1. <?php
  2. namespace Guzzle\Service;
  3. use Guzzle\Common\Collection;
  4. use Guzzle\Common\Exception\InvalidArgumentException;
  5. use Guzzle\Common\Exception\BadMethodCallException;
  6. use Guzzle\Common\Version;
  7. use Guzzle\Inflection\InflectorInterface;
  8. use Guzzle\Inflection\Inflector;
  9. use Guzzle\Http\Client as HttpClient;
  10. use Guzzle\Http\Exception\MultiTransferException;
  11. use Guzzle\Service\Exception\CommandTransferException;
  12. use Guzzle\Http\Message\RequestInterface;
  13. use Guzzle\Service\Command\CommandInterface;
  14. use Guzzle\Service\Command\Factory\CompositeFactory;
  15. use Guzzle\Service\Command\Factory\FactoryInterface as CommandFactoryInterface;
  16. use Guzzle\Service\Resource\ResourceIteratorClassFactory;
  17. use Guzzle\Service\Resource\ResourceIteratorFactoryInterface;
  18. use Guzzle\Service\Description\ServiceDescriptionInterface;
  19. /**
  20. * Client object for executing commands on a web service.
  21. */
  22. class Client extends HttpClient implements ClientInterface
  23. {
  24. const COMMAND_PARAMS = 'command.params';
  25. /** @var ServiceDescriptionInterface Description of the service and possible commands */
  26. protected $serviceDescription;
  27. /** @var CommandFactoryInterface */
  28. protected $commandFactory;
  29. /** @var ResourceIteratorFactoryInterface */
  30. protected $resourceIteratorFactory;
  31. /** @var InflectorInterface Inflector associated with the service/client */
  32. protected $inflector;
  33. /**
  34. * Basic factory method to create a new client. Extend this method in subclasses to build more complex clients.
  35. *
  36. * @param array|Collection $config Configuration data
  37. *
  38. * @return Client
  39. */
  40. public static function factory($config = array())
  41. {
  42. return new static(isset($config['base_url']) ? $config['base_url'] : null, $config);
  43. }
  44. public static function getAllEvents()
  45. {
  46. return array_merge(HttpClient::getAllEvents(), array(
  47. 'client.command.create',
  48. 'command.before_prepare',
  49. 'command.after_prepare',
  50. 'command.before_send',
  51. 'command.after_send',
  52. 'command.parse_response'
  53. ));
  54. }
  55. /**
  56. * Magic method used to retrieve a command
  57. *
  58. * @param string $method Name of the command object to instantiate
  59. * @param array $args Arguments to pass to the command
  60. *
  61. * @return mixed Returns the result of the command
  62. * @throws BadMethodCallException when a command is not found
  63. */
  64. public function __call($method, $args)
  65. {
  66. return $this->getCommand($method, isset($args[0]) ? $args[0] : array())->getResult();
  67. }
  68. public function getCommand($name, array $args = array())
  69. {
  70. // Add global client options to the command
  71. if ($options = $this->getConfig(self::COMMAND_PARAMS)) {
  72. $args += $options;
  73. }
  74. if (!($command = $this->getCommandFactory()->factory($name, $args))) {
  75. throw new InvalidArgumentException("Command was not found matching {$name}");
  76. }
  77. $command->setClient($this);
  78. $this->dispatch('client.command.create', array('client' => $this, 'command' => $command));
  79. return $command;
  80. }
  81. /**
  82. * Set the command factory used to create commands by name
  83. *
  84. * @param CommandFactoryInterface $factory Command factory
  85. *
  86. * @return self
  87. */
  88. public function setCommandFactory(CommandFactoryInterface $factory)
  89. {
  90. $this->commandFactory = $factory;
  91. return $this;
  92. }
  93. /**
  94. * Set the resource iterator factory associated with the client
  95. *
  96. * @param ResourceIteratorFactoryInterface $factory Resource iterator factory
  97. *
  98. * @return self
  99. */
  100. public function setResourceIteratorFactory(ResourceIteratorFactoryInterface $factory)
  101. {
  102. $this->resourceIteratorFactory = $factory;
  103. return $this;
  104. }
  105. public function getIterator($command, array $commandOptions = null, array $iteratorOptions = array())
  106. {
  107. if (!($command instanceof CommandInterface)) {
  108. $command = $this->getCommand($command, $commandOptions ?: array());
  109. }
  110. return $this->getResourceIteratorFactory()->build($command, $iteratorOptions);
  111. }
  112. public function execute($command)
  113. {
  114. if ($command instanceof CommandInterface) {
  115. $this->send($this->prepareCommand($command));
  116. $this->dispatch('command.after_send', array('command' => $command));
  117. return $command->getResult();
  118. } elseif (is_array($command) || $command instanceof \Traversable) {
  119. return $this->executeMultiple($command);
  120. } else {
  121. throw new InvalidArgumentException('Command must be a command or array of commands');
  122. }
  123. }
  124. public function setDescription(ServiceDescriptionInterface $service)
  125. {
  126. $this->serviceDescription = $service;
  127. if ($this->getCommandFactory() && $this->getCommandFactory() instanceof CompositeFactory) {
  128. $this->commandFactory->add(new Command\Factory\ServiceDescriptionFactory($service));
  129. }
  130. // If a baseUrl was set on the description, then update the client
  131. if ($baseUrl = $service->getBaseUrl()) {
  132. $this->setBaseUrl($baseUrl);
  133. }
  134. return $this;
  135. }
  136. public function getDescription()
  137. {
  138. return $this->serviceDescription;
  139. }
  140. /**
  141. * Set the inflector used with the client
  142. *
  143. * @param InflectorInterface $inflector Inflection object
  144. *
  145. * @return self
  146. */
  147. public function setInflector(InflectorInterface $inflector)
  148. {
  149. $this->inflector = $inflector;
  150. return $this;
  151. }
  152. /**
  153. * Get the inflector used with the client
  154. *
  155. * @return self
  156. */
  157. public function getInflector()
  158. {
  159. if (!$this->inflector) {
  160. $this->inflector = Inflector::getDefault();
  161. }
  162. return $this->inflector;
  163. }
  164. /**
  165. * Prepare a command for sending and get the RequestInterface object created by the command
  166. *
  167. * @param CommandInterface $command Command to prepare
  168. *
  169. * @return RequestInterface
  170. */
  171. protected function prepareCommand(CommandInterface $command)
  172. {
  173. // Set the client and prepare the command
  174. $request = $command->setClient($this)->prepare();
  175. // Set the state to new if the command was previously executed
  176. $request->setState(RequestInterface::STATE_NEW);
  177. $this->dispatch('command.before_send', array('command' => $command));
  178. return $request;
  179. }
  180. /**
  181. * Execute multiple commands in parallel
  182. *
  183. * @param array|Traversable $commands Array of CommandInterface objects to execute
  184. *
  185. * @return array Returns an array of the executed commands
  186. * @throws Exception\CommandTransferException
  187. */
  188. protected function executeMultiple($commands)
  189. {
  190. $requests = array();
  191. $commandRequests = new \SplObjectStorage();
  192. foreach ($commands as $command) {
  193. $request = $this->prepareCommand($command);
  194. $commandRequests[$request] = $command;
  195. $requests[] = $request;
  196. }
  197. try {
  198. $this->send($requests);
  199. foreach ($commands as $command) {
  200. $this->dispatch('command.after_send', array('command' => $command));
  201. }
  202. return $commands;
  203. } catch (MultiTransferException $failureException) {
  204. // Throw a CommandTransferException using the successful and failed commands
  205. $e = CommandTransferException::fromMultiTransferException($failureException);
  206. // Remove failed requests from the successful requests array and add to the failures array
  207. foreach ($failureException->getFailedRequests() as $request) {
  208. if (isset($commandRequests[$request])) {
  209. $e->addFailedCommand($commandRequests[$request]);
  210. unset($commandRequests[$request]);
  211. }
  212. }
  213. // Always emit the command after_send events for successful commands
  214. foreach ($commandRequests as $success) {
  215. $e->addSuccessfulCommand($commandRequests[$success]);
  216. $this->dispatch('command.after_send', array('command' => $commandRequests[$success]));
  217. }
  218. throw $e;
  219. }
  220. }
  221. protected function getResourceIteratorFactory()
  222. {
  223. if (!$this->resourceIteratorFactory) {
  224. // Build the default resource iterator factory if one is not set
  225. $clientClass = get_class($this);
  226. $prefix = substr($clientClass, 0, strrpos($clientClass, '\\'));
  227. $this->resourceIteratorFactory = new ResourceIteratorClassFactory(array(
  228. "{$prefix}\\Iterator",
  229. "{$prefix}\\Model"
  230. ));
  231. }
  232. return $this->resourceIteratorFactory;
  233. }
  234. /**
  235. * Get the command factory associated with the client
  236. *
  237. * @return CommandFactoryInterface
  238. */
  239. protected function getCommandFactory()
  240. {
  241. if (!$this->commandFactory) {
  242. $this->commandFactory = CompositeFactory::getDefaultChain($this);
  243. }
  244. return $this->commandFactory;
  245. }
  246. /**
  247. * @deprecated
  248. * @codeCoverageIgnore
  249. */
  250. public function enableMagicMethods($isEnabled)
  251. {
  252. Version::warn(__METHOD__ . ' is deprecated');
  253. }
  254. }