Completed
Push — master ( b716f1...2f9340 )
by Kamil
18:43
created

SymfonyExtension::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\SymfonyExtension\ServiceContainer;
13
14
use Behat\MinkExtension\ServiceContainer\MinkExtension;
15
use Behat\Symfony2Extension\ServiceContainer\Symfony2Extension;
16
use Behat\Testwork\ServiceContainer\Extension;
17
use Behat\Testwork\ServiceContainer\ExtensionManager;
18
use Sylius\Behat\SymfonyExtension\Factory\IsolatedSymfonyFactory;
19
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20
use Symfony\Component\DependencyInjection\Container;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Definition;
23
use Symfony\Component\DependencyInjection\Reference;
24
25
/**
26
 * @author Kamil Kokot <[email protected]>
27
 */
28
final class SymfonyExtension implements Extension
29
{
30
    /**
31
     * Kernel used inside Behat contexts or to create services injected to them
32
     * Container is built before every scenario
33
     */
34
    const KERNEL_ID = Symfony2Extension::KERNEL_ID;
35
36
    /**
37
     * Kernel used by Symfony2 driver to isolate web container from contexts' container
38
     * Container is built before every request
39
     */
40
    const DRIVER_KERNEL_ID = 'sylius_symfony_extension.driver_kernel';
41
42
    /**
43
     * Kernel that should be used by extensions only
44
     * Container is built only once at the first use
45
     */
46
    const SHARED_KERNEL_ID = 'sylius_symfony_extension.shared_kernel';
47
48
    /**
49
     * The only container built by shared kernel
50
     * To be used as a factory for shared injected application services
51
     */
52
    const SHARED_KERNEL_CONTAINER_ID = 'sylius_symfony_extension.shared_kernel.container';
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getConfigKey()
58
    {
59
        return 'sylius_symfony';
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function initialize(ExtensionManager $extensionManager)
66
    {
67
        /** @var MinkExtension $minkExtension */
68
        $minkExtension = $extensionManager->getExtension('mink');
69
        if (null === $minkExtension) {
70
            return;
71
        }
72
73
        // Overrides default Symfony2Extension driver factory
74
        // Have to be registered after that one
75
        $minkExtension->registerDriverFactory(new IsolatedSymfonyFactory());
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function configure(ArrayNodeDefinition $builder)
82
    {
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function load(ContainerBuilder $container, array $config)
89
    {
90
        $this->declareDriverKernel($container);
91
92
        $this->declareSharedKernel($container);
93
        $this->declareSharedKernelContainer($container);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function process(ContainerBuilder $container)
100
    {
101
    }
102
103
    /**
104
     * @param ContainerBuilder $container
105
     */
106
    private function declareDriverKernel(ContainerBuilder $container)
107
    {
108
        $container->setDefinition(self::DRIVER_KERNEL_ID, $container->findDefinition(self::KERNEL_ID));
109
    }
110
111
    /**
112
     * @param ContainerBuilder $container
113
     */
114
    private function declareSharedKernel(ContainerBuilder $container)
115
    {
116
        $container->setDefinition(self::SHARED_KERNEL_ID, $container->findDefinition(self::KERNEL_ID));
117
    }
118
119
    /**
120
     * @param ContainerBuilder $container
121
     */
122
    private function declareSharedKernelContainer(ContainerBuilder $container)
123
    {
124
        $sharedContainerDefinition = new Definition(Container::class);
125
        $sharedContainerDefinition->setFactory([
126
            new Reference(self::SHARED_KERNEL_ID),
127
            'getContainer',
128
        ]);
129
130
        $container->setDefinition(self::SHARED_KERNEL_CONTAINER_ID, $sharedContainerDefinition);
131
    }
132
}
133