EnvironmentExtension::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of the Behat Environment Extension.
4
 * (c) Erik Wohllebe <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Wohlie\Behat\Environment\ServiceContainer;
11
12
use Behat\Behat\Context\ServiceContainer\ContextExtension;
13
use Behat\Testwork\ServiceContainer\Extension;
14
use Behat\Testwork\ServiceContainer\ExtensionManager;
15
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
class EnvironmentExtension implements Extension
21
{
22
    const ID_ENV = 'wohlie_env';
23
    const ID_ENV_VERSION_RESOLVER = 'wohlie_env.version_resolver';
24
    const ID_ENV_CONTEXT_INITIALIZER = 'wohlie_env.context_initializer';
25
26
    /**
27
     * Returns the extension config key.
28
     *
29
     * @return string
30
     */
31
    public function getConfigKey()
32
    {
33
        return self::ID_ENV;
34
    }
35
36
    /**
37
     * Setups configuration for the extension.
38
     *
39
     * @param ArrayNodeDefinition $builder
40
     */
41
    public function configure(ArrayNodeDefinition $builder)
42
    {
43
        //@formatter:off
44
        $builder
45
            ->children()
46
                ->scalarNode('property_file')
47
                ->info('Path to property file which can be a *.property or package.json file.')
48
                ->validate()
49
                ->ifEmpty()
50
                ->thenInvalid("Failed to find file.")
51
                ->end()
52
            ->end();
53
        //@formatter:on
54
    }
55
56
    /**
57
     * Loads extension services into temporary container.
58
     *
59
     * @param ContainerBuilder $container
60
     * @param array            $config
61
     */
62
    public function load(ContainerBuilder $container, array $config)
63
    {
64
        $this->loadVersionResolver($container, $config);
65
        $this->loadEnv($container);
66
        $this->loadContextInitializer($container);
67
    }
68
69
    /**
70
     * You can modify the container here before it is dumped to PHP code.
71
     *
72
     * @param ContainerBuilder $container
73
     */
74
    public function process(ContainerBuilder $container)
75
    {
76
    }
77
78
    /**
79
     * Initializes other extensions.
80
     *
81
     * This method is called immediately after all extensions are activated but
82
     * before any extension `configure()` method is called. This allows extensions
83
     * to hook into the configuration of other extensions providing such an
84
     * extension point.
85
     *
86
     * @param ExtensionManager $extensionManager
87
     */
88
    public function initialize(ExtensionManager $extensionManager)
89
    {
90
    }
91
92
    protected function loadVersionResolver(ContainerBuilder $container, array $config)
93
    {
94
        $container->setDefinition(
95
            self::ID_ENV_VERSION_RESOLVER,
96
            new Definition(
97
                'Wohlie\Behat\Environment\Resolver\Version',
98
                [
99
                    $config['property_file'],
100
                ]
101
            )
102
        );
103
    }
104
105
    /**
106
     * Initialize the environment.
107
     *
108
     * @param ContainerBuilder $container
109
     */
110
    protected function loadEnv(ContainerBuilder $container)
111
    {
112
        $container->setDefinition(
113
            self::ID_ENV,
114
            new Definition(
115
                'Wohlie\Behat\Environment\Env',
116
                [
117
                    new Reference(self::ID_ENV_VERSION_RESOLVER),
118
                ]
119
            )
120
        );
121
    }
122
123
    /**
124
     * Define the context initializer.
125
     *
126
     * @param ContainerBuilder $container
127
     */
128
    protected function loadContextInitializer(ContainerBuilder $container)
129
    {
130
        $definition = new Definition(
131
            'Wohlie\Behat\Environment\Context\Initializer\EnvAwareInitializer',
132
            [
133
                new Reference(self::ID_ENV),
134
            ]
135
        );
136
        $definition->addTag(ContextExtension::INITIALIZER_TAG, ['priority' => 0]);
137
        $container->setDefinition(self::ID_ENV_CONTEXT_INITIALIZER, $definition);
138
    }
139
}
140