2
2
3
3
namespace Http \HttplugBundle \DependencyInjection ;
4
4
5
- use Symfony \Component \Config \Definition \ArrayNode ;
6
5
use Symfony \Component \Config \Definition \Builder \ArrayNodeDefinition ;
6
+ use Symfony \Component \Config \Definition \Builder \NodeBuilder ;
7
7
use Symfony \Component \Config \Definition \Builder \NodeDefinition ;
8
8
use Symfony \Component \Config \Definition \Builder \TreeBuilder ;
9
9
use Symfony \Component \Config \Definition \ConfigurationInterface ;
@@ -46,7 +46,7 @@ public function getConfigTreeBuilder()
46
46
$ rootNode = $ treeBuilder ->root ('httplug ' );
47
47
48
48
$ this ->configureClients ($ rootNode );
49
- $ this ->configurePlugins ($ rootNode );
49
+ $ this ->configureSharedPlugins ($ rootNode );
50
50
51
51
$ rootNode
52
52
->validate ()
@@ -154,8 +154,9 @@ public function getConfigTreeBuilder()
154
154
155
155
private function configureClients (ArrayNodeDefinition $ root )
156
156
{
157
- $ root ->children ()
157
+ $ pluginNode = $ root ->children ()
158
158
->arrayNode ('clients ' )
159
+ ->fixXmlConfig ('plugin ' )
159
160
->validate ()
160
161
->ifTrue (function ($ clients ) {
161
162
foreach ($ clients as $ name => $ config ) {
@@ -186,55 +187,82 @@ private function configureClients(ArrayNodeDefinition $root)
186
187
->defaultFalse ()
187
188
->info ('Set to true to get the client wrapped in a BatchClient which allows you to send multiple request at the same time. ' )
188
189
->end ()
189
- ->arrayNode ('plugins ' )
190
- ->info ('A list of service ids of plugins. The order is important. ' )
191
- ->prototype ('scalar ' )->end ()
192
- ->end ()
193
190
->variableNode ('config ' )->defaultValue ([])->end ()
194
- ->append ($ this ->createExtraPluginsNode ())
195
- ->end ()
196
- ->end ();
191
+ ->arrayNode ('plugins ' )
192
+ ->info ('A list of plugin service ids and client specific plugin definitions. The order is important. ' )
193
+ ->prototype ('array ' )
194
+ ;
195
+
196
+ $ this ->configureClientPlugins ($ pluginNode );
197
197
}
198
198
199
199
/**
200
200
* @param ArrayNodeDefinition $root
201
201
*/
202
- private function configurePlugins (ArrayNodeDefinition $ root )
202
+ private function configureSharedPlugins (ArrayNodeDefinition $ root )
203
203
{
204
204
$ pluginsNode = $ root
205
205
->children ()
206
206
->arrayNode ('plugins ' )
207
207
->addDefaultsIfNotSet ()
208
208
;
209
- $ this ->configureSharedPluginNodes ($ pluginsNode );
209
+ $ this ->addSharedPluginNodes ($ pluginsNode );
210
210
}
211
211
212
212
/**
213
- * Create configuration for the extra_plugins node inside the client.
213
+ * Configure plugins node of a client.
214
214
*
215
- * @return NodeDefinition Definition of the extra_plugins node in the client .
215
+ * @param ArrayNodeDefinition $pluginNode The node to add plugin definitions to .
216
216
*/
217
- private function createExtraPluginsNode ( )
217
+ private function configureClientPlugins ( ArrayNodeDefinition $ pluginNode )
218
218
{
219
- $ builder = new TreeBuilder ();
220
- $ node = $ builder ->root ('extra_plugins ' );
221
- $ node ->validate ()
222
- ->always (function ($ plugins ) {
223
- if (!count ($ plugins ['authentication ' ])) {
224
- unset($ plugins ['authentication ' ]);
225
- }
226
- foreach ($ plugins as $ name => $ definition ) {
227
- if (!$ definition ['enabled ' ]) {
228
- unset($ plugins [$ name ]);
219
+ $ pluginNode
220
+ // support having just a service id in the list
221
+ ->beforeNormalization ()
222
+ ->always (function ($ plugin ) {
223
+ if (is_string ($ plugin )) {
224
+ return [
225
+ 'reference ' => [
226
+ 'enabled ' => true ,
227
+ 'id ' => $ plugin ,
228
+ ],
229
+ ];
229
230
}
230
- }
231
231
232
- return $ plugins ;
233
- })
232
+ return $ plugin ;
233
+ })
234
+ ->end ()
235
+
236
+ ->validate ()
237
+ ->always (function ($ plugins ) {
238
+ if (isset ($ plugins ['authentication ' ]) && !count ($ plugins ['authentication ' ])) {
239
+ unset($ plugins ['authentication ' ]);
240
+ }
241
+ foreach ($ plugins as $ name => $ definition ) {
242
+ if (!$ definition ['enabled ' ]) {
243
+ unset($ plugins [$ name ]);
244
+ }
245
+ }
246
+
247
+ return $ plugins ;
248
+ })
249
+ ->end ()
234
250
;
235
- $ this ->configureSharedPluginNodes ($ node , true );
236
- $ node
251
+ $ this ->addSharedPluginNodes ($ pluginNode , true );
252
+
253
+ $ pluginNode
237
254
->children ()
255
+ ->arrayNode ('reference ' )
256
+ ->canBeEnabled ()
257
+ ->info ('Reference to a plugin service ' )
258
+ ->children ()
259
+ ->scalarNode ('id ' )
260
+ ->info ('Service id of a plugin ' )
261
+ ->isRequired ()
262
+ ->cannotBeEmpty ()
263
+ ->end ()
264
+ ->end ()
265
+ ->end ()
238
266
->arrayNode ('add_host ' )
239
267
->canBeEnabled ()
240
268
->addDefaultsIfNotSet ()
@@ -251,17 +279,19 @@ private function createExtraPluginsNode()
251
279
->end ()
252
280
->end ()
253
281
->end ()
282
+
283
+ // TODO add aditional plugins that are only usable on a specific client
254
284
->end ()
255
285
->end ();
256
-
257
- return $ node ;
258
286
}
259
287
260
288
/**
261
- * @param ArrayNodeDefinition $pluginNode
289
+ * Add the definitions for shared plugin configurations.
290
+ *
291
+ * @param ArrayNodeDefinition $pluginNode The node to add to.
262
292
* @param bool $disableAll Some shared plugins are enabled by default. On the client, all are disabled by default.
263
293
*/
264
- private function configureSharedPluginNodes (ArrayNodeDefinition $ pluginNode , $ disableAll = false )
294
+ private function addSharedPluginNodes (ArrayNodeDefinition $ pluginNode , $ disableAll = false )
265
295
{
266
296
$ children = $ pluginNode ->children ();
267
297
@@ -305,11 +335,7 @@ private function configureSharedPluginNodes(ArrayNodeDefinition $pluginNode, $di
305
335
// End cookie plugin
306
336
307
337
$ decoder = $ children ->arrayNode ('decoder ' );
308
- if ($ disableAll ) {
309
- $ decoder ->canBeEnabled ();
310
- } else {
311
- $ decoder ->canBeDisabled ();
312
- }
338
+ $ disableAll ? $ decoder ->canBeEnabled () : $ decoder ->canBeDisabled ();
313
339
$ decoder ->addDefaultsIfNotSet ()
314
340
->children ()
315
341
->scalarNode ('use_content_encoding ' )->defaultTrue ()->end ()
@@ -330,11 +356,7 @@ private function configureSharedPluginNodes(ArrayNodeDefinition $pluginNode, $di
330
356
// End history plugin
331
357
332
358
$ logger = $ children ->arrayNode ('logger ' );
333
- if ($ disableAll ) {
334
- $ logger ->canBeEnabled ();
335
- } else {
336
- $ logger ->canBeDisabled ();
337
- }
359
+ $ disableAll ? $ logger ->canBeEnabled () : $ logger ->canBeDisabled ();
338
360
$ logger ->addDefaultsIfNotSet ()
339
361
->children ()
340
362
->scalarNode ('logger ' )
@@ -351,11 +373,7 @@ private function configureSharedPluginNodes(ArrayNodeDefinition $pluginNode, $di
351
373
// End logger plugin
352
374
353
375
$ redirect = $ children ->arrayNode ('redirect ' );
354
- if ($ disableAll ) {
355
- $ redirect ->canBeEnabled ();
356
- } else {
357
- $ redirect ->canBeDisabled ();
358
- }
376
+ $ disableAll ? $ redirect ->canBeEnabled () : $ redirect ->canBeDisabled ();
359
377
$ redirect ->addDefaultsIfNotSet ()
360
378
->children ()
361
379
->scalarNode ('preserve_header ' )->defaultTrue ()->end ()
@@ -365,11 +383,7 @@ private function configureSharedPluginNodes(ArrayNodeDefinition $pluginNode, $di
365
383
// End redirect plugin
366
384
367
385
$ retry = $ children ->arrayNode ('retry ' );
368
- if ($ disableAll ) {
369
- $ retry ->canBeEnabled ();
370
- } else {
371
- $ retry ->canBeDisabled ();
372
- }
386
+ $ disableAll ? $ retry ->canBeEnabled () : $ retry ->canBeDisabled ();
373
387
$ retry ->addDefaultsIfNotSet ()
374
388
->children ()
375
389
->scalarNode ('retry ' )->defaultValue (1 )->end () // TODO: should be called retries for consistency with the class
@@ -378,11 +392,7 @@ private function configureSharedPluginNodes(ArrayNodeDefinition $pluginNode, $di
378
392
// End retry plugin
379
393
380
394
$ stopwatch = $ children ->arrayNode ('stopwatch ' );
381
- if ($ disableAll ) {
382
- $ stopwatch ->canBeEnabled ();
383
- } else {
384
- $ stopwatch ->canBeDisabled ();
385
- }
395
+ $ disableAll ? $ stopwatch ->canBeEnabled () : $ stopwatch ->canBeDisabled ();
386
396
$ stopwatch ->addDefaultsIfNotSet ()
387
397
->children ()
388
398
->scalarNode ('stopwatch ' )
0 commit comments