Completed
Pull Request — master (#37)
by Tobias
31:10 queued 31:10
created

HttplugExtension::configurePlugins()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
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
 */
17
class HttplugExtension extends Extension
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function load(array $configs, ContainerBuilder $container)
23
    {
24
        $configuration = $this->getConfiguration($configs, $container);
25
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Documentation introduced by
$configuration is of type object|null, but the function expects a object<Symfony\Component...ConfigurationInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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