5
5
use Http \Client \Common \FlexibleHttpClient ;
6
6
use Http \Client \Common \HttpMethodsClient ;
7
7
use Http \Client \Common \Plugin \AuthenticationPlugin ;
8
- use Http \Client \Common \PluginClient ;
9
8
use Http \Discovery \HttpAsyncClientDiscovery ;
10
9
use Http \Discovery \HttpClientDiscovery ;
11
10
use Http \HttplugBundle \ClientFactory \DummyClient ;
12
- use Http \HttplugBundle \Collector \ DebugPlugin ;
11
+ use Http \HttplugBundle \ClientFactory \ PluginClientFactory ;
13
12
use Http \Message \Authentication \BasicAuth ;
14
13
use Http \Message \Authentication \Bearer ;
15
14
use Http \Message \Authentication \Wsse ;
@@ -39,21 +38,6 @@ public function load(array $configs, ContainerBuilder $container)
39
38
$ loader ->load ('services.xml ' );
40
39
$ loader ->load ('plugins.xml ' );
41
40
42
- $ toolbar = is_bool ($ config ['toolbar ' ]['enabled ' ]) ? $ config ['toolbar ' ]['enabled ' ] : $ container ->hasParameter ('kernel.debug ' ) && $ container ->getParameter ('kernel.debug ' );
43
-
44
- if ($ toolbar ) {
45
- $ loader ->load ('data-collector.xml ' );
46
-
47
- if (!empty ($ config ['toolbar ' ]['formatter ' ])) {
48
- // Add custom formatter
49
- $ container ->getDefinition ('httplug.collector.debug_collector ' )
50
- ->replaceArgument (0 , new Reference ($ config ['toolbar ' ]['formatter ' ]));
51
- }
52
-
53
- $ container ->getDefinition ('httplug.formatter.full_http_message ' )
54
- ->addArgument ($ config ['toolbar ' ]['captured_body_length ' ]);
55
- }
56
-
57
41
foreach ($ config ['classes ' ] as $ service => $ class ) {
58
42
if (!empty ($ class )) {
59
43
$ container ->register (sprintf ('httplug.%s.default ' , $ service ), $ class );
@@ -66,45 +50,47 @@ public function load(array $configs, ContainerBuilder $container)
66
50
}
67
51
68
52
$ this ->configurePlugins ($ container , $ config ['plugins ' ]);
69
- $ this ->configureClients ($ container , $ config , $ toolbar );
70
- $ this ->configureAutoDiscoveryClients ($ container , $ config , $ toolbar );
53
+ $ serviceIds = $ this ->configureClients ($ container , $ config );
54
+ $ autoServiceIds = $ this ->configureAutoDiscoveryClients ($ container , $ config );
55
+
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 )));
59
+ }
71
60
}
72
61
73
62
/**
74
63
* Configure client services.
75
64
*
76
65
* @param ContainerBuilder $container
77
66
* @param array $config
78
- * @param bool $enableCollector
67
+ *
68
+ * @raturn array with client service names
79
69
*/
80
- private function configureClients (ContainerBuilder $ container , array $ config, $ enableCollector )
70
+ private function configureClients (ContainerBuilder $ container , array $ config )
81
71
{
82
- // If we have a client named 'default'
83
- $ first = isset ( $ config [ ' clients ' ][ ' default ' ]) ? ' default ' : null ;
72
+ $ serviceIds = [];
73
+ $ first = null ;
84
74
85
75
foreach ($ config ['clients ' ] as $ name => $ arguments ) {
86
76
if ($ first === null ) {
87
77
// Save the name of the first configurated client.
88
78
$ first = $ name ;
89
79
}
90
80
91
- $ this ->configureClient ($ container , $ name , $ arguments, $ enableCollector );
81
+ $ serviceIds [] = $ this ->configureClient ($ container , $ name , $ arguments );
92
82
}
93
83
94
84
// If we have clients configured
95
85
if ($ first !== null ) {
96
- if ($ first !== 'default ' ) {
86
+ // If we do not have a client named 'default'
87
+ if (!isset ($ config ['clients ' ]['default ' ])) {
97
88
// Alias the first client to httplug.client.default
98
89
$ container ->setAlias ('httplug.client.default ' , 'httplug.client. ' .$ first );
99
90
}
100
- } elseif ($ enableCollector ) {
101
- $ serviceIdDebugPlugin = $ this ->registerDebugPlugin ($ container , 'default ' );
102
- // No client was configured. Make sure to configure the auto discovery client with the PluginClient.
103
- $ container ->register ('httplug.client ' , PluginClient::class)
104
- ->addArgument (new Reference ('httplug.client.default ' ))
105
- ->addArgument ([])
106
- ->addArgument (['debug_plugins ' => [new Reference ($ serviceIdDebugPlugin )]]);
107
91
}
92
+
93
+ return $ serviceIds ;
108
94
}
109
95
110
96
/**
@@ -212,46 +198,34 @@ private function configureAuthentication(ContainerBuilder $container, array $con
212
198
* @param ContainerBuilder $container
213
199
* @param string $name
214
200
* @param array $arguments
215
- * @param bool $enableCollector
201
+ *
202
+ * @return string The service id of the client.
216
203
*/
217
- private function configureClient (ContainerBuilder $ container , $ name , array $ arguments, $ enableCollector )
204
+ private function configureClient (ContainerBuilder $ container , $ name , array $ arguments )
218
205
{
219
206
$ serviceId = 'httplug.client. ' .$ name ;
220
- $ def = $ container ->register ($ serviceId , DummyClient::class);
221
-
222
- // If there are no plugins nor should we use the data collector
223
- if (empty ($ arguments ['plugins ' ]) && !$ enableCollector ) {
224
- $ def ->setFactory ([new Reference ($ arguments ['factory ' ]), 'createClient ' ])
225
- ->addArgument ($ arguments ['config ' ]);
226
- } else {
227
- $ def
228
- ->setFactory ('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient ' )
229
- ->addArgument (
230
- array_map (
231
- function ($ id ) {
232
- return new Reference ($ id );
233
- },
234
- $ arguments ['plugins ' ]
235
- )
207
+ $ definition = $ container ->register ($ serviceId , DummyClient::class);
208
+ $ definition ->setFactory ([PluginClientFactory::class, 'createPluginClient ' ])
209
+ ->addArgument (
210
+ array_map (
211
+ function ($ id ) {
212
+ return new Reference ($ id );
213
+ },
214
+ $ arguments ['plugins ' ]
236
215
)
237
- ->addArgument (new Reference ($ arguments ['factory ' ]))
238
- ->addArgument ($ arguments ['config ' ])
239
- ;
240
-
241
- if ($ enableCollector ) {
242
- $ serviceIdDebugPlugin = $ this ->registerDebugPlugin ($ container , $ name );
243
- $ def ->addArgument (['debug_plugins ' => [new Reference ($ serviceIdDebugPlugin )]]);
216
+ )
217
+ ->addArgument (new Reference ($ arguments ['factory ' ]))
218
+ ->addArgument ($ arguments ['config ' ])
219
+ ->addArgument ([])
220
+ ;
244
221
245
- // tell the plugin journal what plugins we used
246
- $ container ->getDefinition ('httplug.collector.plugin_journal ' )
247
- ->addMethodCall ('setPlugins ' , [$ name , $ arguments ['plugins ' ]]);
248
- }
249
- }
222
+ // Tell the plugin journal what plugins we used
223
+ $ container ->getDefinition ('httplug.collector.plugin_journal ' )
224
+ ->addMethodCall ('setPlugins ' , [$ name , $ arguments ['plugins ' ]]);
250
225
251
226
/*
252
227
* Decorate the client with clients from client-common
253
228
*/
254
-
255
229
if ($ arguments ['flexible_client ' ]) {
256
230
$ container ->register ($ serviceId .'.flexible ' , FlexibleHttpClient::class)
257
231
->addArgument (new Reference ($ serviceId .'.flexible.inner ' ))
@@ -265,91 +239,74 @@ function ($id) {
265
239
->setPublic (false )
266
240
->setDecoratedService ($ serviceId );
267
241
}
268
- }
269
242
270
- /**
271
- * Create a new plugin service for this client.
272
- *
273
- * @param ContainerBuilder $container
274
- * @param string $name
275
- *
276
- * @return string
277
- */
278
- private function registerDebugPlugin (ContainerBuilder $ container , $ name )
279
- {
280
- $ serviceIdDebugPlugin = 'httplug.client. ' .$ name .'.debug_plugin ' ;
281
- $ container ->register ($ serviceIdDebugPlugin , DebugPlugin::class)
282
- ->addArgument (new Reference ('httplug.collector.debug_collector ' ))
283
- ->addArgument ($ name )
284
- ->setPublic (false );
285
-
286
- return $ serviceIdDebugPlugin ;
243
+ return $ serviceId ;
287
244
}
288
245
289
246
/**
290
- * Make sure we inject the debug plugin for clients found by auto discovery.
247
+ * Make the user can select what client is used for auto discovery. If none is provided, a service will be created
248
+ * by finding a client using auto discovery.
291
249
*
292
250
* @param ContainerBuilder $container
293
251
* @param array $config
294
- * @param bool $enableCollector
295
- */
296
- private function configureAutoDiscoveryClients (ContainerBuilder $ container , array $ config , $ enableCollector )
252
+ *
253
+ * @return array of service ids.
254
+ * */
255
+ private function configureAutoDiscoveryClients (ContainerBuilder $ container , array $ config )
297
256
{
257
+ $ serviceIds = [];
258
+
298
259
$ httpClient = $ config ['discovery ' ]['client ' ];
299
- if ($ httpClient === 'auto ' ) {
300
- $ httpClient = $ this ->registerAutoDiscoverableClientWithDebugPlugin (
301
- $ container ,
302
- 'client ' ,
303
- [HttpClientDiscovery::class, 'find ' ],
304
- $ enableCollector
305
- );
306
- } elseif ($ httpClient ) {
260
+ if (!empty ($ httpClient )) {
261
+ if ($ httpClient === 'auto ' ) {
262
+ $ httpClient = $ this ->registerAutoDiscoverableClient (
263
+ $ container ,
264
+ 'auto_discovered_client ' ,
265
+ [HttpClientDiscovery::class, 'find ' ]
266
+ );
267
+ }
268
+
269
+ $ serviceIds [] = $ httpClient ;
307
270
$ httpClient = new Reference ($ httpClient );
308
271
}
309
272
310
273
$ asyncHttpClient = $ config ['discovery ' ]['async_client ' ];
311
- if ($ asyncHttpClient === 'auto ' ) {
312
- $ asyncHttpClient = $ this ->registerAutoDiscoverableClientWithDebugPlugin (
313
- $ container ,
314
- 'async_client ' ,
315
- [HttpAsyncClientDiscovery::class, 'find ' ],
316
- $ enableCollector
317
- );
318
- } elseif ($ asyncHttpClient ) {
274
+ if (!empty ($ asyncHttpClient )) {
275
+ if ($ asyncHttpClient === 'auto ' ) {
276
+ $ asyncHttpClient = $ this ->registerAutoDiscoverableClient (
277
+ $ container ,
278
+ 'auto_discovered_async ' ,
279
+ [HttpAsyncClientDiscovery::class, 'find ' ]
280
+ );
281
+ }
282
+ $ serviceIds [] = $ asyncHttpClient ;
319
283
$ asyncHttpClient = new Reference ($ httpClient );
320
284
}
321
285
322
286
$ container ->getDefinition ('httplug.strategy ' )
323
287
->addArgument ($ httpClient )
324
288
->addArgument ($ asyncHttpClient );
289
+
290
+ return $ serviceIds ;
325
291
}
326
292
327
293
/**
294
+ * Find a client with auto discovery and return a service Reference to it.
295
+ *
328
296
* @param ContainerBuilder $container
329
297
* @param string $name
330
298
* @param callable $factory
331
- * @param bool $enableCollector
332
299
*
333
- * @return Reference
300
+ * @return string service id
334
301
*/
335
- private function registerAutoDiscoverableClientWithDebugPlugin (ContainerBuilder $ container , $ name , $ factory, $ enableCollector )
302
+ private function registerAutoDiscoverableClient (ContainerBuilder $ container , $ name , $ factory )
336
303
{
337
- $ definition = $ container ->register ('httplug.auto_discovery_ ' .$ name .'.pure ' , DummyClient::class);
338
- $ definition ->setPublic (false );
339
- $ definition ->setFactory ($ factory );
340
-
341
- $ pluginDefinition = $ container
342
- ->register ('httplug.auto_discovery_ ' .$ name .'.plugin ' , PluginClient::class)
343
- ->setPublic (false )
344
- ->addArgument (new Reference ('httplug.auto_discovery_ ' .$ name .'.pure ' ))
345
- ->addArgument ([])
346
- ;
347
-
348
- if ($ enableCollector ) {
349
- $ serviceIdDebugPlugin = $ this ->registerDebugPlugin ($ container , 'auto_discovery_ ' .$ name );
350
- $ pluginDefinition ->addArgument (['debug_plugins ' => [new Reference ($ serviceIdDebugPlugin )]]);
351
- }
304
+ $ serviceId = 'httplug.auto_discovery. ' .$ name ;
305
+ $ definition = $ container ->register ($ serviceId , DummyClient::class);
306
+ $ definition
307
+ ->setFactory ([PluginClientFactory::class, 'createPluginClient ' ])
308
+ ->setArguments ([[], $ factory , [], []]);
352
309
353
- return new Reference ( ' httplug.auto_discovery_ ' . $ name . ' .plugin ' ) ;
310
+ return $ serviceId ;
354
311
}
355
312
}
0 commit comments