GuzzleClient.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 */
  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. *
  57. * @param string $name
  58. *
  59. * @return CommandInterface
  60. *
  61. * @throws \InvalidArgumentException
  62. */
  63. public function getCommand($name, array $args = [])
  64. {
  65. if (!$this->description->hasOperation($name)) {
  66. $name = ucfirst($name);
  67. if (!$this->description->hasOperation($name)) {
  68. throw new \InvalidArgumentException(
  69. "No operation found named {$name}"
  70. );
  71. }
  72. }
  73. // Merge in default command options
  74. $args += $this->getConfig('defaults');
  75. return parent::getCommand($name, $args);
  76. }
  77. /**
  78. * Return the description
  79. *
  80. * @return DescriptionInterface
  81. */
  82. public function getDescription()
  83. {
  84. return $this->description;
  85. }
  86. /**
  87. * Returns the passed Serializer when set, a new instance otherwise
  88. *
  89. * @param callable|null $commandToRequestTransformer
  90. *
  91. * @return \GuzzleHttp\Command\Guzzle\Serializer
  92. */
  93. private function getSerializer($commandToRequestTransformer)
  94. {
  95. return $commandToRequestTransformer !== null
  96. ? $commandToRequestTransformer
  97. : new Serializer($this->description);
  98. }
  99. /**
  100. * Returns the passed Deserializer when set, a new instance otherwise
  101. *
  102. * @param callable|null $responseToResultTransformer
  103. *
  104. * @return \GuzzleHttp\Command\Guzzle\Deserializer
  105. */
  106. private function getDeserializer($responseToResultTransformer)
  107. {
  108. $process = (!isset($this->config['process']) || $this->config['process'] === true);
  109. return $responseToResultTransformer !== null
  110. ? $responseToResultTransformer
  111. : new Deserializer($this->description, $process);
  112. }
  113. /**
  114. * Get the config of the client
  115. *
  116. * @param array|string $option
  117. *
  118. * @return mixed
  119. */
  120. public function getConfig($option = null)
  121. {
  122. return $option === null
  123. ? $this->config
  124. : (isset($this->config[$option]) ? $this->config[$option] : []);
  125. }
  126. public function setConfig($option, $value)
  127. {
  128. $this->config[$option] = $value;
  129. }
  130. /**
  131. * Prepares the client based on the configuration settings of the client.
  132. *
  133. * @param array $config Constructor config as an array
  134. */
  135. protected function processConfig(array $config)
  136. {
  137. // set defaults as an array if not provided
  138. if (!isset($config['defaults'])) {
  139. $config['defaults'] = [];
  140. }
  141. // Add the handlers based on the configuration option
  142. $stack = $this->getHandlerStack();
  143. if (!isset($config['validate']) || $config['validate'] === true) {
  144. $stack->push(new ValidatedDescriptionHandler($this->description), 'validate_description');
  145. }
  146. if (!isset($config['process']) || $config['process'] === true) {
  147. // TODO: This belongs to the Deserializer and should be handled there.
  148. // Question: What is the result when the Deserializer is bypassed?
  149. // Possible answer: The raw response.
  150. }
  151. }
  152. }