1414use  Http \Message \Authentication \BasicAuth ;
1515use  Http \Message \Authentication \Bearer ;
1616use  Http \Message \Authentication \Wsse ;
17+ use  Psr \Http \Message \UriInterface ;
1718use  Symfony \Component \Config \FileLocator ;
1819use  Symfony \Component \DependencyInjection \ContainerBuilder ;
20+ use  Symfony \Component \DependencyInjection \ContainerInterface ;
1921use  Symfony \Component \DependencyInjection \Definition ;
2022use  Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
2123use  Symfony \Component \DependencyInjection \Reference ;
@@ -53,7 +55,7 @@ public function load(array $configs, ContainerBuilder $container)
5355        }
5456
5557        // Configure toolbar 
56-         if  ($ config'profiling ' ][ ' enabled ' ] ) {
58+         if  ($ this -> isConfigEnabled ( $ container ,  $ config'profiling ' ]) ) {
5759            $ loaderload ('data-collector.xml ' );
5860
5961            if  (!empty ($ config'profiling ' ]['formatter ' ])) {
@@ -70,8 +72,8 @@ public function load(array $configs, ContainerBuilder $container)
7072            ;
7173        }
7274
73-         $ this configurePlugins ($ container$ config'plugins ' ]);
7475        $ this configureClients ($ container$ config
76+         $ this configureSharedPlugins ($ container$ config'plugins ' ]); // must be after clients, as clients.X.plugins might use plugins as templates that will be removed 
7577        $ this configureAutoDiscoveryClients ($ container$ config
7678    }
7779
@@ -91,7 +93,7 @@ private function configureClients(ContainerBuilder $container, array $config)
9193                $ first$ name
9294            }
9395
94-             $ this configureClient ($ container$ name$ arguments$ config'profiling ' ][ ' enabled ' ] );
96+             $ this configureClient ($ container$ name$ arguments$ this -> isConfigEnabled ( $ container ,  $ config'profiling ' ]) );
9597        }
9698
9799        // If we have clients configured 
@@ -108,7 +110,7 @@ private function configureClients(ContainerBuilder $container, array $config)
108110     * @param ContainerBuilder $container 
109111     * @param array            $config 
110112     */ 
111-     private  function  configurePlugins (ContainerBuilder $ containerarray  $ config
113+     private  function  configureSharedPlugins (ContainerBuilder $ containerarray  $ config
112114    {
113115        if  (!empty ($ config'authentication ' ])) {
114116            $ this configureAuthentication ($ container$ config'authentication ' ]);
@@ -118,21 +120,23 @@ private function configurePlugins(ContainerBuilder $container, array $config)
118120        foreach  ($ configas  $ name$ pluginConfig
119121            $ pluginId'httplug.plugin. ' .$ name
120122
121-             if  ($ pluginConfig[ ' enabled ' ] ) {
123+             if  ($ this -> isConfigEnabled ( $ container ,  $ pluginConfig) ) {
122124                $ def$ containergetDefinition ($ pluginId
123-                 $ this configurePluginByName ($ name$ def$ pluginConfig
125+                 $ this configurePluginByName ($ name$ def$ pluginConfig,  $ container ,  $ pluginId 
124126            } else  {
125127                $ containerremoveDefinition ($ pluginId
126128            }
127129        }
128130    }
129131
130132    /** 
131-      * @param string     $name 
132-      * @param Definition $definition 
133-      * @param array      $config 
133+      * @param string           $name 
134+      * @param Definition       $definition 
135+      * @param array            $config 
136+      * @param ContainerBuilder $container  In case we need to add additional services for this plugin 
137+      * @param string           $serviceId  Service id of the plugin, in case we need to add additional services for this plugin. 
134138     */ 
135-     private  function  configurePluginByName ($ nameDefinition $ definitionarray  $ config
139+     private  function  configurePluginByName ($ nameDefinition $ definitionarray  $ config,  ContainerInterface   $ container ,  $ serviceId 
136140    {
137141        switch  ($ name
138142            case  'cache ' :
@@ -173,6 +177,23 @@ private function configurePluginByName($name, Definition $definition, array $con
173177                $ definitionreplaceArgument (0 , new  Reference ($ config'stopwatch ' ]));
174178                break ;
175179
180+             /* client specific plugins */ 
181+ 
182+             case  'add_host ' :
183+                 $ uriService$ serviceId'.host_uri ' ;
184+                 $ this createUri ($ container$ uriService$ config'host ' ]);
185+                 $ definitionreplaceArgument (0 , new  Reference ($ uriService
186+                 $ definitionreplaceArgument (1 , [
187+                     'replace '  => $ config'replace ' ],
188+                 ]);
189+                 break ;
190+             case  'header_append ' :
191+             case  'header_defaults ' :
192+             case  'header_set ' :
193+             case  'header_remove ' :
194+                 $ definitionreplaceArgument (0 , $ config'headers ' ]);
195+                 break ;
196+ 
176197            default :
177198                throw  new  \InvalidArgumentException (sprintf ('Internal exception: Plugin %s is not handled ' , $ name
178199        }
@@ -181,11 +202,15 @@ private function configurePluginByName($name, Definition $definition, array $con
181202    /** 
182203     * @param ContainerBuilder $container 
183204     * @param array            $config 
205+      * 
206+      * @return array List of service ids for the authentication plugins. 
184207     */ 
185-     private  function  configureAuthentication (ContainerBuilder $ containerarray  $ config
208+     private  function  configureAuthentication (ContainerBuilder $ containerarray  $ config,  $ servicePrefix  =  ' httplug.plugin.authentication ' 
186209    {
210+         $ pluginServices
211+ 
187212        foreach  ($ configas  $ name$ values
188-             $ authServiceKeysprintf (' httplug.plugin.authentication .%s.auth$ name
213+             $ authServiceKeysprintf ($ servicePrefix . ' .%s.auth '$ name
189214            switch  ($ values'type ' ]) {
190215                case  'bearer ' :
191216                    $ containerregister ($ authServiceKey
@@ -208,33 +233,54 @@ private function configureAuthentication(ContainerBuilder $container, array $con
208233                    throw  new  \LogicException (sprintf ('Unknown authentication type: "%s" ' , $ values'type ' ]));
209234            }
210235
211-             $ containerregister ('httplug.plugin.authentication. ' .$ name
212-                 ->addArgument (new  Reference ($ authServiceKey
236+             $ pluginServiceKey$ servicePrefix'. ' .$ name
237+             $ containerregister ($ pluginServiceKey
238+                 ->addArgument (new  Reference ($ authServiceKey
239+             ;
240+             $ pluginServices$ pluginServiceKey
213241        }
242+ 
243+         return  $ pluginServices
214244    }
215245
216246    /** 
217247     * @param ContainerBuilder $container 
218-      * @param string           $name  
248+      * @param string           $clientName  
219249     * @param array            $arguments 
220250     * @param bool             $profiling 
221251     */ 
222-     private  function  configureClient (ContainerBuilder $ container$ name array  $ arguments$ profiling
252+     private  function  configureClient (ContainerBuilder $ container$ clientName array  $ arguments$ profiling
223253    {
224-         $ serviceId'httplug.client. ' .$ name
254+         $ serviceId'httplug.client. ' .$ clientName
255+ 
256+         $ plugins
257+         foreach  ($ arguments'plugins ' ] as  $ plugin
258+             list ($ pluginName$ pluginConfigeach ($ plugin
259+             if  ('reference '  === $ pluginName
260+                 $ plugins$ pluginConfig'id ' ];
261+             } elseif  ('authentication '  === $ pluginName
262+                 $ pluginsarray_merge ($ plugins$ this configureAuthentication ($ container$ pluginConfig$ serviceId'.authentication ' ));
263+             } else  {
264+                 $ pluginServiceId$ serviceId'.plugin. ' .$ pluginName
265+                 $ defclone  $ containergetDefinition ('httplug.plugin ' .'. ' .$ pluginName
266+                 $ defsetAbstract (false );
267+                 $ this configurePluginByName ($ pluginName$ def$ pluginConfig$ container$ pluginServiceId
268+                 $ containersetDefinition ($ pluginServiceId$ def
269+                 $ plugins$ pluginServiceId
270+             }
271+         }
225272
226273        $ pluginClientOptions
227- 
228274        if  ($ profiling
275+             // Add the stopwatch plugin 
229276            if  (!in_array ('httplug.plugin.stopwatch ' , $ arguments'plugins ' ])) {
230-                 // Add the stopwatch plugin 
231-                 array_unshift ($ arguments'plugins ' ], 'httplug.plugin.stopwatch ' );
277+                 array_unshift ($ plugins'httplug.plugin.stopwatch ' );
232278            }
233279
234280            // Tell the plugin journal what plugins we used 
235281            $ container
236282                ->getDefinition ('httplug.collector.plugin_journal ' )
237-                 ->addMethodCall ('setPlugins ' , [$ name $ arguments [ ' plugins ' ] ])
283+                 ->addMethodCall ('setPlugins ' , [$ clientName $ plugins
238284            ;
239285
240286            $ debugPluginServiceId$ this registerDebugPlugin ($ container$ serviceId
@@ -250,7 +296,7 @@ private function configureClient(ContainerBuilder $container, $name, array $argu
250296                    function  ($ id
251297                        return  new  Reference ($ id
252298                    },
253-                     $ arguments [ ' plugins ' ] 
299+                     $ plugins
254300                )
255301            )
256302            ->addArgument (new  Reference ($ arguments'factory ' ]))
@@ -290,6 +336,23 @@ function ($id) {
290336        }
291337    }
292338
339+     /** 
340+      * Create a URI object with the default URI factory. 
341+      * 
342+      * @param ContainerBuilder $container 
343+      * @param string           $serviceId Name of the private service to create 
344+      * @param string           $uri       String representation of the URI 
345+      */ 
346+     private  function  createUri (ContainerBuilder $ container$ serviceId$ uri
347+     {
348+         $ container
349+             ->register ($ serviceId
350+             ->setPublic (false )
351+             ->setFactory ([new  Reference ('httplug.uri_factory ' ), 'createUri ' ])
352+             ->addArgument ($ uri
353+         ;
354+     }
355+ 
293356    /** 
294357     * Make the user can select what client is used for auto discovery. If none is provided, a service will be created 
295358     * by finding a client using auto discovery. 
@@ -307,7 +370,7 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra
307370                    $ container
308371                    'auto_discovered_client ' ,
309372                    [HttpClientDiscovery::class, 'find ' ],
310-                     $ config'profiling ' ][ ' enabled ' ] 
373+                     $ this -> isConfigEnabled ( $ container ,  $ config'profiling ' ]) 
311374                );
312375            }
313376
@@ -322,7 +385,7 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra
322385                    $ container
323386                    'auto_discovered_async ' ,
324387                    [HttpAsyncClientDiscovery::class, 'find ' ],
325-                     $ config'profiling ' ][ ' enabled ' ] 
388+                     $ this -> isConfigEnabled ( $ container ,  $ config'profiling ' ]) 
326389                );
327390            }
328391
0 commit comments