Completed
Pull Request — master (#68)
by Vladimir
03:57
created

ContainerBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 67
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A build() 0 9 2
A isPhar() 0 4 1
A compileAndDump() 0 40 2
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Console;
9
10
use allejo\stakx\DataTransformer\DataTransformer;
11
use allejo\stakx\MarkupEngine\MarkupEngine;
12
use allejo\stakx\Templating\Twig\Extension\TwigFilterInterface;
13
use allejo\stakx\Templating\Twig\Extension\TwigFunctionInterface;
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder as BaseBuilder;
16
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
17
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
18
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
19
20
class ContainerBuilder
21
{
22
    private $containerPath;
23
    private $options;
24
25
    public function __construct(array $options)
26
    {
27
        $this->containerPath = __DIR__ . '/Container.php';
28
        $this->options = $options;
29
    }
30
31
    public function build()
32
    {
33
        if (!$this->isPhar())
34
        {
35
            $this->compileAndDump();
36
        }
37
38
        return $this->containerPath;
39
    }
40
41
    private function isPhar()
42
    {
43
        return strlen(\Phar::running()) > 0;
44
    }
45
46
    private function compileAndDump()
47
    {
48
        $container = new BaseBuilder();
49
        $container
50
            ->addCompilerPass(new RegisterListenersPass())
51
        ;
52
53
        foreach ($this->options['parameters'] as $key => $value)
54
        {
55
            $container->setParameter($key, $value);
56
        }
57
58
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/app/'));
59
        $loader->load('services.yml');
60
61
        $container
62
            ->registerForAutoconfiguration(DataTransformer::class)
63
            ->addTag(DataTransformer::CONTAINER_TAG)
64
        ;
65
66
        $container
67
            ->registerForAutoconfiguration(MarkupEngine::class)
68
            ->addTag(MarkupEngine::CONTAINER_TAG)
69
        ;
70
71
        $container
72
            ->registerForAutoconfiguration(TwigFilterInterface::class)
73
            ->addTag(TwigFilterInterface::CONTAINER_TAG)
74
        ;
75
76
        $container
77
            ->registerForAutoconfiguration(TwigFunctionInterface::class)
78
            ->addTag(TwigFunctionInterface::CONTAINER_TAG)
79
        ;
80
81
        $container->compile();
82
83
        $dumper = new PhpDumper($container);
84
        file_put_contents($this->containerPath, $dumper->dump());
85
    }
86
}
87