Completed
Pull Request — master (#190)
by
unknown
03:12
created

Extension::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace Knp\FriendlyContexts;
4
5
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
6
use Behat\Testwork\ServiceContainer\ExtensionManager;
7
use Knp\FriendlyContexts\DependencyInjection\Compiler;
8
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
12
13
class Extension implements ExtensionInterface
14
{
15
    public function initialize(ExtensionManager $extensionManager)
16
    {
17
    }
18
19
    public function load(ContainerBuilder $container, array $config)
20
    {
21
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/services'));
22
        $loader->load('core.yml');
23
        $loader->load('fakers.yml');
24
        $loader->load('guessers.yml');
25
        $loader->load('builder.yml');
26
27
        $container->setParameter('friendly.parameters', $config);
28
29
        $container->addCompilerPass(new Compiler\ConfigPass);
30
        $container->addCompilerPass(new Compiler\FormatGuesserPass);
31
        $container->addCompilerPass(new Compiler\FakerProviderPass);
32
        $container->addCompilerPass(new Compiler\ApiUrlPass);
33
        $container->addCompilerPass(new Compiler\KernelPass);
34
    }
35
36
    public function configure(ArrayNodeDefinition $builder)
37
    {
38
        $builder
39
            ->children()
40
                ->arrayNode('symfony_kernel')
41
                    ->addDefaultsIfNotSet()
42
                    ->children()
43
                        ->scalarNode('bootstrap')
44
                            ->defaultValue('app/autoload.php')
45
                        ->end()
46
                        ->scalarNode('path')
47
                            ->defaultValue('app/AppKernel.php')
48
                        ->end()
49
                        ->scalarNode('class')
50
                            ->defaultValue('AppKernel')
51
                        ->end()
52
                        ->scalarNode('env')
53
                            ->defaultValue('test')
54
                        ->end()
55
                        ->booleanNode('debug')
56
                            ->defaultTrue()
57
                        ->end()
58
                    ->end()
59
                ->end()
60
                ->arrayNode('doctrine')
61
                    ->addDefaultsIfNotSet()
62
                    ->children()
63
                        ->scalarNode('service')
64
                            ->defaultValue('doctrine')
65
                        ->end()
66
                    ->end()
67
                ->end()
68
                ->arrayNode('alice')
69
                    ->addDefaultsIfNotSet()
70
                    ->children()
71
                        ->scalarNode('locale')
72
                            ->defaultValue('en_US')
73
                        ->end()
74
                        ->arrayNode('fixtures')
75
                            ->prototype('scalar')->end()
76
                        ->end()
77
                        ->arrayNode('dependencies')
78
                            ->useAttributeAsKey('name')
79
                            ->prototype('array')
80
                                ->prototype('scalar')->end()
81
                            ->end()
82
                        ->end()
83
                        ->arrayNode('providers')
84
                            ->prototype('scalar')->end()
85
                        ->end()
86
                    ->end()
87
                ->end()
88
                ->arrayNode('entities')
89
                    ->addDefaultsIfNotSet()
90
                    ->children()
91
                        ->arrayNode('namespaces')
92
                            ->prototype('scalar')->end()
93
                        ->end()
94
                    ->end()
95
                ->end()
96
                ->arrayNode('page')
97
                    ->addDefaultsIfNotSet()
98
                    ->children()
99
                        ->scalarNode('namespace')
100
                            ->defaultValue('Page')
101
                        ->end()
102
                    ->end()
103
                ->end()
104
                ->arrayNode('api')
105
                    ->addDefaultsIfNotSet()
106
                    ->children()
107
                        ->scalarNode('base_url')
108
                            ->defaultValue('')
109
                        ->end()
110
                    ->end()
111
                ->end()
112
                ->scalarNode('smartTag')
113
                    ->defaultValue('smartStep')
114
                ->end()
115
            ->end()
116
        ;
117
    }
118
119
    public function process(ContainerBuilder $container)
120
    {
121
    }
122
123
    public function getConfigKey()
124
    {
125
        return 'friendly';
126
    }
127
128 View Code Duplication
    protected function buildParameters($name, &$parameters, $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        foreach ($config as $key => $element) {
131
            if (is_array($element) && $this->arrayHasStringKeys($element)) {
132
                $this->buildParameters(sprintf('%s.%s', $name, $key), $parameters, $element);
133
            }
134
            $parameters[sprintf('%s.%s', $name, $key)] = $element;
135
        }
136
    }
137
138
    protected function arrayHasStringKeys(array $array)
139
    {
140
        foreach ($array as $key => $value) {
141
            if (is_string($key)) {
142
143
                return true;
144
            }
145
        }
146
147
        return false;
148
    }
149
}
150