1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Http\Client\Plugin\AuthenticationPlugin; |
6
|
|
|
use Http\Client\Plugin\PluginClient; |
7
|
|
|
use Http\HttplugBundle\ClientFactory\DummyClient; |
8
|
|
|
use Http\Message\Authentication\BasicAuth; |
9
|
|
|
use Http\Message\Authentication\Bearer; |
10
|
|
|
use Http\Message\Authentication\Wsse; |
11
|
|
|
use Symfony\Component\Config\FileLocator; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
13
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
14
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
15
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
16
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author David Buchmann <[email protected]> |
20
|
|
|
* @author Tobias Nyholm <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class HttplugExtension extends Extension |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function load(array $configs, ContainerBuilder $container) |
28
|
|
|
{ |
29
|
|
|
$configuration = $this->getConfiguration($configs, $container); |
30
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
|
|
|
31
|
|
|
|
32
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
33
|
|
|
|
34
|
|
|
$loader->load('services.xml'); |
35
|
|
|
$loader->load('plugins.xml'); |
36
|
|
|
$loader->load('discovery.xml'); |
37
|
|
|
|
38
|
|
|
$enabled = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug'); |
39
|
|
|
if ($enabled) { |
40
|
|
|
$loader->load('data-collector.xml'); |
41
|
|
|
$config['_inject_collector_plugin'] = true; |
42
|
|
|
|
43
|
|
|
if (!empty($config['toolbar']['formatter'])) { |
44
|
|
|
$container->getDefinition('httplug.collector.message_journal') |
45
|
|
|
->replaceArgument(0, new Reference($config['toolbar']['formatter'])); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
foreach ($config['classes'] as $service => $class) { |
50
|
|
|
if (!empty($class)) { |
51
|
|
|
$container->removeDefinition(sprintf('httplug.%s.default', $service)); |
52
|
|
|
$container->register(sprintf('httplug.%s.default', $service), $class); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($config['main_alias'] as $type => $id) { |
57
|
|
|
$container->setAlias(sprintf('httplug.%s', $type), $id); |
58
|
|
|
} |
59
|
|
|
$this->configurePlugins($container, $config['plugins']); |
60
|
|
|
$this->configureClients($container, $config); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Configure client services. |
65
|
|
|
* |
66
|
|
|
* @param ContainerBuilder $container |
67
|
|
|
* @param array $config |
68
|
|
|
*/ |
69
|
|
|
private function configureClients(ContainerBuilder $container, array $config) |
70
|
|
|
{ |
71
|
|
|
$first = isset($config['clients']['default']) ? 'default' : null; |
72
|
|
|
foreach ($config['clients'] as $name => $arguments) { |
73
|
|
|
if ($first === null) { |
74
|
|
|
$first = $name; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (isset($config['_inject_collector_plugin'])) { |
78
|
|
|
array_unshift($arguments['plugins'], 'httplug.collector.history_plugin'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$def = $container->register('httplug.client.'.$name, DummyClient::class); |
82
|
|
|
|
83
|
|
|
if (empty($arguments['plugins'])) { |
84
|
|
|
$def->setFactory([new Reference($arguments['factory']), 'createClient']) |
85
|
|
|
->addArgument($arguments['config']); |
86
|
|
|
} else { |
87
|
|
|
$def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient') |
88
|
|
|
->addArgument(array_map(function ($id) { |
89
|
|
|
return new Reference($id); |
90
|
|
|
}, $arguments['plugins'])) |
91
|
|
|
->addArgument(new Reference($arguments['factory'])) |
92
|
|
|
->addArgument($arguments['config']); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// If we have clients configured |
97
|
|
|
if ($first !== null) { |
98
|
|
|
if ($first !== 'default') { |
99
|
|
|
// Alias the first client to httplug.client.default |
100
|
|
|
$container->setAlias('httplug.client.default', 'httplug.client.'.$first); |
101
|
|
|
} |
102
|
|
|
} elseif (isset($config['_inject_collector_plugin'])) { |
103
|
|
|
// No client was configured. Make sure to inject history plugin to the auto discovery client. |
104
|
|
|
$container->register('httplug.client', PluginClient::class) |
105
|
|
|
->addArgument(new Reference('httplug.client.default')) |
106
|
|
|
->addArgument([new Reference('httplug.collector.history_plugin')]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param ContainerBuilder $container |
112
|
|
|
* @param array $config |
113
|
|
|
*/ |
114
|
|
|
private function configurePlugins(ContainerBuilder $container, array $config) |
115
|
|
|
{ |
116
|
|
|
if (!empty($config['authentication'])) { |
117
|
|
|
$this->configureAuthentication($container, $config['authentication']); |
118
|
|
|
} |
119
|
|
|
unset($config['authentication']); |
120
|
|
|
|
121
|
|
|
foreach ($config as $name => $pluginConfig) { |
122
|
|
|
$pluginId = 'httplug.plugin.'.$name; |
123
|
|
|
if ($pluginConfig['enabled']) { |
124
|
|
|
$def = $container->getDefinition($pluginId); |
125
|
|
|
$this->configurePluginByName($name, $def, $pluginConfig); |
126
|
|
|
} else { |
127
|
|
|
$container->removeDefinition($pluginId); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $name |
134
|
|
|
* @param Definition $definition |
135
|
|
|
* @param array $config |
136
|
|
|
*/ |
137
|
|
|
private function configurePluginByName($name, Definition $definition, array $config) |
138
|
|
|
{ |
139
|
|
|
switch ($name) { |
140
|
|
|
case 'cache': |
141
|
|
|
$definition |
142
|
|
|
->replaceArgument(0, new Reference($config['cache_pool'])) |
143
|
|
|
->replaceArgument(1, new Reference($config['stream_factory'])) |
144
|
|
|
->replaceArgument(2, $config['config']); |
145
|
|
|
break; |
146
|
|
|
case 'cookie': |
147
|
|
|
$definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
148
|
|
|
break; |
149
|
|
|
case 'decoder': |
150
|
|
|
$definition->addArgument($config['use_content_encoding']); |
151
|
|
|
break; |
152
|
|
|
case 'history': |
153
|
|
|
$definition->replaceArgument(0, new Reference($config['journal'])); |
154
|
|
|
break; |
155
|
|
|
case 'logger': |
156
|
|
|
$definition->replaceArgument(0, new Reference($config['logger'])); |
157
|
|
|
if (!empty($config['formatter'])) { |
158
|
|
|
$definition->replaceArgument(1, new Reference($config['formatter'])); |
159
|
|
|
} |
160
|
|
|
break; |
161
|
|
|
case 'redirect': |
162
|
|
|
$definition |
163
|
|
|
->addArgument($config['preserve_header']) |
164
|
|
|
->addArgument($config['use_default_for_multiple']); |
165
|
|
|
break; |
166
|
|
|
case 'retry': |
167
|
|
|
$definition->addArgument($config['retry']); |
168
|
|
|
break; |
169
|
|
|
case 'stopwatch': |
170
|
|
|
$definition->replaceArgument(0, new Reference($config['stopwatch'])); |
171
|
|
|
break; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param ContainerBuilder $container |
177
|
|
|
* @param Definition $parent |
|
|
|
|
178
|
|
|
* @param array $config |
179
|
|
|
*/ |
180
|
|
|
private function configureAuthentication(ContainerBuilder $container, array $config) |
181
|
|
|
{ |
182
|
|
|
foreach ($config as $name => $values) { |
183
|
|
|
$authServiceKey = sprintf('httplug.plugin.authentication.%s.auth', $name); |
184
|
|
|
switch ($values['type']) { |
185
|
|
|
case 'bearer': |
186
|
|
|
$container->register($authServiceKey, Bearer::class) |
187
|
|
|
->addArgument($values['token']); |
188
|
|
|
break; |
189
|
|
|
case 'basic': |
190
|
|
|
$container->register($authServiceKey, BasicAuth::class) |
191
|
|
|
->addArgument($values['username']) |
192
|
|
|
->addArgument($values['password']); |
193
|
|
|
break; |
194
|
|
|
case 'wsse': |
195
|
|
|
$container->register($authServiceKey, Wsse::class) |
196
|
|
|
->addArgument($values['username']) |
197
|
|
|
->addArgument($values['password']); |
198
|
|
|
break; |
199
|
|
|
case 'service': |
200
|
|
|
$authServiceKey = $values['service']; |
201
|
|
|
break; |
202
|
|
|
default: |
203
|
|
|
throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type'])); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$container->register('httplug.plugin.authentication.'.$name, AuthenticationPlugin::class) |
207
|
|
|
->addArgument(new Reference($authServiceKey)); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: