AwsClientTrait.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Aws;
  3. use Aws\Api\Service;
  4. /**
  5. * A trait providing generic functionality for interacting with Amazon Web
  6. * Services. This is meant to be used in classes implementing
  7. * \Aws\AwsClientInterface
  8. */
  9. trait AwsClientTrait
  10. {
  11. public function getPaginator($name, array $args = [])
  12. {
  13. $config = $this->getApi()->getPaginatorConfig($name);
  14. return new ResultPaginator($this, $name, $args, $config);
  15. }
  16. public function getIterator($name, array $args = [])
  17. {
  18. $config = $this->getApi()->getPaginatorConfig($name);
  19. if (!$config['result_key']) {
  20. throw new \UnexpectedValueException(sprintf(
  21. 'There are no resources to iterate for the %s operation of %s',
  22. $name, $this->getApi()['serviceFullName']
  23. ));
  24. }
  25. $key = is_array($config['result_key'])
  26. ? $config['result_key'][0]
  27. : $config['result_key'];
  28. if ($config['output_token'] && $config['input_token']) {
  29. return $this->getPaginator($name, $args)->search($key);
  30. }
  31. $result = $this->execute($this->getCommand($name, $args))->search($key);
  32. return new \ArrayIterator((array) $result);
  33. }
  34. public function waitUntil($name, array $args = [])
  35. {
  36. return $this->getWaiter($name, $args)->promise()->wait();
  37. }
  38. public function getWaiter($name, array $args = [])
  39. {
  40. $config = isset($args['@waiter']) ? $args['@waiter'] : [];
  41. $config += $this->getApi()->getWaiterConfig($name);
  42. return new Waiter($this, $name, $args, $config);
  43. }
  44. public function execute(CommandInterface $command)
  45. {
  46. return $this->executeAsync($command)->wait();
  47. }
  48. public function executeAsync(CommandInterface $command)
  49. {
  50. $handler = $command->getHandlerList()->resolve();
  51. return $handler($command);
  52. }
  53. public function __call($name, array $args)
  54. {
  55. $params = isset($args[0]) ? $args[0] : [];
  56. if (substr($name, -5) === 'Async') {
  57. return $this->executeAsync(
  58. $this->getCommand(substr($name, 0, -5), $params)
  59. );
  60. }
  61. return $this->execute($this->getCommand($name, $params));
  62. }
  63. /**
  64. * @param string $name
  65. * @param array $args
  66. *
  67. * @return CommandInterface
  68. */
  69. abstract public function getCommand($name, array $args = []);
  70. /**
  71. * @return Service
  72. */
  73. abstract public function getApi();
  74. }