1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Http\Client\Plugin\PluginClient; |
6
|
|
|
use Http\HttplugBundle\ClientFactory\DummyClient; |
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
10
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
12
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author David Buchmann <[email protected]> |
16
|
|
|
* @author Tobias Nyholm <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class HttplugExtension extends Extension |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function load(array $configs, ContainerBuilder $container) |
24
|
|
|
{ |
25
|
|
|
$configuration = $this->getConfiguration($configs, $container); |
26
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
29
|
|
|
|
30
|
|
|
$loader->load('services.xml'); |
31
|
|
|
$loader->load('plugins.xml'); |
32
|
|
|
$loader->load('discovery.xml'); |
33
|
|
|
|
34
|
|
|
$enabled = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug'); |
35
|
|
|
if ($enabled) { |
36
|
|
|
$loader->load('data-collector.xml'); |
37
|
|
|
$config['_inject_collector_plugin'] = true; |
38
|
|
|
|
39
|
|
|
if (!empty($config['toolbar']['formatter'])) { |
40
|
|
|
$container->getDefinition('httplug.collector.message_journal') |
41
|
|
|
->replaceArgument(0, new Reference($config['toolbar']['formatter'])); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
foreach ($config['classes'] as $service => $class) { |
46
|
|
|
if (!empty($class)) { |
47
|
|
|
$container->removeDefinition(sprintf('httplug.%s.default', $service)); |
48
|
|
|
$container->register(sprintf('httplug.%s.default', $service), $class); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
foreach ($config['main_alias'] as $type => $id) { |
53
|
|
|
$container->setAlias(sprintf('httplug.%s', $type), $id); |
54
|
|
|
} |
55
|
|
|
$this->configurePlugins($container, $config['plugins']); |
56
|
|
|
$this->configureClients($container, $config); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Configure client services. |
61
|
|
|
* |
62
|
|
|
* @param ContainerBuilder $container |
63
|
|
|
* @param array $config |
64
|
|
|
*/ |
65
|
|
|
private function configureClients(ContainerBuilder $container, array $config) |
66
|
|
|
{ |
67
|
|
|
$first = isset($config['clients']['default']) ? 'default' : null; |
68
|
|
|
foreach ($config['clients'] as $name => $arguments) { |
69
|
|
|
if ($first === null) { |
70
|
|
|
$first = $name; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (isset($config['_inject_collector_plugin'])) { |
74
|
|
|
array_unshift($arguments['plugins'], 'httplug.collector.history_plugin'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$def = $container->register('httplug.client.'.$name, DummyClient::class); |
78
|
|
|
|
79
|
|
|
if (empty($arguments['plugins'])) { |
80
|
|
|
$def->setFactory([new Reference($arguments['factory']), 'createClient']) |
81
|
|
|
->addArgument($arguments['config']); |
82
|
|
|
} else { |
83
|
|
|
$def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient') |
84
|
|
|
->addArgument(array_map(function ($id) { |
85
|
|
|
return new Reference($id); |
86
|
|
|
}, $arguments['plugins'])) |
87
|
|
|
->addArgument(new Reference($arguments['factory'])) |
88
|
|
|
->addArgument($arguments['config']); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// If we have clients configured |
93
|
|
|
if ($first !== null) { |
94
|
|
|
if ($first !== 'default') { |
95
|
|
|
// Alias the first client to httplug.client.default |
96
|
|
|
$container->setAlias('httplug.client.default', 'httplug.client.'.$first); |
97
|
|
|
} |
98
|
|
|
} elseif (isset($config['_inject_collector_plugin'])) { |
99
|
|
|
// No client was configured. Make sure to inject history plugin to the auto discovery client. |
100
|
|
|
$container->register('httplug.client', PluginClient::class) |
101
|
|
|
->addArgument(new Reference('httplug.client.default')) |
102
|
|
|
->addArgument([new Reference('httplug.collector.history_plugin')]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param ContainerBuilder $container |
108
|
|
|
* @param array $config |
109
|
|
|
*/ |
110
|
|
|
private function configurePlugins(ContainerBuilder $container, array $config) |
111
|
|
|
{ |
112
|
|
|
foreach ($config as $name => $pluginConfig) { |
113
|
|
|
$pluginId = 'httplug.plugin.'.$name; |
114
|
|
|
if ($pluginConfig['enabled']) { |
115
|
|
|
$def = $container->getDefinition($pluginId); |
116
|
|
|
$this->configurePluginByName($name, $def, $pluginConfig); |
117
|
|
|
} else { |
118
|
|
|
$container->removeDefinition($pluginId); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $name |
125
|
|
|
* @param Definition $definition |
126
|
|
|
* @param array $config |
127
|
|
|
*/ |
128
|
|
|
private function configurePluginByName($name, Definition $definition, array $config) |
129
|
|
|
{ |
130
|
|
|
switch ($name) { |
131
|
|
|
case 'authentication': |
132
|
|
|
$definition->replaceArgument(0, new Reference($config['authentication'])); |
133
|
|
|
break; |
134
|
|
|
case 'cache': |
135
|
|
|
$definition |
136
|
|
|
->replaceArgument(0, new Reference($config['cache_pool'])) |
137
|
|
|
->replaceArgument(1, new Reference($config['stream_factory'])) |
138
|
|
|
->replaceArgument(2, $config['config']); |
139
|
|
|
break; |
140
|
|
|
case 'cookie': |
141
|
|
|
$definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
142
|
|
|
break; |
143
|
|
|
case 'decoder': |
144
|
|
|
$definition->addArgument($config['use_content_encoding']); |
145
|
|
|
break; |
146
|
|
|
case 'history': |
147
|
|
|
$definition->replaceArgument(0, new Reference($config['journal'])); |
148
|
|
|
break; |
149
|
|
|
case 'logger': |
150
|
|
|
$definition->replaceArgument(0, new Reference($config['logger'])); |
151
|
|
|
if (!empty($config['formatter'])) { |
152
|
|
|
$definition->replaceArgument(1, new Reference($config['formatter'])); |
153
|
|
|
} |
154
|
|
|
break; |
155
|
|
|
case 'redirect': |
156
|
|
|
$definition |
157
|
|
|
->addArgument($config['preserve_header']) |
158
|
|
|
->addArgument($config['use_default_for_multiple']); |
159
|
|
|
break; |
160
|
|
|
case 'retry': |
161
|
|
|
$definition->addArgument($config['retry']); |
162
|
|
|
break; |
163
|
|
|
case 'stopwatch': |
164
|
|
|
$definition->replaceArgument(0, new Reference($config['stopwatch'])); |
165
|
|
|
break; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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: