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