Completed
Push — master ( 58ff14...022ece )
by Samuel
01:27
created

Symfony2Extension::process()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 13
nop 1
dl 0
loc 27
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Behat Symfony2Extension
5
 *
6
 * (c) Konstantin Kudryashov <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Behat\Symfony2Extension\ServiceContainer;
13
14
use Behat\Behat\Context\ServiceContainer\ContextExtension;
15
use Behat\Behat\Gherkin\ServiceContainer\GherkinExtension;
16
use Behat\Symfony2Extension\ServiceContainer\Driver\SymfonyFactory;
17
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
18
use Behat\Testwork\ServiceContainer\Exception\ProcessingException;
19
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
20
use Behat\Testwork\ServiceContainer\ExtensionManager;
21
use Behat\Testwork\Specification\ServiceContainer\SpecificationExtension;
22
use Behat\Testwork\Suite\ServiceContainer\SuiteExtension;
23
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
24
use Symfony\Component\DependencyInjection\ContainerBuilder;
25
use Symfony\Component\DependencyInjection\Definition;
26
use Symfony\Component\DependencyInjection\Reference;
27
28
/**
29
 * Symfony2 extension for Behat class.
30
 *
31
 * @author Konstantin Kudryashov <[email protected]>
32
 * @author Christophe Coevoet <[email protected]>
33
 */
