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\Compiler; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
class CoveragePass implements CompilerPassInterface |
22
|
|
|
{ |
23
|
21 |
|
public function process(ContainerBuilder $container) |
24
|
|
|
{ |
25
|
21 |
|
$this->processDriver($container); |
26
|
21 |
|
$this->processFilter($container); |
27
|
|
|
} |
28
|
|
|
|
29
|
21 |
|
private function processDriver(ContainerBuilder $container) |
30
|
|
|
{ |
31
|
21 |
|
$driverClass = $container->get('runtime')->getDriverClass(); |
32
|
21 |
|
$container->getParameterBag()->set('coverage.driver.class', $driverClass); |
33
|
|
|
|
34
|
21 |
|
$r = new \ReflectionClass($driverClass); |
35
|
21 |
|
if ($r->hasMethod('setFilter')) { |
36
|
|
|
$definition = $container->getDefinition('coverage.filter'); |
37
|
|
|
$definition->addMethodCall('setFilter', [new Reference('coverage.filter')]); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
21 |
|
private function processFilter(ContainerBuilder $container) |
42
|
|
|
{ |
43
|
21 |
|
$config = $container->getParameter('config.filter'); |
44
|
21 |
|
$definition = $container->getDefinition('coverage.filter'); |
45
|
|
|
|
46
|
21 |
|
foreach ($config as $options) { |
47
|
2 |
|
$options['basePath'] = ''; |
48
|
2 |
|
$this->filterWhitelist($definition, $options, 'add'); |
49
|
2 |
|
$exclude = $options['exclude']; |
50
|
2 |
|
foreach ($exclude as $item) { |
51
|
|
|
$item['basePath'] = ''; |
52
|
|
|
$this->filterWhitelist($definition, $item, 'remove'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
private function filterWhitelist(Definition $definition, $options, $method) |
58
|
|
|
{ |
59
|
2 |
|
$suffix = $options['suffix'] ?: '.php'; |
60
|
2 |
|
$prefix = $options['prefix'] ?: ''; |
61
|
2 |
|
$directory = $options['directory']; |
62
|
2 |
|
$file = $options['file']; |
63
|
2 |
|
$type = !is_null($options['directory']) ? 'directory' : 'file'; |
64
|
|
|
|
65
|
2 |
|
if(!is_null($directory) && "" === $directory){ |
66
|
|
|
$directory = getcwd(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( |
70
|
2 |
|
!is_null($directory) |
71
|
2 |
|
&& preg_match('/\/\*(\..+)/', $directory, $matches) |
72
|
|
|
) { |
73
|
|
|
$suffix = $matches[1]; |
74
|
|
|
$directory = str_replace($matches[0], '', $directory); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
$methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist'; |
78
|
2 |
|
if ('directory' === $type) { |
79
|
2 |
|
$definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
if ('file' === $type) { |
83
|
|
|
|
84
|
|
|
$files = [$file]; |
85
|
|
|
|
86
|
|
|
if(false !== ($pos=strpos($file, '*'))){ |
|
|
|
|
87
|
|
|
$files = []; |
88
|
|
|
foreach(glob($file) as $filename){ |
89
|
|
|
$files[] = $filename; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$definition->addMethodCall($method.'File'.$methodSuffix, $files); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|