99use Http \Discovery \HttpClientDiscovery ;
1010use Http \HttplugBundle \ClientFactory \DummyClient ;
1111use Http \HttplugBundle \ClientFactory \PluginClientFactory ;
12+ use Http \HttplugBundle \Collector \DebugPlugin ;
1213use Http \Message \Authentication \BasicAuth ;
1314use Http \Message \Authentication \Bearer ;
1415use Http \Message \Authentication \Wsse ;
@@ -38,6 +39,7 @@ public function load(array $configs, ContainerBuilder $container)
3839 $ loader ->load ('services.xml ' );
3940 $ loader ->load ('plugins.xml ' );
4041
42+ // Register default services
4143 foreach ($ config ['classes ' ] as $ service => $ class ) {
4244 if (!empty ($ class )) {
4345 $ container ->register (sprintf ('httplug.%s.default ' , $ service ), $ class );
@@ -49,27 +51,37 @@ public function load(array $configs, ContainerBuilder $container)
4951 $ container ->setAlias (sprintf ('httplug.%s ' , $ type ), $ id );
5052 }
5153
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+ }
5565
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+ ;
5970 }
71+
72+ $ this ->configurePlugins ($ container , $ config ['plugins ' ]);
73+ $ this ->configureClients ($ container , $ config );
74+ $ this ->configureAutoDiscoveryClients ($ container , $ config );
6075 }
6176
6277 /**
6378 * Configure client services.
6479 *
6580 * @param ContainerBuilder $container
6681 * @param array $config
67- *
68- * @return array with client service names
6982 */
7083 private function configureClients (ContainerBuilder $ container , array $ config )
7184 {
72- $ serviceIds = [];
7385 $ first = null ;
7486
7587 foreach ($ config ['clients ' ] as $ name => $ arguments ) {
@@ -78,7 +90,7 @@ private function configureClients(ContainerBuilder $container, array $config)
7890 $ first = $ name ;
7991 }
8092
81- $ serviceIds [] = $ this ->configureClient ($ container , $ name , $ arguments );
93+ $ this ->configureClient ($ container , $ name , $ arguments, $ config [ ' toolbar ' ][ ' enabled ' ] );
8294 }
8395
8496 // If we have clients configured
@@ -89,8 +101,6 @@ private function configureClients(ContainerBuilder $container, array $config)
89101 $ container ->setAlias ('httplug.client.default ' , 'httplug.client. ' .$ first );
90102 }
91103 }
92-
93- return $ serviceIds ;
94104 }
95105
96106 /**
@@ -198,14 +208,29 @@ private function configureAuthentication(ContainerBuilder $container, array $con
198208 * @param ContainerBuilder $container
199209 * @param string $name
200210 * @param array $arguments
201- *
202- * @return string The service id of the client.
211+ * @param bool $profiling
203212 */
204- private function configureClient (ContainerBuilder $ container , $ name , array $ arguments )
213+ private function configureClient (ContainerBuilder $ container , $ name , array $ arguments, $ profiling )
205214 {
206215 $ serviceId = 'httplug.client. ' .$ name ;
207- $ definition = $ container ->register ($ serviceId , DummyClient::class);
208- $ definition ->setFactory ([PluginClientFactory::class, 'createPluginClient ' ])
216+
217+ $ pluginClientOptions = [];
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+ $ pluginClientOptions ['debug_plugins ' ] = [new Reference ($ debugPluginServiceId )];
229+ }
230+
231+ $ container
232+ ->register ($ serviceId , DummyClient::class)
233+ ->setFactory ([PluginClientFactory::class, 'createPluginClient ' ])
209234 ->addArgument (
210235 array_map (
211236 function ($ id ) {
@@ -216,31 +241,30 @@ function ($id) {
216241 )
217242 ->addArgument (new Reference ($ arguments ['factory ' ]))
218243 ->addArgument ($ arguments ['config ' ])
219- ->addArgument ([] )
244+ ->addArgument ($ pluginClientOptions )
220245 ;
221246
222- // Tell the plugin journal what plugins we used
223- $ container ->getDefinition ('httplug.collector.plugin_journal ' )
224- ->addMethodCall ('setPlugins ' , [$ name , $ arguments ['plugins ' ]]);
225247
226248 /*
227249 * Decorate the client with clients from client-common
228250 */
229251 if ($ arguments ['flexible_client ' ]) {
230- $ container ->register ($ serviceId .'.flexible ' , FlexibleHttpClient::class)
252+ $ container
253+ ->register ($ serviceId .'.flexible ' , FlexibleHttpClient::class)
231254 ->addArgument (new Reference ($ serviceId .'.flexible.inner ' ))
232255 ->setPublic (false )
233- ->setDecoratedService ($ serviceId );
256+ ->setDecoratedService ($ serviceId )
257+ ;
234258 }
235259
236260 if ($ arguments ['http_methods_client ' ]) {
237- $ container ->register ($ serviceId .'.http_methods ' , HttpMethodsClient::class)
261+ $ container
262+ ->register ($ serviceId .'.http_methods ' , HttpMethodsClient::class)
238263 ->setArguments ([new Reference ($ serviceId .'.http_methods.inner ' ), new Reference ('httplug.message_factory ' )])
239264 ->setPublic (false )
240- ->setDecoratedService ($ serviceId );
265+ ->setDecoratedService ($ serviceId )
266+ ;
241267 }
242-
243- return $ serviceId ;
244268 }
245269
246270 /**
@@ -249,45 +273,44 @@ function ($id) {
249273 *
250274 * @param ContainerBuilder $container
251275 * @param array $config
252- *
253- * @return array of service ids.
254276 */
255277 private function configureAutoDiscoveryClients (ContainerBuilder $ container , array $ config )
256278 {
257- $ serviceIds = [];
258-
259279 $ httpClient = $ config ['discovery ' ]['client ' ];
280+
260281 if (!empty ($ httpClient )) {
261282 if ($ httpClient === 'auto ' ) {
262283 $ httpClient = $ this ->registerAutoDiscoverableClient (
263284 $ container ,
264285 'auto_discovered_client ' ,
265- [HttpClientDiscovery::class, 'find ' ]
286+ [HttpClientDiscovery::class, 'find ' ],
287+ $ config ['toolbar ' ]['enabled ' ]
266288 );
267289 }
268290
269- $ serviceIds [] = $ httpClient ;
270291 $ httpClient = new Reference ($ httpClient );
271292 }
272293
273294 $ asyncHttpClient = $ config ['discovery ' ]['async_client ' ];
295+
274296 if (!empty ($ asyncHttpClient )) {
275297 if ($ asyncHttpClient === 'auto ' ) {
276298 $ asyncHttpClient = $ this ->registerAutoDiscoverableClient (
277299 $ container ,
278300 'auto_discovered_async ' ,
279- [HttpAsyncClientDiscovery::class, 'find ' ]
301+ [HttpAsyncClientDiscovery::class, 'find ' ],
302+ $ config ['toolbar ' ]['enabled ' ]
280303 );
281304 }
282- $ serviceIds [] = $ asyncHttpClient ;
283- $ asyncHttpClient = new Reference ($ httpClient );
305+
306+ $ asyncHttpClient = new Reference ($ asyncHttpClient );
284307 }
285308
286- $ container ->getDefinition ('httplug.strategy ' )
309+ $ container
310+ ->getDefinition ('httplug.strategy ' )
287311 ->addArgument ($ httpClient )
288- ->addArgument ($ asyncHttpClient );
289-
290- return $ serviceIds ;
312+ ->addArgument ($ asyncHttpClient )
313+ ;
291314 }
292315
293316 /**
@@ -296,17 +319,58 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra
296319 * @param ContainerBuilder $container
297320 * @param string $name
298321 * @param callable $factory
322+ * @param bool $profiling
299323 *
300324 * @return string service id
301325 */
302- private function registerAutoDiscoverableClient (ContainerBuilder $ container , $ name , $ factory )
326+ private function registerAutoDiscoverableClient (ContainerBuilder $ container , $ name , $ factory, $ profiling )
303327 {
304328 $ serviceId = 'httplug.auto_discovery. ' .$ name ;
305- $ definition = $ container ->register ($ serviceId , DummyClient::class);
306- $ definition
329+
330+ $ pluginClientOptions = [];
331+
332+ if ($ profiling ) {
333+ $ debugPluginServiceId = $ this ->registerDebugPlugin ($ container , $ serviceId );
334+
335+ $ pluginClientOptions ['debug_plugins ' ] = [new Reference ($ debugPluginServiceId )];
336+ }
337+
338+ $ container
339+ ->register ($ serviceId , DummyClient::class)
307340 ->setFactory ([PluginClientFactory::class, 'createPluginClient ' ])
308- ->setArguments ([[], $ factory , [], []]);
341+ ->setArguments ([[], $ factory , [], $ pluginClientOptions ])
342+ ;
309343
310344 return $ serviceId ;
311345 }
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+ }
312376}
0 commit comments