GuzzleClient.php 5.1 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 */
  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 array $config Configuration options
  35. */
  36. public function __construct(
  37. ClientInterface $client,
  38. DescriptionInterface $description,
  39. ?callable $commandToRequestTransformer = null,
  40. ?callable $responseToResultTransformer = null,
  41. ?HandlerStack $commandHandlerStack = null,
  42. array $config = []
  43. ) {
  44. $this->config = $config;
  45. $this->description = $description;
  46. $serializer = $this->getSerializer($commandToRequestTransformer);
  47. $deserializer = $this->getDeserializer($responseToResultTransformer);
  48. parent::__construct($client, $serializer, $deserializer, $commandHandlerStack);
  49. $this->processConfig($config);
  50. }
  51. /**
  52. * Returns the command if valid; otherwise an Exception
  53. *
  54. * @param string $name
  55. *
  56. * @return CommandInterface
  57. *
  58. * @throws \InvalidArgumentException
  59. */
  60. public function getCommand($name, array $args = [])
  61. {
  62. if (!$this->description->hasOperation($name)) {
  63. $name = ucfirst($name);
  64. if (!$this->description->hasOperation($name)) {
  65. throw new \InvalidArgumentException(
  66. "No operation found named {$name}"
  67. );
  68. }
  69. }
  70. // Merge in default command options
  71. $args += $this->getConfig('defaults');
  72. return parent::getCommand($name, $args);
  73. }
  74. /**
  75. * Return the description
  76. *
  77. * @return DescriptionInterface
  78. */
  79. public function getDescription()
  80. {
  81. return $this->description;
  82. }
  83. /**
  84. * Returns the passed Serializer when set, a new instance otherwise
  85. *
  86. * @param callable|null $commandToRequestTransformer
  87. *
  88. * @return 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. *
  101. * @return Deserializer
  102. */
  103. private function getDeserializer($responseToResultTransformer)
  104. {
  105. $process = (!isset($this->config['process']) || $this->config['process'] === true);
  106. return $responseToResultTransformer !== null
  107. ? $responseToResultTransformer
  108. : new Deserializer($this->description, $process);
  109. }
  110. /**
  111. * Get the config of the client
  112. *
  113. * @param array|string $option
  114. *
  115. * @return mixed
  116. */
  117. public function getConfig($option = null)
  118. {
  119. return $option === null
  120. ? $this->config
  121. : (isset($this->config[$option]) ? $this->config[$option] : []);
  122. }
  123. public function setConfig($option, $value)
  124. {
  125. $this->config[$option] = $value;
  126. }
  127. /**
  128. * Prepares the client based on the configuration settings of the client.
  129. *
  130. * @param array $config Constructor config as an array
  131. */
  132. protected function processConfig(array $config)
  133. {
  134. // set defaults as an array if not provided
  135. if (!isset($config['defaults'])) {
  136. $config['defaults'] = [];
  137. }
  138. // Add the handlers based on the configuration option
  139. $stack = $this->getHandlerStack();
  140. if (!isset($config['validate']) || $config['validate'] === true) {
  141. $stack->push(new ValidatedDescriptionHandler($this->description), 'validate_description');
  142. }
  143. if (!isset($config['process']) || $config['process'] === true) {
  144. // TODO: This belongs to the Deserializer and should be handled there.
  145. // Question: What is the result when the Deserializer is bypassed?
  146. // Possible answer: The raw response.
  147. }
  148. }
  149. }