|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EightPoints\Bundle\GuzzleBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use EightPoints\Bundle\GuzzleBundle\Log\Logger; |
|
6
|
|
|
use function method_exists; |
|
7
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
|
8
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
9
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
|
10
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
|
11
|
|
|
|
|
12
|
|
|
class Configuration implements ConfigurationInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $alias; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var boolean |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $debug; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \EightPoints\Bundle\GuzzleBundle\PluginInterface[] |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $plugins; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $alias |
|
31
|
|
|
* @param boolean $debug |
|
32
|
|
|
* @param array $plugins |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(string $alias, bool $debug = false, array $plugins = []) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->alias = $alias; |
|
37
|
|
|
$this->debug = $debug; |
|
38
|
|
|
$this->plugins = $plugins; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Generates the configuration tree builder |
|
43
|
|
|
* |
|
44
|
|
|
* @throws \RuntimeException |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getConfigTreeBuilder() : TreeBuilder |
|
49
|
|
|
{ |
|
50
|
|
|
$builder = new TreeBuilder($this->alias); |
|
51
|
|
|
|
|
52
|
|
|
if (method_exists($builder, 'getRootNode')) { |
|
53
|
|
|
$root = $builder->getRootNode(); |
|
54
|
|
|
} else { |
|
55
|
|
|
// BC layer for symfony/config 4.1 and older |
|
56
|
|
|
$root = $builder->root($this->alias); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$root |
|
60
|
|
|
->children() |
|
61
|
|
|
->append($this->createClientsNode()) |
|
62
|
|
|
->integerNode('logging') |
|
63
|
|
|
->defaultValue($this->debug) |
|
64
|
|
|
->beforeNormalization() |
|
65
|
|
|
->always(\Closure::fromCallable([$this, 'checkLoggingMode'])) |
|
66
|
|
|
->end() |
|
67
|
|
|
->end() |
|
68
|
|
|
->booleanNode('profiling')->defaultValue($this->debug)->end() |
|
69
|
|
|
->integerNode('slow_response_time')->defaultValue(0)->end() |
|
70
|
|
|
->end() |
|
71
|
|
|
->end(); |
|
72
|
|
|
|
|
73
|
|
|
return $builder; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create Clients Configuration |
|
78
|
|
|
* |
|
79
|
|
|
* @throws \RuntimeException |
|
80
|
|
|
* |
|
81
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition |
|
82
|
|
|
*/ |
|
83
|
|
|
private function createClientsNode() : ArrayNodeDefinition |
|
84
|
|
|
{ |
|
85
|
|
|
$builder = new TreeBuilder('clients'); |
|
86
|
|
|
|
|
87
|
|
|
/** @var \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $node */ |
|
88
|
|
|
if (method_exists($builder, 'getRootNode')) { |
|
89
|
|
|
$node = $builder->getRootNode(); |
|
90
|
|
|
} else { |
|
91
|
|
|
// BC layer for symfony/config 4.1 and older |
|
92
|
|
|
$node = $builder->root('clients'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** @var \Symfony\Component\Config\Definition\Builder\NodeBuilder $nodeChildren */ |
|
96
|
|
|
$nodeChildren = $node->useAttributeAsKey('name') |
|
97
|
|
|
->prototype('array') |
|
98
|
|
|
->children(); |
|
99
|
|
|
|
|
100
|
|
|
$nodeChildren->scalarNode('class')->defaultValue('%eight_points_guzzle.http_client.class%')->end() |
|
101
|
|
|
->scalarNode('base_url') |
|
|
|
|
|
|
102
|
|
|
->defaultValue(null) |
|
103
|
|
|
->validate() |
|
104
|
|
|
->ifTrue(function ($v) { |
|
105
|
|
|
return !is_string($v); |
|
106
|
|
|
}) |
|
107
|
|
|
->thenInvalid('base_url can be: string') |
|
108
|
|
|
->end() |
|
109
|
|
|
->end() |
|
110
|
|
|
->booleanNode('lazy')->defaultValue(false)->end() |
|
111
|
|
|
->integerNode('logging') |
|
112
|
|
|
->defaultValue(null) |
|
113
|
|
|
->beforeNormalization() |
|
114
|
|
|
->always(\Closure::fromCallable([$this, 'checkLoggingMode'])) |
|
115
|
|
|
->end() |
|
116
|
|
|
->end() |
|
117
|
|
|
->scalarNode('handler') |
|
118
|
|
|
->defaultValue(null) |
|
119
|
|
|
->validate() |
|
120
|
|
|
->ifTrue(function ($v) { |
|
121
|
|
|
return $v !== null && (!is_string($v) || !class_exists($v)); |
|
122
|
|
|
}) |
|
123
|
|
|
->thenInvalid('handler must be a valid FQCN for a loaded class') |
|
124
|
|
|
->end() |
|
125
|
|
|
->end() |
|
126
|
|
|
->arrayNode('options') |
|
127
|
|
|
->validate() |
|
128
|
|
|
->ifTrue(function ($options) { |
|
129
|
|
|
return count($options['form_params']) && count($options['multipart']); |
|
130
|
|
|
}) |
|
131
|
|
|
->thenInvalid('You cannot use form_params and multipart at the same time.') |
|
132
|
|
|
->end() |
|
133
|
|
|
->children() |
|
134
|
|
|
->arrayNode('headers') |
|
135
|
|
|
->useAttributeAsKey('name') |
|
136
|
|
|
->normalizeKeys(false) |
|
137
|
|
|
->prototype('scalar')->end() |
|
138
|
|
|
->end() |
|
139
|
|
|
->variableNode('allow_redirects') |
|
140
|
|
|
->validate() |
|
141
|
|
|
->ifTrue(function ($v) { |
|
142
|
|
|
return !is_array($v) && !is_bool($v); |
|
143
|
|
|
}) |
|
144
|
|
|
->thenInvalid('allow_redirects can be: bool or array') |
|
145
|
|
|
->end() |
|
146
|
|
|
->end() |
|
147
|
|
|
->variableNode('auth') |
|
148
|
|
|
->validate() |
|
149
|
|
|
->ifTrue(function ($v) { |
|
150
|
|
|
return !is_array($v) && !is_string($v); |
|
151
|
|
|
}) |
|
152
|
|
|
->thenInvalid('auth can be: string or array') |
|
153
|
|
|
->end() |
|
154
|
|
|
->end() |
|
155
|
|
|
->variableNode('query') |
|
156
|
|
|
->validate() |
|
157
|
|
|
->ifTrue(function ($v) { |
|
158
|
|
|
return !is_string($v) && !is_array($v); |
|
159
|
|
|
}) |
|
160
|
|
|
->thenInvalid('query can be: string or array') |
|
161
|
|
|
->end() |
|
162
|
|
|
->end() |
|
163
|
|
|
->arrayNode('curl') |
|
164
|
|
|
->beforeNormalization() |
|
165
|
|
|
->ifArray() |
|
166
|
|
|
->then(function (array $curlOptions) { |
|
167
|
|
|
$result = []; |
|
168
|
|
|
|
|
169
|
|
|
foreach ($curlOptions as $key => $value) { |
|
170
|
|
|
$optionName = 'CURLOPT_' . strtoupper($key); |
|
171
|
|
|
|
|
172
|
|
|
if (!defined($optionName)) { |
|
173
|
|
|
throw new InvalidConfigurationException(sprintf( |
|
174
|
|
|
'Invalid curl option in eight_points_guzzle: %s. ' . |
|
175
|
|
|
'Ex: use sslversion for CURLOPT_SSLVERSION option. ' . PHP_EOL . |
|
176
|
|
|
'See all available options: http://php.net/manual/en/function.curl-setopt.php', |
|
177
|
|
|
$key |
|
178
|
|
|
)); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$result[constant($optionName)] = $value; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $result; |
|
185
|
|
|
}) |
|
186
|
|
|
->end() |
|
187
|
|
|
->prototype('scalar') |
|
188
|
|
|
->end() |
|
189
|
|
|
->end() |
|
190
|
|
|
->variableNode('cert') |
|
191
|
|
|
->validate() |
|
192
|
|
|
->ifTrue(function ($v) { |
|
193
|
|
|
return !is_string($v) && (!is_array($v) || count($v) !== 2); |
|
194
|
|
|
}) |
|
195
|
|
|
->thenInvalid('cert can be: string or array with two entries (path and password)') |
|
196
|
|
|
->end() |
|
197
|
|
|
->end() |
|
198
|
|
|
->scalarNode('connect_timeout') |
|
199
|
|
|
->beforeNormalization() |
|
200
|
|
|
->always(function ($v) { |
|
201
|
|
|
return is_numeric($v) ? (float) $v : $v; |
|
202
|
|
|
}) |
|
203
|
|
|
->end() |
|
204
|
|
|
->validate() |
|
205
|
|
|
->ifTrue(function ($v) { |
|
206
|
|
|
return !is_float($v) && !(is_string($v) && strpos($v, 'env_') === 0); |
|
207
|
|
|
}) |
|
208
|
|
|
->thenInvalid('connect_timeout can be: float') |
|
209
|
|
|
->end() |
|
210
|
|
|
->end() |
|
211
|
|
|
->booleanNode('debug')->end() |
|
212
|
|
|
->variableNode('decode_content') |
|
213
|
|
|
->validate() |
|
214
|
|
|
->ifTrue(function ($v) { |
|
215
|
|
|
return !is_string($v) && !is_bool($v); |
|
216
|
|
|
}) |
|
217
|
|
|
->thenInvalid('decode_content can be: bool or string (gzip, compress, deflate, etc...)') |
|
218
|
|
|
->end() |
|
219
|
|
|
->end() |
|
220
|
|
|
->floatNode('delay')->end() |
|
221
|
|
|
->arrayNode('form_params') |
|
222
|
|
|
->useAttributeAsKey('name') |
|
223
|
|
|
->prototype('variable')->end() |
|
224
|
|
|
->end() |
|
225
|
|
|
->arrayNode('multipart') |
|
226
|
|
|
->prototype('variable')->end() |
|
227
|
|
|
->end() |
|
228
|
|
|
->scalarNode('sink') |
|
229
|
|
|
->validate() |
|
230
|
|
|
->ifTrue(function ($v) { |
|
231
|
|
|
return !is_string($v); |
|
232
|
|
|
}) |
|
233
|
|
|
->thenInvalid('sink can be: string') |
|
234
|
|
|
->end() |
|
235
|
|
|
->end() |
|
236
|
|
|
->booleanNode('http_errors')->end() |
|
237
|
|
|
->variableNode('expect') |
|
238
|
|
|
->validate() |
|
239
|
|
|
->ifTrue(function ($v) { |
|
240
|
|
|
return !is_bool($v) && !is_int($v); |
|
241
|
|
|
}) |
|
242
|
|
|
->thenInvalid('expect can be: bool or int') |
|
243
|
|
|
->end() |
|
244
|
|
|
->end() |
|
245
|
|
|
->variableNode('ssl_key') |
|
246
|
|
|
->validate() |
|
247
|
|
|
->ifTrue(function ($v) { |
|
248
|
|
|
return !is_string($v) && (!is_array($v) || count($v) !== 2); |
|
249
|
|
|
}) |
|
250
|
|
|
->thenInvalid('ssl_key can be: string or array with two entries (path and password)') |
|
251
|
|
|
->end() |
|
252
|
|
|
->end() |
|
253
|
|
|
->booleanNode('stream')->end() |
|
254
|
|
|
->booleanNode('synchronous')->end() |
|
255
|
|
|
->scalarNode('read_timeout') |
|
256
|
|
|
->beforeNormalization() |
|
257
|
|
|
->always(function ($v) { |
|
258
|
|
|
return is_numeric($v) ? (float) $v : $v; |
|
259
|
|
|
}) |
|
260
|
|
|
->end() |
|
261
|
|
|
->validate() |
|
262
|
|
|
->ifTrue(function ($v) { |
|
263
|
|
|
return !is_float($v) && !(is_string($v) && strpos($v, 'env_') === 0); |
|
264
|
|
|
}) |
|
265
|
|
|
->thenInvalid('read_timeout can be: float') |
|
266
|
|
|
->end() |
|
267
|
|
|
->end() |
|
268
|
|
|
->scalarNode('timeout') |
|
269
|
|
|
->beforeNormalization() |
|
270
|
|
|
->always(function ($v) { |
|
271
|
|
|
return is_numeric($v) ? (float) $v : $v; |
|
272
|
|
|
}) |
|
273
|
|
|
->end() |
|
274
|
|
|
->validate() |
|
275
|
|
|
->ifTrue(function ($v) { |
|
276
|
|
|
return !is_float($v) && !(is_string($v) && strpos($v, 'env_') === 0); |
|
277
|
|
|
}) |
|
278
|
|
|
->thenInvalid('timeout can be: float') |
|
279
|
|
|
->end() |
|
280
|
|
|
->end() |
|
281
|
|
|
->variableNode('verify') |
|
282
|
|
|
->validate() |
|
283
|
|
|
->ifTrue(function ($v) { |
|
284
|
|
|
return !is_bool($v) && !is_string($v); |
|
285
|
|
|
}) |
|
286
|
|
|
->thenInvalid('verify can be: bool or string') |
|
287
|
|
|
->end() |
|
288
|
|
|
->end() |
|
289
|
|
|
->booleanNode('cookies')->end() |
|
290
|
|
|
->arrayNode('proxy') |
|
291
|
|
|
->beforeNormalization() |
|
292
|
|
|
->ifString() |
|
293
|
|
|
->then(function($v) { return ['http'=> $v]; }) |
|
294
|
|
|
->end() |
|
295
|
|
|
->validate() |
|
296
|
|
|
->always(function($v) { |
|
297
|
|
|
if (empty($v['no'])) { |
|
298
|
|
|
unset($v['no']); |
|
299
|
|
|
} |
|
300
|
|
|
return $v; |
|
301
|
|
|
}) |
|
302
|
|
|
->end() |
|
303
|
|
|
->children() |
|
304
|
|
|
->scalarNode('http')->end() |
|
305
|
|
|
->scalarNode('https')->end() |
|
306
|
|
|
->arrayNode('no') |
|
307
|
|
|
->prototype('scalar')->end() |
|
308
|
|
|
->end() |
|
309
|
|
|
->end() |
|
310
|
|
|
->end() |
|
311
|
|
|
->scalarNode('version') |
|
312
|
|
|
->validate() |
|
313
|
|
|
->ifTrue(function ($v) { |
|
314
|
|
|
return !is_string($v) && !is_float($v); |
|
315
|
|
|
}) |
|
316
|
|
|
->thenInvalid('version can be: string or float') |
|
317
|
|
|
->end() |
|
318
|
|
|
->end() |
|
319
|
|
|
->end() |
|
320
|
|
|
->end(); |
|
321
|
|
|
|
|
322
|
|
|
$pluginsNode = $nodeChildren->arrayNode('plugin')->addDefaultsIfNotSet(); |
|
323
|
|
|
|
|
324
|
|
|
foreach ($this->plugins as $plugin) { |
|
325
|
|
|
$pluginNode = new ArrayNodeDefinition($plugin->getPluginName()); |
|
326
|
|
|
|
|
327
|
|
|
$plugin->addConfiguration($pluginNode); |
|
328
|
|
|
|
|
329
|
|
|
$pluginsNode->children()->append($pluginNode); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
return $node; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
private function checkLoggingMode($v): int |
|
336
|
|
|
{ |
|
337
|
|
|
if ($v === 1 || $v === true) { |
|
338
|
|
|
return Logger::LOG_MODE_ALL; |
|
339
|
|
|
} elseif ($v === 0 || $v === false) { |
|
340
|
|
|
return Logger::LOG_MODE_NONE; |
|
341
|
|
|
} else { |
|
342
|
|
|
return constant(Logger::class .'::LOG_MODE_' . strtoupper($v)); |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.