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