GuzzleClient.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace GuzzleHttp\Command\Guzzle;
  3. use GuzzleHttp\ClientInterface;
  4. use GuzzleHttp\Command\CommandInterface;
  5. use GuzzleHttp\Command\Guzzle\Handler\ValidatedDescriptionHandler;
  6. use GuzzleHttp\Command\ServiceClient;
  7. use GuzzleHttp\HandlerStack;
  8. /**
  9. * Default Guzzle web service client implementation.
  10. */
  11. class GuzzleClient extends ServiceClient
  12. {
  13. /** @var array $config */
  14. private $config;
  15. /** @var DescriptionInterface Guzzle service description */
  16. private $description;
  17. /**
  18. * The client constructor accepts an associative array of configuration
  19. * options:
  20. *
  21. * - defaults: Associative array of default command parameters to add to
  22. * each command created by the client.
  23. * - validate: Specify if command input is validated (defaults to true).
  24. * Changing this setting after the client has been created will have no
  25. * effect.
  26. * - process: Specify if HTTP responses are parsed (defaults to true).
  27. * Changing this setting after the client has been created will have no
  28. * effect.
  29. * - response_locations: Associative array of location types mapping to
  30. * ResponseLocationInterface objects.
  31. *
  32. * @param ClientInterface $client HTTP client to use.
  33. * @param DescriptionInterface $description Guzzle service description
  34. * @param callable $commandToRequestTransformer
  35. * @param callable $responseToResultTransformer
  36. * @param HandlerStack $commandHandlerStack
  37. * @param array $config Configuration options
  38. */
  39. public function __construct(
  40. ClientInterface $client,
  41. DescriptionInterface $description,
  42. callable $commandToRequestTransformer = null,
  43. callable $responseToResultTransformer = null,
  44. HandlerStack $commandHandlerStack = null,
  45. array $config = []
  46. ) {
  47. $this->config = $config;
  48. $this->description = $description;
  49. $serializer = $this->getSerializer($commandToRequestTransformer);
  50. $deserializer = $this->getDeserializer($responseToResultTransformer);
  51. parent::__construct($client, $serializer, $deserializer, $commandHandlerStack);
  52. $this->processConfig($config);
  53. }
  54. /**
  55. * Returns the command if valid; otherwise an Exception
  56. * @param string $name
  57. * @param array $args
  58. * @return CommandInterface
  59. * @throws \InvalidArgumentException
  60. */
  61. public function getCommand($name, array $args = [])
  62. {
  63. if (!$this->description->hasOperation($name)) {
  64. $name = ucfirst($name);
  65. if (!$this->description->hasOperation($name)) {
  66. throw new \InvalidArgumentException(
  67. "No operation found named {$name}"
  68. );
  69. }
  70. }
  71. // Merge in default command options
  72. $args += $this->getConfig('defaults');
  73. return parent::getCommand($name, $args);
  74. }
  75. /**
  76. * Return the description
  77. *
  78. * @return DescriptionInterface
  79. */
  80. public function getDescription()
  81. {
  82. return $this->description;
  83. }
  84. /**
  85. * Returns the passed Serializer when set, a new instance otherwise
  86. *
  87. * @param callable|null $commandToRequestTransformer
  88. * @return \GuzzleHttp\Command\Guzzle\Serializer
  89. */
  90. private function getSerializer($commandToRequestTransformer)
  91. {
  92. return $commandToRequestTransformer ==! null
  93. ? $commandToRequestTransformer
  94. : new Serializer($this->description);
  95. }
  96. /**
  97. * Returns the passed Deserializer when set, a new instance otherwise
  98. *
  99. * @param callable|null $responseToResultTransformer
  100. * @return \GuzzleHttp\Command\Guzzle\Deserializer
  101. */
  102. private function getDeserializer($responseToResultTransformer)
  103. {
  104. $process = (! isset($this->config['process']) || $this->config['process'] === true);
  105. return $responseToResultTransformer ==! null
  106. ? $responseToResultTransformer
  107. : new Deserializer($this->description, $process);
  108. }
  109. /**
  110. * Get the config of the client
  111. *
  112. * @param array|string $option
  113. * @return mixed
  114. */
  115. public function getConfig($option = null)
  116. {
  117. return $option === null
  118. ? $this->config
  119. : (isset($this->config[$option]) ? $this->config[$option] : []);
  120. }
  121. /**
  122. * @param $option
  123. * @param $value
  124. */
  125. public function setConfig($option, $value)
  126. {
  127. $this->config[$option] = $value;
  128. }
  129. /**
  130. * Prepares the client based on the configuration settings of the client.
  131. *
  132. * @param array $config Constructor config as an array
  133. */
  134. protected function processConfig(array $config)
  135. {
  136. // set defaults as an array if not provided
  137. if (!isset($config['defaults'])) {
  138. $config['defaults'] = [];
  139. }
  140. // Add the handlers based on the configuration option
  141. $stack = $this->getHandlerStack();
  142. if (!isset($config['validate']) || $config['validate'] === true) {
  143. $stack->push(new ValidatedDescriptionHandler($this->description), 'validate_description');
  144. }
  145. if (!isset($config['process']) || $config['process'] === true) {
  146. // TODO: This belongs to the Deserializer and should be handled there.
  147. // Question: What is the result when the Deserializer is bypassed?
  148. // Possible answer: The raw response.
  149. }
  150. }
  151. }