14
14
use Http \Message \Authentication \BasicAuth ;
15
15
use Http \Message \Authentication \Bearer ;
16
16
use Http \Message \Authentication \Wsse ;
17
+ use Psr \Http \Message \UriInterface ;
17
18
use Symfony \Component \Config \FileLocator ;
18
19
use Symfony \Component \DependencyInjection \ContainerBuilder ;
20
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
19
21
use Symfony \Component \DependencyInjection \Definition ;
20
22
use Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
21
23
use Symfony \Component \DependencyInjection \Reference ;
@@ -53,7 +55,7 @@ public function load(array $configs, ContainerBuilder $container)
53
55
}
54
56
55
57
// Configure toolbar
56
- if ($ config ['profiling ' ][ ' enabled ' ] ) {
58
+ if ($ this -> isConfigEnabled ( $ container , $ config ['profiling ' ]) ) {
57
59
$ loader ->load ('data-collector.xml ' );
58
60
59
61
if (!empty ($ config ['profiling ' ]['formatter ' ])) {
@@ -70,8 +72,8 @@ public function load(array $configs, ContainerBuilder $container)
70
72
;
71
73
}
72
74
73
- $ this ->configurePlugins ($ container , $ config ['plugins ' ]);
74
75
$ 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
75
77
$ this ->configureAutoDiscoveryClients ($ container , $ config );
76
78
}
77
79
@@ -91,7 +93,7 @@ private function configureClients(ContainerBuilder $container, array $config)
91
93
$ first = $ name ;
92
94
}
93
95
94
- $ this ->configureClient ($ container , $ name , $ arguments , $ config ['profiling ' ][ ' enabled ' ] );
96
+ $ this ->configureClient ($ container , $ name , $ arguments , $ this -> isConfigEnabled ( $ container , $ config ['profiling ' ]) );
95
97
}
96
98
97
99
// If we have clients configured
@@ -108,7 +110,7 @@ private function configureClients(ContainerBuilder $container, array $config)
108
110
* @param ContainerBuilder $container
109
111
* @param array $config
110
112
*/
111
- private function configurePlugins (ContainerBuilder $ container , array $ config )
113
+ private function configureSharedPlugins (ContainerBuilder $ container , array $ config )
112
114
{
113
115
if (!empty ($ config ['authentication ' ])) {
114
116
$ this ->configureAuthentication ($ container , $ config ['authentication ' ]);
@@ -118,21 +120,23 @@ private function configurePlugins(ContainerBuilder $container, array $config)
118
120
foreach ($ config as $ name => $ pluginConfig ) {
119
121
$ pluginId = 'httplug.plugin. ' .$ name ;
120
122
121
- if ($ pluginConfig[ ' enabled ' ] ) {
123
+ if ($ this -> isConfigEnabled ( $ container , $ pluginConfig) ) {
122
124
$ def = $ container ->getDefinition ($ pluginId );
123
- $ this ->configurePluginByName ($ name , $ def , $ pluginConfig );
125
+ $ this ->configurePluginByName ($ name , $ def , $ pluginConfig, $ container , $ pluginId );
124
126
} else {
125
127
$ container ->removeDefinition ($ pluginId );
126
128
}
127
129
}
128
130
}
129
131
130
132
/**
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.
134
138
*/
135
- private function configurePluginByName ($ name , Definition $ definition , array $ config )
139
+ private function configurePluginByName ($ name , Definition $ definition , array $ config, ContainerInterface $ container , $ serviceId )
136
140
{
137
141
switch ($ name ) {
138
142
case 'cache ' :
@@ -173,6 +177,23 @@ private function configurePluginByName($name, Definition $definition, array $con
173
177
$ definition ->replaceArgument (0 , new Reference ($ config ['stopwatch ' ]));
174
178
break ;
175
179
180
+ /* client specific plugins */
181
+
182
+ case 'add_host ' :
183
+ $ uriService = $ serviceId .'.host_uri ' ;
184
+ $ this ->createUri ($ container , $ uriService , $ config ['host ' ]);
185
+ $ definition ->replaceArgument (0 , new Reference ($ uriService ));
186
+ $ definition ->replaceArgument (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
+ $ definition ->replaceArgument (0 , $ config ['headers ' ]);
195
+ break ;
196
+
176
197
default :
177
198
throw new \InvalidArgumentException (sprintf ('Internal exception: Plugin %s is not handled ' , $ name ));
178
199
}
@@ -181,11 +202,15 @@ private function configurePluginByName($name, Definition $definition, array $con
181
202
/**
182
203
* @param ContainerBuilder $container
183
204
* @param array $config
205
+ *
206
+ * @return array List of service ids for the authentication plugins.
184
207
*/
185
- private function configureAuthentication (ContainerBuilder $ container , array $ config )
208
+ private function configureAuthentication (ContainerBuilder $ container , array $ config, $ servicePrefix = ' httplug.plugin.authentication ' )
186
209
{
210
+ $ pluginServices = [];
211
+
187
212
foreach ($ config as $ name => $ values ) {
188
- $ authServiceKey = sprintf (' httplug.plugin.authentication .%s.auth ' , $ name );
213
+ $ authServiceKey = sprintf ($ servicePrefix . ' .%s.auth ' , $ name );
189
214
switch ($ values ['type ' ]) {
190
215
case 'bearer ' :
191
216
$ container ->register ($ authServiceKey , Bearer::class)
@@ -208,33 +233,54 @@ private function configureAuthentication(ContainerBuilder $container, array $con
208
233
throw new \LogicException (sprintf ('Unknown authentication type: "%s" ' , $ values ['type ' ]));
209
234
}
210
235
211
- $ container ->register ('httplug.plugin.authentication. ' .$ name , AuthenticationPlugin::class)
212
- ->addArgument (new Reference ($ authServiceKey ));
236
+ $ pluginServiceKey = $ servicePrefix .'. ' .$ name ;
237
+ $ container ->register ($ pluginServiceKey , AuthenticationPlugin::class)
238
+ ->addArgument (new Reference ($ authServiceKey ))
239
+ ;
240
+ $ pluginServices [] = $ pluginServiceKey ;
213
241
}
242
+
243
+ return $ pluginServices ;
214
244
}
215
245
216
246
/**
217
247
* @param ContainerBuilder $container
218
- * @param string $name
248
+ * @param string $clientName
219
249
* @param array $arguments
220
250
* @param bool $profiling
221
251
*/
222
- private function configureClient (ContainerBuilder $ container , $ name , array $ arguments , $ profiling )
252
+ private function configureClient (ContainerBuilder $ container , $ clientName , array $ arguments , $ profiling )
223
253
{
224
- $ serviceId = 'httplug.client. ' .$ name ;
254
+ $ serviceId = 'httplug.client. ' .$ clientName ;
255
+
256
+ $ plugins = [];
257
+ foreach ($ arguments ['plugins ' ] as $ plugin ) {
258
+ list ($ pluginName , $ pluginConfig ) = each ($ plugin );
259
+ if ('reference ' === $ pluginName ) {
260
+ $ plugins [] = $ pluginConfig ['id ' ];
261
+ } elseif ('authentication ' === $ pluginName ) {
262
+ $ plugins = array_merge ($ plugins , $ this ->configureAuthentication ($ container , $ pluginConfig , $ serviceId .'.authentication ' ));
263
+ } else {
264
+ $ pluginServiceId = $ serviceId .'.plugin. ' .$ pluginName ;
265
+ $ def = clone $ container ->getDefinition ('httplug.plugin ' .'. ' .$ pluginName );
266
+ $ def ->setAbstract (false );
267
+ $ this ->configurePluginByName ($ pluginName , $ def , $ pluginConfig , $ container , $ pluginServiceId );
268
+ $ container ->setDefinition ($ pluginServiceId , $ def );
269
+ $ plugins [] = $ pluginServiceId ;
270
+ }
271
+ }
225
272
226
273
$ pluginClientOptions = [];
227
-
228
274
if ($ profiling ) {
275
+ // Add the stopwatch plugin
229
276
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 ' );
232
278
}
233
279
234
280
// Tell the plugin journal what plugins we used
235
281
$ container
236
282
->getDefinition ('httplug.collector.plugin_journal ' )
237
- ->addMethodCall ('setPlugins ' , [$ name , $ arguments [ ' plugins ' ] ])
283
+ ->addMethodCall ('setPlugins ' , [$ clientName , $ plugins ])
238
284
;
239
285
240
286
$ debugPluginServiceId = $ this ->registerDebugPlugin ($ container , $ serviceId );
@@ -250,7 +296,7 @@ private function configureClient(ContainerBuilder $container, $name, array $argu
250
296
function ($ id ) {
251
297
return new Reference ($ id );
252
298
},
253
- $ arguments [ ' plugins ' ]
299
+ $ plugins
254
300
)
255
301
)
256
302
->addArgument (new Reference ($ arguments ['factory ' ]))
@@ -290,6 +336,23 @@ function ($id) {
290
336
}
291
337
}
292
338
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 , UriInterface::class)
350
+ ->setPublic (false )
351
+ ->setFactory ([new Reference ('httplug.uri_factory ' ), 'createUri ' ])
352
+ ->addArgument ($ uri )
353
+ ;
354
+ }
355
+
293
356
/**
294
357
* Make the user can select what client is used for auto discovery. If none is provided, a service will be created
295
358
* by finding a client using auto discovery.
@@ -307,7 +370,7 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra
307
370
$ container ,
308
371
'auto_discovered_client ' ,
309
372
[HttpClientDiscovery::class, 'find ' ],
310
- $ config ['profiling ' ][ ' enabled ' ]
373
+ $ this -> isConfigEnabled ( $ container , $ config ['profiling ' ])
311
374
);
312
375
}
313
376
@@ -322,7 +385,7 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra
322
385
$ container ,
323
386
'auto_discovered_async ' ,
324
387
[HttpAsyncClientDiscovery::class, 'find ' ],
325
- $ config ['profiling ' ][ ' enabled ' ]
388
+ $ this -> isConfigEnabled ( $ container , $ config ['profiling ' ])
326
389
);
327
390
}
328
391
0 commit comments