Completed
Pull Request — master (#33)
by Márk
31:08
created

Configuration::configureClients()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 1
eloc 18
nc 1
nop 1
1
<?php
2
3
namespace Http\HttplugBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\ArrayNode;
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
10
11
/**
12
 * This class contains the configuration information for the bundle.
13
 *
14
 * This information is solely responsible for how the different configuration
15
 * sections are normalized, and merged.
16
 *
17
 * @author David Buchmann <[email protected]>
18
 */
19
class Configuration implements ConfigurationInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getConfigTreeBuilder()
25
    {
26
        $treeBuilder = new TreeBuilder();
27
        $rootNode = $treeBuilder->root('httplug');
28
29
        $this->configureClients($rootNode);
30
31
        $rootNode
32
            ->validate()
33
                ->ifTrue(function ($v) {
34
                    return !empty($v['classes']['client'])
35
                        || !empty($v['classes']['message_factory'])
36
                        || !empty($v['classes']['uri_factory'])
37
                        || !empty($v['classes']['stream_factory']);
38
                })
39
                ->then(function ($v) {
40
                    foreach ($v['classes'] as $key => $class) {
41
                        if (null !== $class && !class_exists($class)) {
42
                            throw new InvalidConfigurationException(sprintf(
43
                                'Class %s specified for httplug.classes.%s does not exist.',
44
                                $class,
45
                                $key
46
                            ));
47
                        }
48
                    }
49
50
                    return $v;
51
                })
52
            ->end()
53
            ->children()
54
                ->arrayNode('main_alias')
55
                    ->addDefaultsIfNotSet()
56
                    ->info('Configure which service the main alias point to.')
57
                    ->children()
58
                        ->scalarNode('client')->defaultValue('httplug.client.default')->end()
59
                        ->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end()
60
                        ->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end()
61
                        ->scalarNode('stream_factory')->defaultValue('httplug.stream_factory.default')->end()
62
                    ->end()
63
                ->end()
64
                ->arrayNode('classes')
65
                    ->addDefaultsIfNotSet()
66
                    ->info('Overwrite a service class instead of using the discovery mechanism.')
67
                    ->children()
68
                        ->scalarNode('client')->defaultNull()->end()
69
                        ->scalarNode('message_factory')->defaultNull()->end()
70
                        ->scalarNode('uri_factory')->defaultNull()->end()
71
                        ->scalarNode('stream_factory')->defaultNull()->end()
72
                    ->end()
73
                ->end()
74
                ->arrayNode('toolbar')
75
                    ->addDefaultsIfNotSet()
76
                    ->info('Extend the debug profiler with inforation about requests.')
77
                    ->children()
78
                        ->enumNode('enabled')
79
                            ->info('If "auto" (default), the toolbar is activated when kernel.debug is true. You can force the toolbar on and off by changing this option.')
80
                            ->values([true, false, 'auto'])
81
                            ->defaultValue('auto')
82
                        ->end()
83
                        ->scalarNode('formatter')->defaultNull()->end()
84
                    ->end()
85
                ->end()
86
            ->end();
87
88
        return $treeBuilder;
89
    }
90
91
    protected function configureClients(ArrayNodeDefinition $root)
92
    {
93
        $root->children()
94
            ->arrayNode('clients')
95
                ->useAttributeAsKey('name')
96
                ->prototype('array')
97
                ->children()
98
                    ->scalarNode('factory')
99
                        ->isRequired()
100
                        ->cannotBeEmpty()
101
                        ->info('The service id of a factory to use when creating the adapter.')
102
                    ->end()
103
                    ->arrayNode('plugins')
104
                        ->info('A list of service ids of plugins. The order is important.')
105
                        ->prototype('scalar')->end()
106
                    ->end()
107
                    ->variableNode('config')->end()
108
                ->end()
109
            ->end();
110
    }
111
}
112