1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\BatchClient; |
6
|
|
|
use Http\Client\Common\BatchClientInterface; |
7
|
|
|
use Http\Client\Common\FlexibleHttpClient; |
8
|
|
|
use Http\Client\Common\HttpMethodsClient; |
9
|
|
|
use Http\Client\Common\HttpMethodsClientInterface; |
10
|
|
|
use Http\Client\Common\Plugin\AuthenticationPlugin; |
11
|
|
|
use Http\Client\Common\PluginClient; |
12
|
|
|
use Http\Client\Common\PluginClientFactory; |
13
|
|
|
use Http\Client\HttpAsyncClient; |
14
|
|
|
use Http\Client\HttpClient; |
15
|
|
|
use Http\Client\Plugin\Vcr\RecordPlugin; |
16
|
|
|
use Http\Client\Plugin\Vcr\ReplayPlugin; |
17
|
|
|
use Http\Message\Authentication\BasicAuth; |
18
|
|
|
use Http\Message\Authentication\Bearer; |
19
|
|
|
use Http\Message\Authentication\QueryParam; |
20
|
|
|
use Http\Message\Authentication\Wsse; |
21
|
|
|
use Http\Mock\Client as MockClient; |
22
|
|
|
use Psr\Http\Message\UriInterface; |
23
|
|
|
use Symfony\Component\Config\FileLocator; |
24
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
25
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
26
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
27
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
28
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
29
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
30
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
31
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
32
|
|
|
use Twig\Environment as TwigEnvironment; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @author David Buchmann <[email protected]> |
36
|
|
|
* @author Tobias Nyholm <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
class HttplugExtension extends Extension |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* Used to check is the VCR plugin is installed. |
42
|
|
|
* |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private $useVcrPlugin = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
35 |
|
public function load(array $configs, ContainerBuilder $container) |
51
|
|
|
{ |
52
|
35 |
|
$configuration = $this->getConfiguration($configs, $container); |
53
|
35 |
|
$config = $this->processConfiguration($configuration, $configs); |
54
|
|
|
|
55
|
35 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
56
|
|
|
|
57
|
35 |
|
$loader->load('services.xml'); |
58
|
35 |
|
$loader->load('plugins.xml'); |
59
|
35 |
|
if (\class_exists(MockClient::class)) { |
60
|
35 |
|
$loader->load('mock-client.xml'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Register default services |
64
|
35 |
|
foreach ($config['classes'] as $service => $class) { |
65
|
35 |
|
if (!empty($class)) { |
66
|
1 |
|
$container->register(sprintf('httplug.%s.default', $service), $class); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Set main aliases |
71
|
35 |
|
foreach ($config['main_alias'] as $type => $id) { |
72
|
35 |
|
$container->setAlias(sprintf('httplug.%s', $type), new Alias($id, true)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Configure toolbar |
76
|
35 |
|
$profilingEnabled = $this->isConfigEnabled($container, $config['profiling']); |
77
|
35 |
|
if ($profilingEnabled) { |
78
|
32 |
|
$loader->load('data-collector.xml'); |
79
|
|
|
|
80
|
32 |
|
if (!empty($config['profiling']['formatter'])) { |
81
|
|
|
// Add custom formatter |
82
|
|
|
$container |
83
|
1 |
|
->getDefinition('httplug.collector.formatter') |
84
|
1 |
|
->replaceArgument(0, new Reference($config['profiling']['formatter'])) |
85
|
|
|
; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$container |
89
|
32 |
|
->getDefinition('httplug.formatter.full_http_message') |
90
|
32 |
|
->addArgument($config['profiling']['captured_body_length']) |
91
|
|
|
; |
92
|
|
|
|
93
|
32 |
|
if (!class_exists(TwigEnvironment::class) && !class_exists(\Twig_Environment::class)) { |
94
|
|
|
$container->removeDefinition('httplug.collector.twig.http_message'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
35 |
|
$this->configureClients($container, $config); |
99
|
35 |
|
$this->configurePlugins($container, $config['plugins']); // must be after clients, as clients.X.plugins might use plugins as templates that will be removed |
100
|
35 |
|
$this->configureAutoDiscoveryClients($container, $config); |
101
|
|
|
|
102
|
35 |
|
if (!$config['default_client_autowiring']) { |
103
|
1 |
|
$container->removeAlias(HttpAsyncClient::class); |
104
|
1 |
|
$container->removeAlias(HttpClient::class); |
105
|
|
|
} |
106
|
|
|
|
107
|
35 |
|
if ($this->useVcrPlugin) { |
108
|
5 |
|
if (!\class_exists(RecordPlugin::class)) { |
109
|
|
|
throw new \Exception('You need to require the VCR plugin to be able to use it: "composer require --dev php-http/vcr-plugin".'); |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
$loader->load('vcr-plugin.xml'); |
113
|
|
|
} |
114
|
35 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Configure client services. |
118
|
|
|
* |
119
|
|
|
* @param ContainerBuilder $container |
120
|
|
|
* @param array $config |
121
|
|
|
*/ |
122
|
35 |
|
private function configureClients(ContainerBuilder $container, array $config) |
123
|
|
|
{ |
124
|
35 |
|
$first = null; |
125
|
35 |
|
$clients = []; |
126
|
|
|
|
127
|
35 |
|
foreach ($config['clients'] as $name => $arguments) { |
128
|
25 |
|
if (null === $first) { |
129
|
|
|
// Save the name of the first configured client. |
130
|
25 |
|
$first = $name; |
131
|
|
|
} |
132
|
|
|
|
133
|
25 |
|
$this->configureClient($container, $name, $arguments); |
134
|
25 |
|
$clients[] = $name; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// If we have clients configured |
138
|
35 |
|
if (null !== $first) { |
139
|
|
|
// If we do not have a client named 'default' |
140
|
25 |
|
if (!array_key_exists('default', $config['clients'])) { |
141
|
25 |
|
$serviceId = 'httplug.client.'.$first; |
142
|
|
|
// Alias the first client to httplug.client.default |
143
|
25 |
|
$container->setAlias('httplug.client.default', $serviceId); |
144
|
25 |
|
$default = $first; |
145
|
|
|
} else { |
146
|
|
|
$default = 'default'; |
147
|
|
|
$serviceId = 'httplug.client.'.$default; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Autowiring alias for special clients, if they are enabled on the default client |
151
|
25 |
|
if ($config['clients'][$default]['flexible_client']) { |
152
|
2 |
|
$container->setAlias(FlexibleHttpClient::class, $serviceId.'.flexible'); |
153
|
|
|
} |
154
|
25 |
|
if ($config['clients'][$default]['http_methods_client']) { |
155
|
2 |
|
if (\interface_exists(HttpMethodsClientInterface::class)) { |
156
|
|
|
// support for client-common 1.9 |
157
|
2 |
|
$container->setAlias(HttpMethodsClientInterface::class, $serviceId.'.http_methods'); |
158
|
|
|
} |
159
|
|
|
} |
160
|
25 |
|
if ($config['clients'][$default]['batch_client']) { |
161
|
2 |
|
if (\interface_exists(BatchClientInterface::class)) { |
162
|
|
|
// support for client-common 1.9 |
163
|
2 |
|
$container->setAlias(BatchClientInterface::class, $serviceId.'.batch_client'); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
35 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Configure all Httplug plugins or remove their service definition. |
171
|
|
|
* |
172
|
|
|
* @param ContainerBuilder $container |
173
|
|
|
* @param array $config |
174
|
|
|
*/ |
175
|
35 |
|
private function configurePlugins(ContainerBuilder $container, array $config) |
176
|
|
|
{ |
177
|
35 |
|
if (!empty($config['authentication'])) { |
178
|
|
|
$this->configureAuthentication($container, $config['authentication']); |
179
|
|
|
} |
180
|
35 |
|
unset($config['authentication']); |
181
|
|
|
|
182
|
35 |
|
foreach ($config as $name => $pluginConfig) { |
183
|
35 |
|
$pluginId = 'httplug.plugin.'.$name; |
184
|
|
|
|
185
|
35 |
|
if ($this->isConfigEnabled($container, $pluginConfig)) { |
186
|
35 |
|
$def = $container->getDefinition($pluginId); |
187
|
35 |
|
$this->configurePluginByName($name, $def, $pluginConfig, $container, $pluginId); |
188
|
|
|
} |
189
|
|
|
} |
190
|
35 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $name |
194
|
|
|
* @param Definition $definition |
195
|
|
|
* @param array $config |
196
|
|
|
* @param ContainerBuilder $container In case we need to add additional services for this plugin |
197
|
|
|
* @param string $serviceId service id of the plugin, in case we need to add additional services for this plugin |
198
|
|
|
*/ |
199
|
35 |
|
private function configurePluginByName($name, Definition $definition, array $config, ContainerBuilder $container, $serviceId) |
200
|
|
|
{ |
201
|
35 |
|
switch ($name) { |
202
|
35 |
|
case 'cache': |
203
|
2 |
|
$options = $config['config']; |
204
|
2 |
|
if (!empty($options['cache_key_generator'])) { |
205
|
1 |
|
$options['cache_key_generator'] = new Reference($options['cache_key_generator']); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$definition |
209
|
2 |
|
->replaceArgument(0, new Reference($config['cache_pool'])) |
210
|
2 |
|
->replaceArgument(1, new Reference($config['stream_factory'])) |
211
|
2 |
|
->replaceArgument(2, $options); |
212
|
|
|
|
213
|
2 |
|
break; |
214
|
|
|
|
215
|
35 |
|
case 'cookie': |
216
|
|
|
$definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
217
|
|
|
|
218
|
|
|
break; |
219
|
|
|
|
220
|
35 |
|
case 'decoder': |
221
|
35 |
|
$definition->addArgument([ |
222
|
35 |
|
'use_content_encoding' => $config['use_content_encoding'], |
223
|
|
|
]); |
224
|
|
|
|
225
|
35 |
|
break; |
226
|
|
|
|
227
|
35 |
|
case 'history': |
228
|
|
|
$definition->replaceArgument(0, new Reference($config['journal'])); |
229
|
|
|
|
230
|
|
|
break; |
231
|
|
|
|
232
|
35 |
|
case 'logger': |
233
|
35 |
|
$definition->replaceArgument(0, new Reference($config['logger'])); |
234
|
35 |
|
if (!empty($config['formatter'])) { |
235
|
|
|
$definition->replaceArgument(1, new Reference($config['formatter'])); |
236
|
|
|
} |
237
|
|
|
|
238
|
35 |
|
break; |
239
|
|
|
|
240
|
35 |
|
case 'redirect': |
241
|
35 |
|
$definition->addArgument([ |
242
|
35 |
|
'preserve_header' => $config['preserve_header'], |
243
|
35 |
|
'use_default_for_multiple' => $config['use_default_for_multiple'], |
244
|
|
|
]); |
245
|
|
|
|
246
|
35 |
|
break; |
247
|
|
|
|
248
|
35 |
|
case 'retry': |
249
|
35 |
|
$definition->addArgument([ |
250
|
35 |
|
'retries' => $config['retry'], |
251
|
|
|
]); |
252
|
|
|
|
253
|
35 |
|
break; |
254
|
|
|
|
255
|
35 |
|
case 'stopwatch': |
256
|
35 |
|
$definition->replaceArgument(0, new Reference($config['stopwatch'])); |
257
|
|
|
|
258
|
35 |
|
break; |
259
|
|
|
|
260
|
|
|
/* client specific plugins */ |
261
|
|
|
|
262
|
7 |
View Code Duplication |
case 'add_host': |
|
|
|
|
263
|
6 |
|
$hostUriService = $serviceId.'.host_uri'; |
264
|
6 |
|
$this->createUri($container, $hostUriService, $config['host']); |
265
|
6 |
|
$definition->replaceArgument(0, new Reference($hostUriService)); |
266
|
6 |
|
$definition->replaceArgument(1, [ |
267
|
6 |
|
'replace' => $config['replace'], |
268
|
|
|
]); |
269
|
|
|
|
270
|
6 |
|
break; |
271
|
|
|
|
272
|
2 |
|
case 'add_path': |
273
|
|
|
$pathUriService = $serviceId.'.path_uri'; |
274
|
|
|
$this->createUri($container, $pathUriService, $config['path']); |
275
|
|
|
$definition->replaceArgument(0, new Reference($pathUriService)); |
276
|
|
|
|
277
|
|
|
break; |
278
|
|
|
|
279
|
2 |
View Code Duplication |
case 'base_uri': |
|
|
|
|
280
|
|
|
$baseUriService = $serviceId.'.base_uri'; |
281
|
|
|
$this->createUri($container, $baseUriService, $config['uri']); |
282
|
|
|
$definition->replaceArgument(0, new Reference($baseUriService)); |
283
|
|
|
$definition->replaceArgument(1, [ |
284
|
|
|
'replace' => $config['replace'], |
285
|
|
|
]); |
286
|
|
|
|
287
|
|
|
break; |
288
|
|
|
|
289
|
2 |
|
case 'content_type': |
290
|
2 |
|
unset($config['enabled']); |
291
|
2 |
|
$definition->replaceArgument(0, $config); |
292
|
|
|
|
293
|
2 |
|
break; |
294
|
|
|
|
295
|
1 |
|
case 'header_append': |
296
|
1 |
|
case 'header_defaults': |
297
|
1 |
|
case 'header_set': |
298
|
1 |
|
case 'header_remove': |
299
|
1 |
|
$definition->replaceArgument(0, $config['headers']); |
300
|
|
|
|
301
|
1 |
|
break; |
302
|
|
|
|
303
|
1 |
|
case 'query_defaults': |
304
|
1 |
|
$definition->replaceArgument(0, $config['parameters']); |
305
|
|
|
|
306
|
1 |
|
break; |
307
|
|
|
|
308
|
|
|
default: |
309
|
|
|
throw new \InvalidArgumentException(sprintf('Internal exception: Plugin %s is not handled', $name)); |
310
|
|
|
} |
311
|
35 |
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param ContainerBuilder $container |
315
|
|
|
* @param array $config |
316
|
|
|
* |
317
|
|
|
* @return array list of service ids for the authentication plugins |
318
|
|
|
*/ |
319
|
6 |
|
private function configureAuthentication(ContainerBuilder $container, array $config, $servicePrefix = 'httplug.plugin.authentication') |
320
|
|
|
{ |
321
|
6 |
|
$pluginServices = []; |
322
|
|
|
|
323
|
6 |
|
foreach ($config as $name => $values) { |
324
|
6 |
|
$authServiceKey = sprintf($servicePrefix.'.%s.auth', $name); |
325
|
6 |
|
switch ($values['type']) { |
326
|
6 |
|
case 'bearer': |
327
|
|
|
$container->register($authServiceKey, Bearer::class) |
328
|
|
|
->addArgument($values['token']); |
329
|
|
|
|
330
|
|
|
break; |
331
|
6 |
|
case 'basic': |
332
|
6 |
|
$container->register($authServiceKey, BasicAuth::class) |
333
|
6 |
|
->addArgument($values['username']) |
334
|
6 |
|
->addArgument($values['password']); |
335
|
|
|
|
336
|
6 |
|
break; |
337
|
|
|
case 'wsse': |
338
|
|
|
$container->register($authServiceKey, Wsse::class) |
339
|
|
|
->addArgument($values['username']) |
340
|
|
|
->addArgument($values['password']); |
341
|
|
|
|
342
|
|
|
break; |
343
|
|
|
case 'query_param': |
344
|
|
|
$container->register($authServiceKey, QueryParam::class) |
345
|
|
|
->addArgument($values['params']); |
346
|
|
|
|
347
|
|
|
break; |
348
|
|
|
case 'service': |
349
|
|
|
$authServiceKey = $values['service']; |
350
|
|
|
|
351
|
|
|
break; |
352
|
|
|
default: |
353
|
|
|
throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type'])); |
354
|
|
|
} |
355
|
|
|
|
356
|
6 |
|
$pluginServiceKey = $servicePrefix.'.'.$name; |
357
|
6 |
|
$container->register($pluginServiceKey, AuthenticationPlugin::class) |
358
|
6 |
|
->addArgument(new Reference($authServiceKey)) |
359
|
|
|
; |
360
|
6 |
|
$pluginServices[] = $pluginServiceKey; |
361
|
|
|
} |
362
|
|
|
|
363
|
6 |
|
return $pluginServices; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param ContainerBuilder $container |
368
|
|
|
* @param string $clientName |
369
|
|
|
* @param array $arguments |
370
|
|
|
*/ |
371
|
25 |
|
private function configureClient(ContainerBuilder $container, $clientName, array $arguments) |
372
|
|
|
{ |
373
|
25 |
|
$serviceId = 'httplug.client.'.$clientName; |
374
|
|
|
|
375
|
25 |
|
$plugins = []; |
376
|
25 |
|
foreach ($arguments['plugins'] as $plugin) { |
377
|
15 |
|
$pluginName = key($plugin); |
378
|
15 |
|
$pluginConfig = current($plugin); |
379
|
|
|
|
380
|
15 |
|
switch ($pluginName) { |
381
|
15 |
|
case 'reference': |
382
|
9 |
|
$plugins[] = $pluginConfig['id']; |
383
|
9 |
|
break; |
384
|
12 |
|
case 'authentication': |
385
|
6 |
|
$plugins = array_merge($plugins, $this->configureAuthentication($container, $pluginConfig, $serviceId.'.authentication')); |
386
|
6 |
|
break; |
387
|
12 |
|
case 'vcr': |
388
|
5 |
|
$this->useVcrPlugin = true; |
389
|
5 |
|
$plugins = array_merge($plugins, $this->configureVcrPlugin($container, $pluginConfig, $serviceId.'.vcr')); |
390
|
5 |
|
break; |
391
|
|
|
default: |
392
|
7 |
|
$plugins[] = $this->configurePlugin($container, $serviceId, $pluginName, $pluginConfig); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|
396
|
25 |
|
if (empty($arguments['service'])) { |
397
|
|
|
$container |
398
|
24 |
|
->register($serviceId.'.client', HttpClient::class) |
399
|
24 |
|
->setFactory([new Reference($arguments['factory']), 'createClient']) |
400
|
24 |
|
->addArgument($arguments['config']) |
401
|
24 |
|
->setPublic(false); |
402
|
|
|
} else { |
403
|
|
|
$container |
404
|
2 |
|
->setAlias($serviceId.'.client', new Alias($arguments['service'], false)); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
$definition = $container |
408
|
25 |
|
->register($serviceId, PluginClient::class) |
409
|
25 |
|
->setFactory([new Reference(PluginClientFactory::class), 'createClient']) |
410
|
25 |
|
->addArgument(new Reference($serviceId.'.client')) |
411
|
25 |
|
->addArgument( |
412
|
25 |
|
array_map( |
413
|
25 |
|
function ($id) { |
414
|
15 |
|
return new Reference($id); |
415
|
25 |
|
}, |
416
|
|
|
$plugins |
417
|
|
|
) |
418
|
|
|
) |
419
|
25 |
|
->addArgument([ |
420
|
25 |
|
'client_name' => $clientName, |
421
|
|
|
]) |
422
|
|
|
; |
423
|
|
|
|
424
|
25 |
|
if (is_bool($arguments['public'])) { |
425
|
5 |
|
$definition->setPublic($arguments['public']); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/* |
429
|
|
|
* Decorate the client with clients from client-common |
430
|
|
|
*/ |
431
|
25 |
View Code Duplication |
if ($arguments['flexible_client']) { |
|
|
|
|
432
|
|
|
$container |
433
|
2 |
|
->register($serviceId.'.flexible', FlexibleHttpClient::class) |
434
|
2 |
|
->addArgument(new Reference($serviceId.'.flexible.inner')) |
435
|
2 |
|
->setPublic($arguments['public'] ? true : false) |
436
|
2 |
|
->setDecoratedService($serviceId) |
437
|
|
|
; |
438
|
|
|
} |
439
|
|
|
|
440
|
25 |
View Code Duplication |
if ($arguments['http_methods_client']) { |
|
|
|
|
441
|
|
|
$container |
442
|
2 |
|
->register($serviceId.'.http_methods', HttpMethodsClient::class) |
443
|
2 |
|
->setArguments([new Reference($serviceId.'.http_methods.inner'), new Reference('httplug.message_factory')]) |
444
|
2 |
|
->setPublic($arguments['public'] ? true : false) |
445
|
2 |
|
->setDecoratedService($serviceId) |
446
|
|
|
; |
447
|
|
|
} |
448
|
|
|
|
449
|
25 |
View Code Duplication |
if ($arguments['batch_client']) { |
|
|
|
|
450
|
|
|
$container |
451
|
2 |
|
->register($serviceId.'.batch_client', BatchClient::class) |
452
|
2 |
|
->setArguments([new Reference($serviceId.'.batch_client.inner')]) |
453
|
2 |
|
->setPublic($arguments['public'] ? true : false) |
454
|
2 |
|
->setDecoratedService($serviceId) |
455
|
|
|
; |
456
|
|
|
} |
457
|
25 |
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Create a URI object with the default URI factory. |
461
|
|
|
* |
462
|
|
|
* @param ContainerBuilder $container |
463
|
|
|
* @param string $serviceId Name of the private service to create |
464
|
|
|
* @param string $uri String representation of the URI |
465
|
|
|
*/ |
466
|
6 |
|
private function createUri(ContainerBuilder $container, $serviceId, $uri) |
467
|
|
|
{ |
468
|
|
|
$container |
469
|
6 |
|
->register($serviceId, UriInterface::class) |
470
|
6 |
|
->setPublic(false) |
471
|
6 |
|
->setFactory([new Reference('httplug.uri_factory'), 'createUri']) |
472
|
6 |
|
->addArgument($uri) |
473
|
|
|
; |
474
|
6 |
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
478
|
|
|
* by finding a client using auto discovery. |
479
|
|
|
* |
480
|
|
|
* @param ContainerBuilder $container |
481
|
|
|
* @param array $config |
482
|
|
|
*/ |
483
|
35 |
|
private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
484
|
|
|
{ |
485
|
35 |
|
$httpClient = $config['discovery']['client']; |
486
|
35 |
View Code Duplication |
if ('auto' !== $httpClient) { |
|
|
|
|
487
|
2 |
|
$container->removeDefinition('httplug.auto_discovery.auto_discovered_client'); |
488
|
2 |
|
$container->removeDefinition('httplug.collector.auto_discovered_client'); |
489
|
|
|
|
490
|
2 |
|
if (!empty($httpClient)) { |
491
|
1 |
|
$container->setAlias('httplug.auto_discovery.auto_discovered_client', $httpClient); |
492
|
1 |
|
$container->getAlias('httplug.auto_discovery.auto_discovered_client')->setPublic(false); |
493
|
|
|
} |
494
|
|
|
} |
495
|
|
|
|
496
|
35 |
|
$asyncHttpClient = $config['discovery']['async_client']; |
497
|
35 |
View Code Duplication |
if ('auto' !== $asyncHttpClient) { |
|
|
|
|
498
|
32 |
|
$container->removeDefinition('httplug.auto_discovery.auto_discovered_async'); |
499
|
32 |
|
$container->removeDefinition('httplug.collector.auto_discovered_async'); |
500
|
|
|
|
501
|
32 |
|
if (!empty($asyncHttpClient)) { |
502
|
1 |
|
$container->setAlias('httplug.auto_discovery.auto_discovered_async', $asyncHttpClient); |
503
|
1 |
|
$container->getAlias('httplug.auto_discovery.auto_discovered_async')->setPublic(false); |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
|
507
|
35 |
|
if (null === $httpClient && null === $asyncHttpClient) { |
508
|
1 |
|
$container->removeDefinition('httplug.strategy'); |
509
|
|
|
|
510
|
1 |
|
return; |
511
|
|
|
} |
512
|
34 |
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* {@inheritdoc} |
516
|
|
|
*/ |
517
|
35 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
518
|
|
|
{ |
519
|
35 |
|
return new Configuration($container->getParameter('kernel.debug')); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Configure a plugin using the parent definition from plugins.xml. |
524
|
|
|
* |
525
|
|
|
* @param ContainerBuilder $container |
526
|
|
|
* @param string $serviceId |
527
|
|
|
* @param string $pluginName |
528
|
|
|
* @param array $pluginConfig |
529
|
|
|
* |
530
|
|
|
* @return string configured service id |
531
|
|
|
*/ |
532
|
7 |
|
private function configurePlugin(ContainerBuilder $container, $serviceId, $pluginName, array $pluginConfig) |
533
|
|
|
{ |
534
|
7 |
|
$pluginServiceId = $serviceId.'.plugin.'.$pluginName; |
535
|
|
|
|
536
|
7 |
|
$definition = $this->createChildDefinition('httplug.plugin.'.$pluginName); |
537
|
|
|
|
538
|
7 |
|
$this->configurePluginByName($pluginName, $definition, $pluginConfig, $container, $pluginServiceId); |
539
|
7 |
|
$container->setDefinition($pluginServiceId, $definition); |
540
|
|
|
|
541
|
7 |
|
return $pluginServiceId; |
542
|
|
|
} |
543
|
|
|
|
544
|
5 |
|
private function configureVcrPlugin(ContainerBuilder $container, array $config, $prefix) |
545
|
|
|
{ |
546
|
5 |
|
$recorder = $config['recorder']; |
547
|
5 |
|
$recorderId = in_array($recorder, ['filesystem', 'in_memory']) ? 'httplug.plugin.vcr.recorder.'.$recorder : $recorder; |
548
|
5 |
|
$namingStrategyId = $config['naming_strategy']; |
549
|
5 |
|
$replayId = $prefix.'.replay'; |
550
|
5 |
|
$recordId = $prefix.'.record'; |
551
|
|
|
|
552
|
5 |
|
if ('filesystem' === $recorder) { |
553
|
2 |
|
$recorderDefinition = $this->createChildDefinition('httplug.plugin.vcr.recorder.filesystem'); |
554
|
2 |
|
$recorderDefinition->replaceArgument(0, $config['fixtures_directory']); |
555
|
2 |
|
$recorderId = $prefix.'.recorder'; |
556
|
|
|
|
557
|
2 |
|
$container->setDefinition($recorderId, $recorderDefinition); |
558
|
|
|
} |
559
|
|
|
|
560
|
5 |
|
if ('default' === $config['naming_strategy']) { |
561
|
2 |
|
$namingStrategyId = $prefix.'.naming_strategy'; |
562
|
2 |
|
$namingStrategy = $this->createChildDefinition('httplug.plugin.vcr.naming_strategy.path'); |
563
|
|
|
|
564
|
2 |
|
if (!empty($config['naming_strategy_options'])) { |
565
|
1 |
|
$namingStrategy->setArguments([$config['naming_strategy_options']]); |
566
|
|
|
} |
567
|
|
|
|
568
|
2 |
|
$container->setDefinition($namingStrategyId, $namingStrategy); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
$arguments = [ |
572
|
5 |
|
new Reference($namingStrategyId), |
573
|
5 |
|
new Reference($recorderId), |
574
|
|
|
]; |
575
|
5 |
|
$record = new Definition(RecordPlugin::class, $arguments); |
576
|
5 |
|
$replay = new Definition(ReplayPlugin::class, $arguments); |
577
|
5 |
|
$plugins = []; |
578
|
|
|
|
579
|
5 |
|
switch ($config['mode']) { |
580
|
5 |
|
case 'replay': |
581
|
1 |
|
$container->setDefinition($replayId, $replay); |
582
|
1 |
|
$plugins[] = $replayId; |
583
|
1 |
|
break; |
584
|
4 |
|
case 'replay_or_record': |
585
|
3 |
|
$replay->setArgument(2, false); |
586
|
3 |
|
$container->setDefinition($replayId, $replay); |
587
|
3 |
|
$container->setDefinition($recordId, $record); |
588
|
3 |
|
$plugins[] = $replayId; |
589
|
3 |
|
$plugins[] = $recordId; |
590
|
3 |
|
break; |
591
|
1 |
|
case 'record': |
592
|
1 |
|
$container->setDefinition($recordId, $record); |
593
|
1 |
|
$plugins[] = $recordId; |
594
|
1 |
|
break; |
595
|
|
|
} |
596
|
|
|
|
597
|
5 |
|
return $plugins; |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* BC for old Symfony versions. Remove this method and use new ChildDefinition directly when we drop support for Symfony 2. |
602
|
|
|
* |
603
|
|
|
* @param string $parent the parent service id |
604
|
|
|
* |
605
|
|
|
* @return ChildDefinition|DefinitionDecorator |
606
|
|
|
*/ |
607
|
9 |
|
private function createChildDefinition($parent) |
608
|
|
|
{ |
609
|
9 |
|
$definitionClass = class_exists(ChildDefinition::class) ? ChildDefinition::class : DefinitionDecorator::class; |
610
|
|
|
|
611
|
9 |
|
return new $definitionClass($parent); |
612
|
|
|
} |
613
|
|
|
} |
614
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.