Issues (142)

ContainerFactory.php (2 issues)

1
<?php
2
3
/*
4
 * This file is part of the doyo/code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <[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
declare(strict_types=1);
13
14
namespace Doyo\Bridge\CodeCoverage;
15
16
use Doyo\Bridge\CodeCoverage\Compiler\CoveragePass;
17
use Doyo\Bridge\CodeCoverage\Compiler\ReportPass;
18
use Doyo\Bridge\CodeCoverage\DependencyInjection\CodeCoverageExtension;
19
use Symfony\Component\Config\ConfigCache;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
23
24
class ContainerFactory
25
{
26
    private $builder;
0 ignored issues
show
The private property $builder is not used, and could be removed.
Loading history...
27
28
    private $configCache;
0 ignored issues
show
The private property $configCache is not used, and could be removed.
Loading history...
29
30
    private $id;
31
32
    private $class;
33
34
    private $config;
35
36
    /**
37
     * @var bool
38
     */
39
    private $debug;
40
41
    public function __construct(array $config = [], bool $debug = false)
42
    {
43
        $id    = md5(serialize($config));
44
        $class = 'CodeCoverageContainer'.$id;
45
46
        $this->id     = $id;
47
        $this->class  = $class;
48
        $this->config = $config;
49
        $this->debug  = $debug;
50
    }
51
52
    /**
53
     * @return ContainerInterface
54
     */
55
    public function getContainer(): ContainerInterface
56
    {
57
        $id    = $this->id;
58
        $class = $this->class;
59
        $file  = sys_get_temp_dir().'/doyo/coverage/'.$id.'.php';
60
        //$config = ['config' => $this->config];
61
        $config = $this->config;
62
63
        $cachedContainer = new ConfigCache($file, $this->debug);
64
        if (!$cachedContainer->isFresh() || $this->debug) {
65
            //$this->dumpConfig();
66
            $builder = new ContainerBuilder();
67
68
            $builder->registerExtension(new CodeCoverageExtension());
69
            $builder->loadFromExtension('coverage', $config);
70
71
            $builder->addCompilerPass(new CoveragePass());
72
            $builder->addCompilerPass(new ReportPass());
73
            $builder->compile(true);
74
75
            $dumper = new PhpDumper($builder);
76
            $cachedContainer->write(
77
                $dumper->dump([
78
                    'class' => $class,
79
                ]),
80
                $builder->getResources()
81
            );
82
        }
83
84
        require_once $file;
85
86
        return new $class();
87
    }
88
}
89