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