Completed
Push — master ( f920ce...c63bfe )
by Kamil
16:08
created

enableSupportForLazyServicesIfPossible()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ContextServiceExtension package.
5
 *
6
 * (c) Kamil Kokot <[email protected]>
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 FriendsOfBehat\ContextServiceExtension\ServiceContainer\Scenario;
13
14
use ProxyManager\Configuration as ProxyManagerConfiguration;
15
use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\Config\Loader\DelegatingLoader;
18
use Symfony\Component\Config\Loader\LoaderResolver;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Loader;
21
22
/**
23
 * @internal
24
 */
25
final class ContainerFactory
26
{
27
    /**
28
     * @param string $basePath
29
     * @param array $importedFiles
30
     *
31
     * @return ContainerBuilder
32
     */
33
    public function createContainer($basePath, array $importedFiles = [])
34
    {
35
        $container = new ContainerBuilder();
36
37
        $this->enableSupportForLazyServicesIfPossible($container);
38
39
        $loader = $this->createLoader($container, $basePath);
40
        foreach ($importedFiles as $file) {
41
            $loader->load($file);
42
        }
43
44
        return $container;
45
    }
46
47
    /**
48
     * @param ContainerBuilder $container
49
     * @param string $basePath
50
     *
51
     * @return DelegatingLoader
52
     */
53
    private function createLoader(ContainerBuilder $container, $basePath)
54
    {
55
        $fileLocator = new FileLocator($basePath);
56
        $loader = new DelegatingLoader(new LoaderResolver([
57
            new Loader\XmlFileLoader($container, $fileLocator),
58
            new Loader\YamlFileLoader($container, $fileLocator),
59
            new Loader\PhpFileLoader($container, $fileLocator),
60
        ]));
61
62
        return $loader;
63
    }
64
65
    /**
66
     * @param ContainerBuilder $container
67
     */
68
    private function enableSupportForLazyServicesIfPossible(ContainerBuilder $container)
69
    {
70
        if (class_exists(ProxyManagerConfiguration::class) && class_exists(RuntimeInstantiator::class)) {
71
            $container->setProxyInstantiator(new RuntimeInstantiator());
72
        }
73
    }
74
}
75