55use Http \Client \Common \FlexibleHttpClient ;
66use Http \Client \Common \HttpMethodsClient ;
77use Http \Client \Common \Plugin \AuthenticationPlugin ;
8- use Http \Client \Common \PluginClient ;
98use Http \Discovery \HttpAsyncClientDiscovery ;
109use Http \Discovery \HttpClientDiscovery ;
1110use Http \HttplugBundle \ClientFactory \DummyClient ;
12- use Http \HttplugBundle \Collector \ DebugPlugin ;
11+ use Http \HttplugBundle \ClientFactory \ PluginClientFactory ;
1312use Http \Message \Authentication \BasicAuth ;
1413use Http \Message \Authentication \Bearer ;
1514use Http \Message \Authentication \Wsse ;
@@ -39,21 +38,6 @@ public function load(array $configs, ContainerBuilder $container)
3938 $ loader ->load ('services.xml ' );
4039 $ loader ->load ('plugins.xml ' );
4140
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-
5741 foreach ($ config ['classes ' ] as $ service => $ class ) {
5842 if (!empty ($ class )) {
5943 $ container ->register (sprintf ('httplug.%s.default ' , $ service ), $ class );
@@ -66,45 +50,47 @@ public function load(array $configs, ContainerBuilder $container)
6650 }
6751
6852 $ 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+ }
7160 }
7261
7362 /**
7463 * Configure client services.
7564 *
7665 * @param ContainerBuilder $container
7766 * @param array $config
78- * @param bool $enableCollector
67+ *
68+ * @raturn array with client service names
7969 */
80- private function configureClients (ContainerBuilder $ container , array $ config, $ enableCollector )
70+ private function configureClients (ContainerBuilder $ container , array $ config )
8171 {
82- // If we have a client named 'default'
83- $ first = isset ( $ config [ ' clients ' ][ ' default ' ]) ? ' default ' : null ;
72+ $ serviceIds = [];
73+ $ first = null ;
8474
8575 foreach ($ config ['clients ' ] as $ name => $ arguments ) {
8676 if ($ first === null ) {
8777 // Save the name of the first configurated client.
8878 $ first = $ name ;
8979 }
9080
91- $ this ->configureClient ($ container , $ name , $ arguments, $ enableCollector );
81+ $ serviceIds [] = $ this ->configureClient ($ container , $ name , $ arguments );
9282 }
9383
9484 // If we have clients configured
9585 if ($ first !== null ) {
96- if ($ first !== 'default ' ) {
86+ // If we do not have a client named 'default'
87+ if (!isset ($ config ['clients ' ]['default ' ])) {
9788 // Alias the first client to httplug.client.default
9889 $ container ->setAlias ('httplug.client.default ' , 'httplug.client. ' .$ first );
9990 }
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 )]]);
10791 }
92+
93+ return $ serviceIds ;
10894 }
10995
11096 /**
@@ -212,46 +198,34 @@ private function configureAuthentication(ContainerBuilder $container, array $con
212198 * @param ContainerBuilder $container
213199 * @param string $name
214200 * @param array $arguments
215- * @param bool $enableCollector
201+ *
202+ * @return string The service id of the client.
216203 */
217- private function configureClient (ContainerBuilder $ container , $ name , array $ arguments, $ enableCollector )
204+ private function configureClient (ContainerBuilder $ container , $ name , array $ arguments )
218205 {
219206 $ 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 ' ]
236215 )
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+ ;
244221
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 ' ]]);
250225
251226 /*
252227 * Decorate the client with clients from client-common
253228 */
254-
255229 if ($ arguments ['flexible_client ' ]) {
256230 $ container ->register ($ serviceId .'.flexible ' , FlexibleHttpClient::class)
257231 ->addArgument (new Reference ($ serviceId .'.flexible.inner ' ))
@@ -265,91 +239,74 @@ function ($id) {
265239 ->setPublic (false )
266240 ->setDecoratedService ($ serviceId );
267241 }
268- }
269242
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 ;
287244 }
288245
289246 /**
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.
291249 *
292250 * @param ContainerBuilder $container
293251 * @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 )
297256 {
257+ $ serviceIds = [];
258+
298259 $ 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 ;
307270 $ httpClient = new Reference ($ httpClient );
308271 }
309272
310273 $ 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 ;
319283 $ asyncHttpClient = new Reference ($ httpClient );
320284 }
321285
322286 $ container ->getDefinition ('httplug.strategy ' )
323287 ->addArgument ($ httpClient )
324288 ->addArgument ($ asyncHttpClient );
289+
290+ return $ serviceIds ;
325291 }
326292
327293 /**
294+ * Find a client with auto discovery and return a service Reference to it.
295+ *
328296 * @param ContainerBuilder $container
329297 * @param string $name
330298 * @param callable $factory
331- * @param bool $enableCollector
332299 *
333- * @return Reference
300+ * @return string service id
334301 */
335- private function registerAutoDiscoverableClientWithDebugPlugin (ContainerBuilder $ container , $ name , $ factory, $ enableCollector )
302+ private function registerAutoDiscoverableClient (ContainerBuilder $ container , $ name , $ factory )
336303 {
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 , [], []]);
352309
353- return new Reference ( ' httplug.auto_discovery_ ' . $ name . ' .plugin ' ) ;
310+ return $ serviceId ;
354311 }
355312}
0 commit comments