Completed
Push — master ( 01982b...3c7394 )
by ANTHONIUS
11s
created

Builder::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Core\DI;
15
16
use Dotfiles\Core\Config\Config;
17
use Dotfiles\Core\DI\Compiler\CommandPass;
18
use Dotfiles\Core\DI\Compiler\ListenerPass;
19
use Symfony\Component\Config\ConfigCache;
20
use Symfony\Component\Config\FileLocator;
21
use Symfony\Component\DependencyInjection\Container;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\DependencyInjection\Dumper\DumperInterface;
24
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
25
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
26
27
class Builder
28
{
29
    /**
30
     * @var string
31
     */
32
    private $cacheFileName;
33
34
    /**
35
     * @var Config
36
     */
37
    private $config;
38
39
    /**
40
     * @var ContainerInterface
41
     */
42
    private $container;
43
44
    /**
45
     * @var ContainerBuilder
46
     */
47
    private $containerBuilder;
48
49
    /**
50
     * @var DumperInterface
51
     */
52
    private $dumper;
53
54
    public function __construct(Config $config)
55
    {
56
        $this->config = $config;
57
    }
58
59
    /**
60
     * @throws \Exception
61
     */
62
    public function compile(): void
63
    {
64
        $cachePath = $this->getCacheFileName();
65
        $cache = new ConfigCache($cachePath, true);
66
        $env = getenv('DOTFILES_ENV');
67
68
        if (!$cache->isFresh() || 'dev' === $env) {
69
            $builder = $this->getContainerBuilder();
70
            $this->configureCoreServices($builder);
71
            $builder->addCompilerPass(new CommandPass());
72
            $builder->addCompilerPass(new ListenerPass());
73
            $builder->compile();
74
75
            $dumper = $this->getDumper();
76
            //file_put_contents($target,$dumper->dump(['class'=>'CachedContainer']), LOCK_EX);
77
            $cache->write($dumper->dump(array('class' => 'CachedContainer')), $builder->getResources());
0 ignored issues
show
Bug introduced by
It seems like $dumper->dump(array('cla... => 'CachedContainer')) can also be of type array; however, parameter $content of Symfony\Component\Config...kerConfigCache::write() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            $cache->write(/** @scrutinizer ignore-type */ $dumper->dump(array('class' => 'CachedContainer')), $builder->getResources());
Loading history...
78
        }
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getCacheFileName(): string
85
    {
86
        return $this->config->get('dotfiles.cache_dir').'/container.php';
87
    }
88
89
    /**
90
     * @return Container
91
     */
92
    public function getContainer()
93
    {
94
        if (null === $this->container) {
95
            if (!class_exists('CachedContainer')) {
96
                include_once $this->getCacheFileName();
97
            }
98
            $this->container = new \CachedContainer();
0 ignored issues
show
Bug introduced by
The type CachedContainer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
99
        }
100
101
        return $this->container;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->container also could return the type Dotfiles\Core\DI\ContainerInterface which is incompatible with the documented return type Symfony\Component\DependencyInjection\Container.
Loading history...
102
    }
103
104
    /**
105
     * @return ContainerBuilder
106
     */
107
    public function getContainerBuilder(): ContainerBuilder
108
    {
109
        if (null === $this->containerBuilder) {
110
            $this->containerBuilder = new ContainerBuilder();
111
        }
112
113
        return $this->containerBuilder;
114
    }
115
116
    /**
117
     * @return DumperInterface
118
     */
119
    public function getDumper(): DumperInterface
120
    {
121
        if (null === $this->dumper) {
122
            $this->dumper = new PhpDumper($this->containerBuilder);
123
        }
124
125
        return $this->dumper;
126
    }
127
128
    /**
129
     * @param string $cacheFileName
130
     *
131
     * @return self
132
     */
133
    public function setCacheFileName(string $cacheFileName): self
134
    {
135
        if (!is_dir($dir = dirname($cacheFileName))) {
136
            mkdir($dir, 0755, true);
137
        }
138
        $this->cacheFileName = $cacheFileName;
139
140
        return $this;
141
    }
142
143
    /**
144
     * @param Config $config
145
     *
146
     * @return Builder
147
     */
148
    public function setConfig(Config $config): self
149
    {
150
        $this->config = $config;
151
152
        return $this;
153
    }
154
155
    /**
156
     * @param ContainerBuilder $builder
157
     *
158
     * @return self
159
     */
160
    public function setContainerBuilder(ContainerBuilder $builder): self
161
    {
162
        $this->containerBuilder = $builder;
163
164
        return $this;
165
    }
166
167
    /**
168
     * @param DumperInterface $dumper
169
     *
170
     * @return Builder
171
     */
172
    public function setDumper(DumperInterface $dumper): self
173
    {
174
        $this->dumper = $dumper;
175
176
        return $this;
177
    }
178
179
    /**
180
     * @param ContainerBuilder $builder
181
     *
182
     * @throws \Exception
183
     */
184
    private function configureCoreServices(ContainerBuilder $builder): void
185
    {
186
        $locator = new FileLocator(__DIR__.'/../Resources/config');
187
        $loader = new YamlFileLoader($builder, $locator);
188
        $loader->load('services.yaml');
189
    }
190
}
191