34
class Symfony2Extension implements ExtensionInterface
35
{
36
    const KERNEL_ID = 'symfony2_extension.kernel';
37
    const DEFAULT_KERNEL_BOOTSTRAP = 'app/autoload.php';
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getConfigKey()
43
    {
44
        return 'symfony2';
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function initialize(ExtensionManager $extensionManager)
51
    {
52
        if (null !== $minkExtension = $extensionManager->getExtension('mink')) {
53
            $minkExtension->registerDriverFactory(new SymfonyFactory());
54
        }
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function configure(ArrayNodeDefinition $builder)
61
    {
62
        $boolFilter = function ($v) {
63
            $filtered = filter_var($v, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
64
65
            return (null === $filtered) ? $v : $filtered;
66
        };
67
68
        $builder
69
            ->addDefaultsIfNotSet()
70
            ->children()
71
                ->arrayNode('kernel')
72
                    ->addDefaultsIfNotSet()
73
                    ->children()
74
                        ->scalarNode('bootstrap')->defaultValue(self::DEFAULT_KERNEL_BOOTSTRAP)->end()
75
                        ->scalarNode('path')->defaultValue('app/AppKernel.php')->end()
76
                        ->scalarNode('class')->defaultValue('AppKernel')->end()
77
                        ->scalarNode('env')->defaultValue('test')->end()
78
                        ->booleanNode('debug')
79
                            ->beforeNormalization()
80
                                ->ifString()->then($boolFilter)
81
                            ->end()
82
                            ->defaultTrue()
83
                        ->end()
84
                    ->end()
85
                ->end()
86
                ->arrayNode('context')
87
                    ->addDefaultsIfNotSet()
88
                    ->children()
89
                        ->scalarNode('path_suffix')
90
                            ->defaultValue('Features')
91
                        ->end()
92
                        ->scalarNode('class_suffix')
93
                            ->defaultValue('Features\Context\FeatureContext')
94
                        ->end()
95
                    ->end()
96
                ->end()
97
            ->end()
98
        ->end();
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function load(ContainerBuilder $container, array $config)
105
    {
106
        $this->loadClassGenerator($container);
107
        $this->loadContextInitializer($container);
108
        $this->loadFeatureLocator($container);
109
        $this->loadKernel($container, $config['kernel']);
110
        $this->loadSuiteGenerator($container, $config['context']);
111
        $this->loadServiceArgumentResolver($container);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function process(ContainerBuilder $container)
118
    {
119
        // get base path
120
        $basePath = $container->getParameter('paths.base');
121
122
        // find and require bootstrap
123
        $bootstrapPath = $container->getParameter('symfony2_extension.kernel.bootstrap');
124
        if ($bootstrapPath) {
125
            if (file_exists($bootstrap = $basePath . '/' . $bootstrapPath)) {
126
                require_once($bootstrap);
127
            } elseif (file_exists($bootstrapPath)) {
128
                require_once($bootstrapPath);
129
            } elseif ($bootstrapPath !== self::DEFAULT_KERNEL_BOOTSTRAP) {
130
                throw new ProcessingException(
131
                    'Could not load bootstrap file. Please check your configuration at "kernel.bootstrap"'
132
                );
133
            }
134
        }
135
136
        // find and require kernel
137
        $kernelPath = $container->getParameter('symfony2_extension.kernel.path');
138
        if (file_exists($kernel = $basePath . '/' . $kernelPath)) {
139
            $container->getDefinition(self::KERNEL_ID)->setFile($kernel);
140
        } elseif (file_exists($kernelPath)) {
141
            $container->getDefinition(self::KERNEL_ID)->setFile($kernelPath);
142
        }
143
    }
144
145
    private function loadClassGenerator(ContainerBuilder $container)
146
    {
147
        $definition = new Definition('Behat\Symfony2Extension\Context\ContextClass\KernelAwareClassGenerator');
148
        $definition->addTag(ContextExtension::CLASS_GENERATOR_TAG, array('priority' => 100));
149
        $container->setDefinition('symfony2_extension.class_generator.kernel_aware', $definition);
150
    }
151
152 View Code Duplication
    private function loadContextInitializer(ContainerBuilder $container)
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...
153
    {
154
        $definition = new Definition('Behat\Symfony2Extension\Context\Initializer\KernelAwareInitializer', array(
155
            new Reference(self::KERNEL_ID),
156
        ));
157
        $definition->addTag(ContextExtension::INITIALIZER_TAG, array('priority' => 0));
158
        $definition->addTag(EventDispatcherExtension::SUBSCRIBER_TAG, array('priority' => 0));
159
        $container->setDefinition('symfony2_extension.context_initializer.kernel_aware', $definition);
160
    }
161
162 View Code Duplication
    private function loadFeatureLocator(ContainerBuilder $container)
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...
163
    {
164
        $definition = new Definition('Behat\Symfony2Extension\Specification\BundleFeatureLocator', array(
165
            new Definition('Behat\Behat\Gherkin\Specification\Locator\FilesystemFeatureLocator', array(
166
                new Reference(GherkinExtension::MANAGER_ID),
167
                '%paths.base%'
168
            ))
169
        ));
170
        $definition->addTag(SpecificationExtension::LOCATOR_TAG, array('priority' => 100));
171
        $container->setDefinition('symfony2_extension.specification_locator.bundle_feature', $definition);
172
    }
173
174
    private function loadKernel(ContainerBuilder $container, array $config)
175
    {
176
        $definition = new Definition($config['class'], array(
177
            $config['env'],
178
            $config['debug'],
179
        ));
180
        $definition->addMethodCall('boot');
181
        $container->setDefinition(self::KERNEL_ID, $definition);
182
        $container->setParameter(self::KERNEL_ID . '.path', $config['path']);
183
        $container->setParameter(self::KERNEL_ID . '.bootstrap', $config['bootstrap']);
184
    }
185
186
    private function loadSuiteGenerator(ContainerBuilder $container, array $config)
187
    {
188
        $definition = new Definition('Behat\Symfony2Extension\Suite\SymfonySuiteGenerator', array(
189
            new Reference(self::KERNEL_ID),
190
            $config['path_suffix'],
191
            $config['class_suffix'],
192
        ));
193
        $definition->addTag(SuiteExtension::GENERATOR_TAG, array('priority' => 100));
194
        $container->setDefinition('symfony2_extension.suite.generator', $definition);
195
    }
196
197
    private function loadServiceArgumentResolver(ContainerBuilder $container)
198
    {
199
        $definition = new Definition('Behat\Symfony2Extension\Context\Argument\ServiceArgumentResolver', array(
200
            new Reference(self::KERNEL_ID)
201
        ));
202
        $definition->addTag(ContextExtension::ARGUMENT_RESOLVER_TAG, array('priority' => 0));
203
        $container->setDefinition('symfony2_extension.context.argument.service_resolver', $definition);
204
    }
205
}
206