|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Doyo\Bridge\CodeCoverage\Compiler; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
10
|
|
|
|
|
11
|
|
|
class CoveragePass implements CompilerPassInterface |
|
12
|
|
|
{ |
|
13
|
15 |
|
public function process(ContainerBuilder $container) |
|
14
|
|
|
{ |
|
15
|
15 |
|
$this->processDriver($container); |
|
16
|
15 |
|
$this->processFilter($container); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
15 |
|
private function processDriver(ContainerBuilder $container) |
|
20
|
|
|
{ |
|
21
|
15 |
|
$driverClass = $container->get('runtime')->getDriverClass(); |
|
22
|
15 |
|
$container->getParameterBag()->set('coverage.driver.class', $driverClass); |
|
23
|
|
|
|
|
24
|
15 |
|
$r = new \ReflectionClass($driverClass); |
|
25
|
15 |
|
if($r->hasMethod('setFilter')){ |
|
26
|
|
|
$definition = $container->getDefinition('coverage.filter'); |
|
27
|
|
|
$definition->addMethodCall('setFilter',[new Reference('coverage.filter')]); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
15 |
|
private function processFilter(ContainerBuilder $container) |
|
32
|
|
|
{ |
|
33
|
15 |
|
$config = $container->getParameter('config.filter'); |
|
34
|
15 |
|
$definition = $container->getDefinition('coverage.filter'); |
|
35
|
|
|
|
|
36
|
15 |
|
foreach ($config as $options) { |
|
37
|
1 |
|
$options['basePath'] = ""; |
|
38
|
1 |
|
$this->filterWhitelist($definition, $options, 'add'); |
|
39
|
1 |
|
$exclude = $options['exclude']; |
|
40
|
1 |
|
foreach ($exclude as $item) { |
|
41
|
|
|
$item['basePath'] = ""; |
|
42
|
|
|
$this->filterWhitelist($definition, $item, 'remove'); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
private function filterWhitelist(Definition $definition, $options, $method) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$basePath = $options['basePath']; |
|
|
|
|
|
|
50
|
1 |
|
$suffix = $options['suffix'] ?: '.php'; |
|
51
|
1 |
|
$prefix = $options['prefix'] ?: ''; |
|
52
|
1 |
|
$type = $options['directory'] ? 'directory' : 'file'; |
|
53
|
1 |
|
$directory = $options['directory']; |
|
54
|
1 |
|
$file = $options['file']; |
|
55
|
|
|
|
|
56
|
1 |
|
if (preg_match('/\/\*(\..+)/', $directory, $matches)) { |
|
57
|
|
|
$suffix = $matches[1]; |
|
58
|
|
|
$directory = str_replace($matches[0], '', $directory); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
$methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist'; |
|
62
|
1 |
|
if ('directory' === $type) { |
|
63
|
1 |
|
$definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
if ('file' === $type) { |
|
67
|
|
|
$definition->addMethodCall($method.'File'.$methodSuffix, [$file]); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|