|
5 | 5 | use Http\HttplugBundle\ClientFactory\DummyClient; |
6 | 6 | use Symfony\Component\Config\FileLocator; |
7 | 7 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 8 | +use Symfony\Component\DependencyInjection\Definition; |
8 | 9 | use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
9 | 10 | use Symfony\Component\DependencyInjection\Reference; |
10 | 11 | use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
@@ -92,73 +93,66 @@ protected function configureClients(ContainerBuilder $container, array $config) |
92 | 93 | } |
93 | 94 | } |
94 | 95 |
|
| 96 | + /** |
| 97 | + * @param ContainerBuilder $container |
| 98 | + * @param array $config |
| 99 | + */ |
95 | 100 | protected function configurePlugins(ContainerBuilder $container, array $config) |
96 | 101 | { |
97 | | - if ($config['authentication']['enabled']) { |
98 | | - $container->getDefinition('httplug.plugin.authentication') |
99 | | - ->replaceArgument(0, new Reference($config['authentication']['authentication'])); |
100 | | - } else { |
101 | | - $container->removeDefinition('httplug.plugin.authentication'); |
102 | | - } |
103 | | - |
104 | | - if ($config['cache']['enabled']) { |
105 | | - $container->getDefinition('httplug.plugin.cache') |
106 | | - ->replaceArgument(0, new Reference($config['cache']['cache_pool'])) |
107 | | - ->replaceArgument(1, new Reference($config['cache']['stream_factory'])) |
108 | | - ->replaceArgument(2, $config['cache']['config']); |
109 | | - } else { |
110 | | - $container->removeDefinition('httplug.plugin.cache'); |
111 | | - } |
112 | | - |
113 | | - if ($config['cookie']['enabled']) { |
114 | | - $container->getDefinition('httplug.plugin.cookie') |
115 | | - ->replaceArgument(0, new Reference($config['cookie']['cookie_jar'])); |
116 | | - } else { |
117 | | - $container->removeDefinition('httplug.plugin.cookie'); |
118 | | - } |
119 | | - |
120 | | - if ($config['decoder']['enabled']) { |
121 | | - $container->getDefinition('httplug.plugin.decoder') |
122 | | - ->replaceArgument(0, $config['decoder']['use_content_encoding']); |
123 | | - } else { |
124 | | - $container->removeDefinition('httplug.plugin.decoder'); |
125 | | - } |
126 | | - |
127 | | - if ($config['history']['enabled']) { |
128 | | - $container->getDefinition('httplug.plugin.history') |
129 | | - ->replaceArgument(0, new Reference($config['history']['journal'])); |
130 | | - } else { |
131 | | - $container->removeDefinition('httplug.plugin.history'); |
132 | | - } |
133 | | - |
134 | | - if ($config['logger']['enabled']) { |
135 | | - $container->getDefinition('httplug.plugin.logger') |
136 | | - ->replaceArgument(0, new Reference($config['logger']['logger'])) |
137 | | - ->replaceArgument(1, new Reference($config['logger']['formatter'])); |
138 | | - } else { |
139 | | - $container->removeDefinition('httplug.plugin.logger'); |
140 | | - } |
141 | | - |
142 | | - if ($config['redirect']['enabled']) { |
143 | | - $container->getDefinition('httplug.plugin.redirect') |
144 | | - ->replaceArgument(0, $config['redirect']['preserve_header']) |
145 | | - ->replaceArgument(1, $config['redirect']['use_default_for_multiple']); |
146 | | - } else { |
147 | | - $container->removeDefinition('httplug.plugin.redirect'); |
| 102 | + foreach ($config as $name => $pluginConfig) { |
| 103 | + $pluginId = 'httplug.plugin.'.$name; |
| 104 | + if ($config[$name]['enabled']) { |
| 105 | + $def = $container->getDefinition($pluginId); |
| 106 | + $this->configurePluginByName($name, $def, $pluginConfig); |
| 107 | + } else { |
| 108 | + $container->removeDefinition($pluginId); |
| 109 | + } |
148 | 110 | } |
149 | 111 |
|
150 | | - if ($config['retry']['enabled']) { |
151 | | - $container->getDefinition('httplug.plugin.retry') |
152 | | - ->replaceArgument(0, $config['retry']['retry']); |
153 | | - } else { |
154 | | - $container->removeDefinition('httplug.plugin.retry'); |
155 | | - } |
| 112 | + } |
156 | 113 |
|
157 | | - if ($config['stopwatch']['enabled']) { |
158 | | - $container->getDefinition('httplug.plugin.stopwatch') |
159 | | - ->replaceArgument(0, new Reference($config['stopwatch']['stopwatch'])); |
160 | | - } else { |
161 | | - $container->removeDefinition('httplug.plugin.stopwatch'); |
| 114 | + /** |
| 115 | + * @param string $name |
| 116 | + * @param Definition $definition |
| 117 | + * @param array $config |
| 118 | + */ |
| 119 | + private function configurePluginByName($name, Definition $definition, array $config) |
| 120 | + { |
| 121 | + switch ($name) { |
| 122 | + case 'authentication': |
| 123 | + $definition->replaceArgument(0, new Reference($config['authentication'])); |
| 124 | + break; |
| 125 | + case 'cache': |
| 126 | + $definition |
| 127 | + ->replaceArgument(0, new Reference($config['cache_pool'])) |
| 128 | + ->replaceArgument(1, new Reference($config['stream_factory'])) |
| 129 | + ->replaceArgument(2, $config['config']); |
| 130 | + break; |
| 131 | + case 'cookie': |
| 132 | + $definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
| 133 | + break; |
| 134 | + case 'decoder': |
| 135 | + $definition->addArgument($config['use_content_encoding']); |
| 136 | + break; |
| 137 | + case 'history': |
| 138 | + $definition->replaceArgument(0, new Reference($config['journal'])); |
| 139 | + break; |
| 140 | + case 'logger': |
| 141 | + $definition |
| 142 | + ->replaceArgument(0, new Reference($config['logger'])) |
| 143 | + ->replaceArgument(1, new Reference($config['formatter'])); |
| 144 | + break; |
| 145 | + case 'redirect': |
| 146 | + $definition |
| 147 | + ->addArgument($config['preserve_header']) |
| 148 | + ->addArgument($config['use_default_for_multiple']); |
| 149 | + break; |
| 150 | + case 'retry': |
| 151 | + $definition->addArgument($config['retry']); |
| 152 | + break; |
| 153 | + case 'stopwatch': |
| 154 | + $definition->replaceArgument(0, new Reference($config['stopwatch'])); |
| 155 | + break; |
162 | 156 | } |
163 | 157 | } |
164 | 158 | } |
0 commit comments