Completed
Push — master ( e5e74e...13adb5 )
by Yo
02:16
created

KernelSubExtension::loadContainerParameter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
namespace Yoanm\Behat3SymfonyExtension\ServiceContainer\SubExtension;
3
4
use Behat\MinkExtension\ServiceContainer\MinkExtension;
5
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
6
use Behat\Testwork\ServiceContainer\Exception\ProcessingException;
7
use Behat\Testwork\ServiceContainer\ExtensionManager;
8
use Symfony\Component\BrowserKit\CookieJar;
9
use Symfony\Component\BrowserKit\History;
10
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Reference;
13
use Yoanm\Behat3SymfonyExtension\Client\Client;
14
use Yoanm\Behat3SymfonyExtension\Context\Initializer\KernelAwareInitializer;
15
use Yoanm\Behat3SymfonyExtension\Context\Initializer\KernelHandlerAwareInitializer;
16
use Yoanm\Behat3SymfonyExtension\Handler\KernelHandler;
17
use Yoanm\Behat3SymfonyExtension\ServiceContainer\AbstractExtension;
18
use Yoanm\Behat3SymfonyExtension\ServiceContainer\DriverFactory\Behat3SymfonyFactory;
19
use Yoanm\Behat3SymfonyExtension\Subscriber\RebootKernelSubscriber;
20
21
class KernelSubExtension extends AbstractExtension
22
{
23
    /**
24
     * @inheritDoc
25
     */
26 3
    public function getConfigKey()
27
    {
28 3
        return 'kernel';
29
    }
30
31
    // @codeCoverageIgnoreStart
32
    // Not possible to cover this because ExtensionManager is a final class
33
    /**
34
     * @inheritDoc
35
     */
36
    public function initialize(ExtensionManager $extensionManager)
37
    {
38
        $minExtension = $extensionManager->getExtension('mink');
39
        if ($minExtension instanceof MinkExtension) {
40
            $minExtension->registerDriverFactory(new Behat3SymfonyFactory());
41
        }
42
    }
43
    // @codeCoverageIgnoreEnd
44
45
    // @codeCoverageIgnoreStart
46
    // Will be covered by FT
47
    /**
48
     * @inheritDoc
49
     */
50
    public function configure(ArrayNodeDefinition $builder)
51
    {
52
        $castToBool = function ($value) {
53
            $filtered = filter_var(
54
                $value,
55
                FILTER_VALIDATE_BOOLEAN,
56
                FILTER_NULL_ON_FAILURE
57
            );
58
59
            return (null === $filtered) ? (bool) $value : $filtered;
60
        };
61
        $builder
62
            ->addDefaultsIfNotSet()
63
            ->children()
64
                ->scalarNode('bootstrap')
65
                    ->defaultValue('app/autoload.php')
66
                ->end()
67
                ->scalarNode('path')
68
                    ->defaultValue('app/AppKernel.php')
69
                ->end()
70
                ->scalarNode('class')
71
                    ->defaultValue('AppKernel')
72
                ->end()
73
                ->scalarNode('env')
74
                    ->defaultValue('test')
75
                ->end()
76
                ->booleanNode('debug')
77
                    ->beforeNormalization()
78
                    ->always()
79
                        ->then($castToBool)
80
                    ->end()
81
                    ->defaultTrue()
82
                ->end()
83
                ->booleanNode('reboot')
84
                    ->info('If true symfony kernel will be rebooted after each scenario/example')
85
                    ->beforeNormalization()
86
                        ->always()
87
                        ->then($castToBool)
88
                    ->end()
89
                    ->defaultTrue()
90
                ->end()
91
            ->end();
92
    }
93
    // @codeCoverageIgnoreEnd
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 2
    public function load(ContainerBuilder $container, array $config)
99
    {
100 2
        $kernelConfig = $config[$this->getConfigKey()];
101
102 2
        $this->loadContainerParameter($container, $kernelConfig);
103 2
        $this->loadInitializer($container);
104 2
        $this->loadSubscriber($container, $kernelConfig);
105 2
        $this->createService(
106 2
            $container,
107 2
            'test.client',
108 2
            Client::class,
109
            [
110 2
                    new Reference($this->buildContainerId('handler.kernel')),
111 2
                    new Reference(self::KERNEL_SERVICE_ID),
112 2
                    [],
113 2
                    new Reference($this->buildContainerId('test.client.history')),
114 2
                    new Reference($this->buildContainerId('test.client.cookiejar'))
115 2
                ]
116 2
        );
117 2
        $this->createService(
118 2
            $container,
119 2
            'test.client.history',
120
            History::class
121 2
        );
122 2
        $this->createService(
123 2
            $container,
124 2
            'test.client.cookiejar',
125
            CookieJar::class
126 2
        );
127 2
        $this->createService(
128 2
            $container,
129 2
            'kernel',
130 2
            $kernelConfig['class'],
131 2
            [$kernelConfig['env'], $kernelConfig['debug']],
132 2
            [],
133 2
            [['boot']]
134 2
        );
135 2
        $this->createService(
136 2
            $container,
137 2
            'handler.kernel',
138 2
            KernelHandler::class,
139
            [
140 2
                new Reference('event_dispatcher'),
141 2
                new Reference(self::KERNEL_SERVICE_ID),
142
            ]
143 2
        );
144 2
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 3
    public function process(ContainerBuilder $container)
150
    {
151 3
        $basePath = $container->getParameter('paths.base');
152 3
        $bootstrapPath = $container->getParameter($this->buildContainerId('kernel.bootstrap'));
153 3
        if ($bootstrapPath) {
154 2
            $bootstrapPathUnderBasePath = sprintf('%s/%s', $basePath, $bootstrapPath);
155 2
            if (file_exists($bootstrapPathUnderBasePath)) {
156 1
                $bootstrapPath = $bootstrapPathUnderBasePath;
157 1
            }
158 2
            if (file_exists($bootstrapPath)) {
159 1
                require_once($bootstrapPath);
160 1
            } else {
161 1
                throw new ProcessingException('Could not find bootstrap file !');
162
            }
163 1
        }
164
165
        // load kernel
166 2
        $kernelPath = $container->getParameter($this->buildContainerId('kernel.path'));
167 2
        $kernelPathUnderBasePath = sprintf('%s/%s', $basePath, $kernelPath);
168 2
        if (file_exists($kernelPathUnderBasePath)) {
169
            $kernelPath = $kernelPathUnderBasePath;
170
        }
171
172 2
        $container->getDefinition(self::KERNEL_SERVICE_ID)
173 2
            ->setFile($kernelPath);
174 2
    }
175
176
    /**
177
     * @param ContainerBuilder $container
178
     */
179 2
    protected function loadInitializer(ContainerBuilder $container)
180
    {
181 2
        $this->createService(
182 2
            $container,
183 2
            'initializer.kernel_aware',
184 2
            KernelAwareInitializer::class,
185 2
            [new Reference(self::KERNEL_SERVICE_ID)],
186 2
            ['context.initializer']
187 2
        );
188
189 2
        $this->createService(
190 2
            $container,
191 2
            'initializer.kernel_handler_aware',
192 2
            KernelHandlerAwareInitializer::class,
193 2
            [new Reference($this->buildContainerId('handler.kernel'))],
194 2
            ['context.initializer']
195 2
        );
196 2
    }
197
198
    /**
199
     * @param ContainerBuilder $container
200
     * @param $kernelConfig
201
     */
202 2
    protected function loadSubscriber(ContainerBuilder $container, $kernelConfig)
203
    {
204 2
        if (true === $kernelConfig['reboot']) {
205 1
            $this->createService(
206 1
                $container,
207 1
                'subscriber.reboot_kernel',
208 1
                RebootKernelSubscriber::class,
209 1
                [new Reference($this->buildContainerId('handler.kernel'))],
210 1
                [EventDispatcherExtension::SUBSCRIBER_TAG]
211 1
            );
212 1
        }
213 2
    }
214
215
    /**
216
     * @param ContainerBuilder $container
217
     * @param $kernelConfig
218
     */
219 2
    protected function loadContainerParameter(ContainerBuilder $container, $kernelConfig)
220
    {
221 2
        foreach ($kernelConfig as $key => $value) {
222 2
            $container->setParameter($this->buildContainerId(sprintf('kernel.%s', $key)), $value);
223 2
        }
224 2
    }
225
}
226