1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the doyo/code-coverage project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <https://itstoni.com> |
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\Console\Application; |
19
|
|
|
use Doyo\Bridge\CodeCoverage\DependencyInjection\CodeCoverageExtension; |
20
|
|
|
use Symfony\Component\Config\ConfigCache; |
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
23
|
|
|
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
24
|
|
|
use Symfony\Component\Yaml\Yaml; |
25
|
|
|
|
26
|
|
|
class ContainerFactory |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ContainerInterface |
35
|
|
|
*/ |
36
|
|
|
private $container; |
37
|
|
|
|
38
|
37 |
|
public function __construct(array $config = []) |
39
|
|
|
{ |
40
|
37 |
|
$this->config = $config; |
41
|
37 |
|
$this->doCreateContainer(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return ContainerInterface |
46
|
|
|
*/ |
47
|
33 |
|
public function getContainer(): ContainerInterface |
48
|
|
|
{ |
49
|
33 |
|
return $this->container; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param bool $useDummyDriver |
54
|
|
|
* |
55
|
|
|
* @return ProcessorInterface |
56
|
|
|
*/ |
57
|
17 |
|
public function createProcessor(bool $useDummyDriver = false): ProcessorInterface |
58
|
|
|
{ |
59
|
17 |
|
$coverage = $this->createCodeCoverage($useDummyDriver); |
60
|
|
|
|
61
|
17 |
|
return new Processor($coverage); |
62
|
|
|
} |
63
|
|
|
|
64
|
18 |
|
public function createCodeCoverage(bool $useDummyDriver = false) |
65
|
|
|
{ |
66
|
18 |
|
$container = $this->container; |
67
|
18 |
|
$driverClass = $container->getParameter('coverage.driver.class'); |
68
|
|
|
|
69
|
18 |
|
if ($useDummyDriver) { |
70
|
11 |
|
$driverClass = $container->getParameter('coverage.driver.dummy.class'); |
71
|
|
|
} |
72
|
|
|
|
73
|
18 |
|
$driver = new $driverClass(); |
74
|
18 |
|
$filter = $container->get('coverage.filter'); |
75
|
|
|
|
76
|
18 |
|
return new \SebastianBergmann\CodeCoverage\CodeCoverage($driver, $filter); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public function createApplication($version = 'dev') |
80
|
|
|
{ |
81
|
1 |
|
return new Application('code-coverage', $version); |
82
|
|
|
} |
83
|
|
|
|
84
|
37 |
|
private function doCreateContainer() |
85
|
|
|
{ |
86
|
37 |
|
$config = $this->config; |
87
|
37 |
|
$configs = []; |
88
|
|
|
|
89
|
37 |
|
if (isset($config['imports'])) { |
90
|
6 |
|
$configs = $this->normalizeConfig($config); |
91
|
6 |
|
unset($config['imports']); |
92
|
|
|
} |
93
|
|
|
|
94
|
37 |
|
$configs[] = $config; |
95
|
|
|
|
96
|
37 |
|
$debug = false; |
97
|
37 |
|
foreach ($configs as $config) { |
98
|
37 |
|
if (isset($config['debug'])) { |
99
|
18 |
|
$debug = $config['debug']; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
37 |
|
$id = md5(serialize($configs)); |
104
|
37 |
|
$file = sys_get_temp_dir().'/doyo/coverage/container'.$id.'.php'; |
105
|
37 |
|
$class = 'CodeCoverageContainer'.$id; |
106
|
37 |
|
$cachedContainer = new ConfigCache($file, $debug); |
107
|
37 |
|
$debug = true; |
108
|
37 |
|
if (!$cachedContainer->isFresh() || $debug) { |
|
|
|
|
109
|
|
|
//$this->dumpConfig(); |
110
|
37 |
|
$builder = new ContainerBuilder(); |
111
|
|
|
|
112
|
37 |
|
$builder->registerExtension(new CodeCoverageExtension()); |
113
|
37 |
|
foreach ($configs as $config) { |
114
|
37 |
|
$builder->loadFromExtension('coverage', $config); |
115
|
|
|
} |
116
|
|
|
|
117
|
37 |
|
$builder->addCompilerPass(new CoveragePass()); |
118
|
37 |
|
$builder->addCompilerPass(new ReportPass()); |
119
|
37 |
|
$builder->compile(true); |
120
|
|
|
|
121
|
37 |
|
$dumper = new PhpDumper($builder); |
122
|
37 |
|
$cachedContainer->write( |
123
|
37 |
|
$dumper->dump([ |
|
|
|
|
124
|
37 |
|
'class' => $class, |
125
|
|
|
]), |
126
|
37 |
|
$builder->getResources() |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
37 |
|
require_once $file; |
131
|
|
|
|
132
|
|
|
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */ |
133
|
37 |
|
$container = new $class(); |
134
|
37 |
|
$container->set('factory', $this); |
135
|
|
|
|
136
|
37 |
|
$this->container = $container; |
137
|
|
|
} |
138
|
|
|
|
139
|
6 |
|
private function normalizeConfig($configuration) |
140
|
|
|
{ |
141
|
6 |
|
$configs = []; |
142
|
6 |
|
foreach ($configuration['imports'] as $file) { |
143
|
6 |
|
$configs[] = $this->importFile($file); |
144
|
|
|
} |
145
|
|
|
|
146
|
6 |
|
return $configs; |
147
|
|
|
} |
148
|
|
|
|
149
|
6 |
|
private function importFile($file) |
150
|
|
|
{ |
151
|
6 |
|
return Yaml::parseFile($file); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|