9
9
use Http \Discovery \HttpClientDiscovery ;
10
10
use Http \HttplugBundle \ClientFactory \DummyClient ;
11
11
use Http \HttplugBundle \ClientFactory \PluginClientFactory ;
12
+ use Http \HttplugBundle \Collector \DebugPlugin ;
12
13
use Http \Message \Authentication \BasicAuth ;
13
14
use Http \Message \Authentication \Bearer ;
14
15
use Http \Message \Authentication \Wsse ;
@@ -38,6 +39,7 @@ public function load(array $configs, ContainerBuilder $container)
38
39
$ loader ->load ('services.xml ' );
39
40
$ loader ->load ('plugins.xml ' );
40
41
42
+ // Register default services
41
43
foreach ($ config ['classes ' ] as $ service => $ class ) {
42
44
if (!empty ($ class )) {
43
45
$ container ->register (sprintf ('httplug.%s.default ' , $ service ), $ class );
@@ -49,27 +51,37 @@ public function load(array $configs, ContainerBuilder $container)
49
51
$ container ->setAlias (sprintf ('httplug.%s ' , $ type ), $ id );
50
52
}
51
53
52
- $ this ->configurePlugins ($ container , $ config ['plugins ' ]);
53
- $ serviceIds = $ this ->configureClients ($ container , $ config );
54
- $ autoServiceIds = $ this ->configureAutoDiscoveryClients ($ container , $ config );
54
+ // Configure toolbar
55
+ if ($ config ['toolbar ' ]['enabled ' ]) {
56
+ $ loader ->load ('data-collector.xml ' );
57
+
58
+ if (!empty ($ config ['toolbar ' ]['formatter ' ])) {
59
+ // Add custom formatter
60
+ $ container
61
+ ->getDefinition ('httplug.collector.debug_collector ' )
62
+ ->replaceArgument (0 , new Reference ($ config ['toolbar ' ]['formatter ' ]))
63
+ ;
64
+ }
55
65
56
- $ toolbar = is_bool ($ config ['toolbar ' ]['enabled ' ]) ? $ config ['toolbar ' ]['enabled ' ] : $ container ->hasParameter ('kernel.debug ' ) && $ container ->getParameter ('kernel.debug ' );
57
- if ($ toolbar ) {
58
- (new ProfilerExtension ())->load ($ config , $ container , array_unique (array_merge ($ serviceIds , $ autoServiceIds )));
66
+ $ container
67
+ ->getDefinition ('httplug.formatter.full_http_message ' )
68
+ ->addArgument ($ config ['toolbar ' ]['captured_body_length ' ])
69
+ ;
59
70
}
71
+
72
+ $ this ->configurePlugins ($ container , $ config ['plugins ' ]);
73
+ $ this ->configureClients ($ container , $ config );
74
+ $ this ->configureAutoDiscoveryClients ($ container , $ config );
60
75
}
61
76
62
77
/**
63
78
* Configure client services.
64
79
*
65
80
* @param ContainerBuilder $container
66
81
* @param array $config
67
- *
68
- * @return array with client service names
69
82
*/
70
83
private function configureClients (ContainerBuilder $ container , array $ config )
71
84
{
72
- $ serviceIds = [];
73
85
$ first = null ;
74
86
75
87
foreach ($ config ['clients ' ] as $ name => $ arguments ) {
@@ -78,7 +90,7 @@ private function configureClients(ContainerBuilder $container, array $config)
78
90
$ first = $ name ;
79
91
}
80
92
81
- $ serviceIds [] = $ this ->configureClient ($ container , $ name , $ arguments );
93
+ $ this ->configureClient ($ container , $ name , $ arguments, $ config [ ' toolbar ' ][ ' enabled ' ] );
82
94
}
83
95
84
96
// If we have clients configured
@@ -89,8 +101,6 @@ private function configureClients(ContainerBuilder $container, array $config)
89
101
$ container ->setAlias ('httplug.client.default ' , 'httplug.client. ' .$ first );
90
102
}
91
103
}
92
-
93
- return $ serviceIds ;
94
104
}
95
105
96
106
/**
@@ -198,14 +208,29 @@ private function configureAuthentication(ContainerBuilder $container, array $con
198
208
* @param ContainerBuilder $container
199
209
* @param string $name
200
210
* @param array $arguments
201
- *
202
- * @return string The service id of the client.
211
+ * @param bool $profiling
203
212
*/
204
- private function configureClient (ContainerBuilder $ container , $ name , array $ arguments )
213
+ private function configureClient (ContainerBuilder $ container , $ name , array $ arguments, $ profiling )
205
214
{
206
215
$ serviceId = 'httplug.client. ' .$ name ;
207
- $ definition = $ container ->register ($ serviceId , DummyClient::class);
208
- $ definition ->setFactory ([PluginClientFactory::class, 'createPluginClient ' ])
216
+
217
+ $ clientOptions = [];
218
+
219
+ if ($ profiling ) {
220
+ // Tell the plugin journal what plugins we used
221
+ $ container
222
+ ->getDefinition ('httplug.collector.plugin_journal ' )
223
+ ->addMethodCall ('setPlugins ' , [$ name , $ arguments ['plugins ' ]])
224
+ ;
225
+
226
+ $ debugPluginServiceId = $ this ->registerDebugPlugin ($ container , $ serviceId );
227
+
228
+ $ clientOptions ['debug_plugins ' ] = [new Reference ($ debugPluginServiceId )];
229
+ }
230
+
231
+ $ container
232
+ ->register ($ serviceId , DummyClient::class)
233
+ ->setFactory ([PluginClientFactory::class, 'createPluginClient ' ])
209
234
->addArgument (
210
235
array_map (
211
236
function ($ id ) {
@@ -216,31 +241,30 @@ function ($id) {
216
241
)
217
242
->addArgument (new Reference ($ arguments ['factory ' ]))
218
243
->addArgument ($ arguments ['config ' ])
219
- ->addArgument ([] )
244
+ ->addArgument ($ clientOptions )
220
245
;
221
246
222
- // Tell the plugin journal what plugins we used
223
- $ container ->getDefinition ('httplug.collector.plugin_journal ' )
224
- ->addMethodCall ('setPlugins ' , [$ name , $ arguments ['plugins ' ]]);
225
247
226
248
/*
227
249
* Decorate the client with clients from client-common
228
250
*/
229
251
if ($ arguments ['flexible_client ' ]) {
230
- $ container ->register ($ serviceId .'.flexible ' , FlexibleHttpClient::class)
252
+ $ container
253
+ ->register ($ serviceId .'.flexible ' , FlexibleHttpClient::class)
231
254
->addArgument (new Reference ($ serviceId .'.flexible.inner ' ))
232
255
->setPublic (false )
233
- ->setDecoratedService ($ serviceId );
256
+ ->setDecoratedService ($ serviceId )
257
+ ;
234
258
}
235
259
236
260
if ($ arguments ['http_methods_client ' ]) {
237
- $ container ->register ($ serviceId .'.http_methods ' , HttpMethodsClient::class)
261
+ $ container
262
+ ->register ($ serviceId .'.http_methods ' , HttpMethodsClient::class)
238
263
->setArguments ([new Reference ($ serviceId .'.http_methods.inner ' ), new Reference ('httplug.message_factory ' )])
239
264
->setPublic (false )
240
- ->setDecoratedService ($ serviceId );
265
+ ->setDecoratedService ($ serviceId )
266
+ ;
241
267
}
242
-
243
- return $ serviceId ;
244
268
}
245
269
246
270
/**
@@ -249,45 +273,44 @@ function ($id) {
249
273
*
250
274
* @param ContainerBuilder $container
251
275
* @param array $config
252
- *
253
- * @return array of service ids.
254
276
*/
255
277
private function configureAutoDiscoveryClients (ContainerBuilder $ container , array $ config )
256
278
{
257
- $ serviceIds = [];
258
-
259
279
$ httpClient = $ config ['discovery ' ]['client ' ];
280
+
260
281
if (!empty ($ httpClient )) {
261
282
if ($ httpClient === 'auto ' ) {
262
283
$ httpClient = $ this ->registerAutoDiscoverableClient (
263
284
$ container ,
264
285
'auto_discovered_client ' ,
265
- [HttpClientDiscovery::class, 'find ' ]
286
+ [HttpClientDiscovery::class, 'find ' ],
287
+ $ config ['toolbar ' ]['enabled ' ]
266
288
);
267
289
}
268
290
269
- $ serviceIds [] = $ httpClient ;
270
291
$ httpClient = new Reference ($ httpClient );
271
292
}
272
293
273
294
$ asyncHttpClient = $ config ['discovery ' ]['async_client ' ];
295
+
274
296
if (!empty ($ asyncHttpClient )) {
275
297
if ($ asyncHttpClient === 'auto ' ) {
276
298
$ asyncHttpClient = $ this ->registerAutoDiscoverableClient (
277
299
$ container ,
278
300
'auto_discovered_async ' ,
279
- [HttpAsyncClientDiscovery::class, 'find ' ]
301
+ [HttpAsyncClientDiscovery::class, 'find ' ],
302
+ $ config ['toolbar ' ]['enabled ' ]
280
303
);
281
304
}
282
- $ serviceIds [] = $ asyncHttpClient ;
283
- $ asyncHttpClient = new Reference ($ httpClient );
305
+
306
+ $ asyncHttpClient = new Reference ($ asyncHttpClient );
284
307
}
285
308
286
- $ container ->getDefinition ('httplug.strategy ' )
309
+ $ container
310
+ ->getDefinition ('httplug.strategy ' )
287
311
->addArgument ($ httpClient )
288
- ->addArgument ($ asyncHttpClient );
289
-
290
- return $ serviceIds ;
312
+ ->addArgument ($ asyncHttpClient )
313
+ ;
291
314
}
292
315
293
316
/**
@@ -296,17 +319,58 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra
296
319
* @param ContainerBuilder $container
297
320
* @param string $name
298
321
* @param callable $factory
322
+ * @param bool $profiling
299
323
*
300
324
* @return string service id
301
325
*/
302
- private function registerAutoDiscoverableClient (ContainerBuilder $ container , $ name , $ factory )
326
+ private function registerAutoDiscoverableClient (ContainerBuilder $ container , $ name , $ factory, $ profiling )
303
327
{
304
328
$ serviceId = 'httplug.auto_discovery. ' .$ name ;
305
- $ definition = $ container ->register ($ serviceId , DummyClient::class);
306
- $ definition
329
+
330
+ $ clientOptions = [];
331
+
332
+ if ($ profiling ) {
333
+ $ debugPluginServiceId = $ this ->registerDebugPlugin ($ container , $ serviceId );
334
+
335
+ $ clientOptions ['debug_plugins ' ] = [new Reference ($ debugPluginServiceId )];
336
+ }
337
+
338
+ $ container
339
+ ->register ($ serviceId , DummyClient::class)
307
340
->setFactory ([PluginClientFactory::class, 'createPluginClient ' ])
308
- ->setArguments ([[], $ factory , [], []]);
341
+ ->setArguments ([[], $ factory , [], $ clientOptions ])
342
+ ;
309
343
310
344
return $ serviceId ;
311
345
}
346
+
347
+ /**
348
+ * Create a new plugin service for this client.
349
+ *
350
+ * @param ContainerBuilder $container
351
+ * @param string $name
352
+ *
353
+ * @return string
354
+ */
355
+ private function registerDebugPlugin (ContainerBuilder $ container , $ name )
356
+ {
357
+ $ serviceIdDebugPlugin = $ name .'.debug_plugin ' ;
358
+
359
+ $ container
360
+ ->register ($ serviceIdDebugPlugin , DebugPlugin::class)
361
+ ->addArgument (new Reference ('httplug.collector.debug_collector ' ))
362
+ ->addArgument (substr ($ name , strrpos ($ name , '. ' ) + 1 ))
363
+ ->setPublic (false )
364
+ ;
365
+
366
+ return $ serviceIdDebugPlugin ;
367
+ }
368
+
369
+ /**
370
+ * {@inheritdoc}
371
+ */
372
+ public function getConfiguration (array $ config , ContainerBuilder $ container )
373
+ {
374
+ return new Configuration ($ container ->getParameter ('kernel.debug ' ));
375
+ }
312
376
}
0 commit comments