Builder::configureCoreServices()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

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 3
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 Dotfiles\Core\Util\Toolkit;
20
use Symfony\Component\Config\ConfigCache;
21
use Symfony\Component\Config\FileLocator;
22
use Symfony\Component\DependencyInjection\Container;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24
use Symfony\Component\DependencyInjection\Dumper\DumperInterface;
25
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
26
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
27
28
class Builder
29
{
30
    /**
31
     * @var Config
32
     */
33
    private $config;
34
35
    /**
36
     * @var ContainerInterface
37
     */
38
    private $container;
39
40
    /**
41
     * @var ContainerBuilder
42
     */
43
    private $containerBuilder;
44
45
    /**
46
     * @var DumperInterface
47
     */
48
    private $dumper;
49
50
    public function __construct(Config $config)
51
    {
52
        $this->config = $config;
53
    }
54
55
    /**
56
     * @throws \Exception
57
     */
58
    public function compile(): void
59
    {
60
        $cachePath = $this->getCacheFileName();
61
        $cache = new ConfigCache($cachePath, true);
62
        $env = getenv('DOTFILES_ENV');
63
64
        if (!$cache->isFresh() || 'dev' === $env) {
65
            $builder = $this->getContainerBuilder();
66
            $this->configureCoreServices($builder);
67
            $builder->addCompilerPass(new CommandPass());
68
            $builder->addCompilerPass(new ListenerPass());
69
            $builder->compile();
70
71
            $dumper = $this->getDumper();
72
            //file_put_contents($target,$dumper->dump(['class'=>'CachedContainer']), LOCK_EX);
73
            $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

73
            $cache->write(/** @scrutinizer ignore-type */ $dumper->dump(array('class' => 'CachedContainer')), $builder->getResources());
Loading history...
74
        }
75
    }
76
77
    public function getCacheFileName()
78
    {
79
        return Toolkit::getCachePathPrefix().'/container.php';
80
    }
81
82
    /**
83
     * @return Container
84
     */
85
    public function getContainer()
86
    {
87
        if (null === $this->container) {
88
            if (!class_exists('CachedContainer')) {
89
                include_once $this->getCacheFileName();
90
            }
91
            $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...
92
        }
93
94
        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...
95
    }
96
97
    /**
98
     * @return ContainerBuilder
99
     */
100
    public function getContainerBuilder(): ContainerBuilder
101
    {
102
        if (null === $this->containerBuilder) {
103
            $this->containerBuilder = new ContainerBuilder();
104
        }
105
106
        return $this->containerBuilder;
107
    }
108
109
    /**
110
     * @return DumperInterface
111
     */
112
    public function getDumper(): DumperInterface
113
    {
114
        if (null === $this->dumper) {
115
            $this->dumper = new PhpDumper($this->containerBuilder);
116
        }
117
118
        return $this->dumper;
119
    }
120
121
    /**
122
     * @param Config $config
123
     *
124
     * @return Builder
125
     */
126
    public function setConfig(Config $config): self
127
    {
128
        $this->config = $config;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param ContainerBuilder $builder
135
     *
136
     * @return self
137
     */
138
    public function setContainerBuilder(ContainerBuilder $builder): self
139
    {
140
        $this->containerBuilder = $builder;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @param DumperInterface $dumper
147
     *
148
     * @return Builder
149
     */
150
    public function setDumper(DumperInterface $dumper): self
151
    {
152
        $this->dumper = $dumper;
153
154
        return $this;
155
    }
156
157
    /**
158
     * @param ContainerBuilder $builder
159
     *
160
     * @throws \Exception
161
     */
162
    private function configureCoreServices(ContainerBuilder $builder): void
163
    {
164
        $locator = new FileLocator(__DIR__.'/../Resources/config');
165
        $loader = new YamlFileLoader($builder, $locator);
166
        $loader->load('services.yaml');
167
    }
168
}
169