1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the doyo/behat-coverage-extension 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\Behat\Coverage\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
|
2 |
|
public function process(ContainerBuilder $container) |
24
|
|
|
{ |
25
|
2 |
|
$this->compileFilterOptions($container); |
26
|
2 |
|
$this->compileDrivers($container); |
27
|
2 |
|
$this->compileCoverageOptions($container); |
28
|
|
|
|
29
|
2 |
|
$definition = $container->getDefinition('doyo.coverage.dispatcher'); |
30
|
2 |
|
$tagged = $container->findTaggedServiceIds('doyo.dispatcher.subscriber'); |
31
|
|
|
|
32
|
2 |
|
foreach ($tagged as $id=>$arguments) { |
33
|
2 |
|
$definition->addMethodCall('addSubscriber', [new Reference($id)]); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
private function compileDrivers(ContainerBuilder $container) |
38
|
|
|
{ |
39
|
2 |
|
$drivers = $container->getParameterBag()->get('doyo.coverage.drivers'); |
40
|
2 |
|
$codeCoverageOptions = $container->getParameterBag()->get('doyo.coverage.options'); |
41
|
|
|
|
42
|
|
|
$map = [ |
43
|
2 |
|
'cached' => $container->getParameterBag()->get('doyo.coverage.cached.class'), |
44
|
|
|
]; |
45
|
|
|
|
46
|
2 |
|
foreach ($drivers as $config) { |
47
|
2 |
|
$namespace = $config['namespace']; |
48
|
2 |
|
$driver = $config['driver']; |
49
|
2 |
|
$class = $map[$driver]; |
50
|
2 |
|
$id = 'doyo.coverage.cached.'.$namespace; |
51
|
|
|
|
52
|
2 |
|
$definition = new Definition($class); |
53
|
2 |
|
$definition->addTag('doyo.dispatcher.subscriber'); |
54
|
2 |
|
$definition->addArgument($namespace); |
55
|
2 |
|
$definition->addArgument($codeCoverageOptions); |
56
|
2 |
|
$definition->addArgument(new Reference('doyo.coverage.filter')); |
57
|
2 |
|
$definition->setPublic(true); |
58
|
|
|
|
59
|
2 |
|
$container->setDefinition($id, $definition); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
private function compileCoverageOptions(ContainerBuilder $container) |
64
|
|
|
{ |
65
|
2 |
|
$options = $container->getParameterBag()->get('doyo.coverage.options'); |
66
|
|
|
|
67
|
2 |
|
$definitions = $container->findTaggedServiceIds('doyo.code_coverage'); |
68
|
|
|
/* @var \Symfony\Component\DependencyInjection\Definition $definition */ |
69
|
2 |
|
foreach ($definitions as $id => $test) { |
70
|
2 |
|
$definition = $container->getDefinition($id); |
71
|
2 |
|
$this->addCoverageOption($definition, $options); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
private function addCoverageOption(Definition $definition, array $options) |
76
|
|
|
{ |
77
|
2 |
|
foreach ($options as $name => $value) { |
78
|
2 |
|
$method = 'set'.ucfirst($name); |
79
|
2 |
|
$definition->addMethodCall($method, [$value]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
private function compileFilterOptions(ContainerBuilder $container) |
84
|
|
|
{ |
85
|
2 |
|
$config = $container->getParameterBag()->get('doyo.coverage.config'); |
86
|
2 |
|
$filter = $config['filter']; |
87
|
2 |
|
$basePath = $container->getParameterBag()->get('paths.base'); |
88
|
2 |
|
$definition = $container->getDefinition('doyo.coverage.filter'); |
89
|
|
|
|
90
|
2 |
|
foreach ($filter as $options) { |
91
|
2 |
|
$options['basePath'] = $basePath; |
92
|
2 |
|
$this->filterWhitelist($definition, $options, 'add'); |
93
|
2 |
|
$exclude = $options['exclude']; |
94
|
2 |
|
foreach ($exclude as $item) { |
95
|
2 |
|
$item['basePath'] = $basePath; |
96
|
2 |
|
$this->filterWhitelist($definition, $item, 'remove'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
private function filterWhitelist(Definition $definition, $options, $method) |
102
|
|
|
{ |
103
|
2 |
|
$basePath = $options['basePath']; |
104
|
2 |
|
$suffix = $options['suffix'] ?: '.php'; |
105
|
2 |
|
$prefix = $options['prefix'] ?: ''; |
106
|
2 |
|
$type = $options['directory'] ? 'directory' : 'file'; |
107
|
2 |
|
$directory = $basePath.\DIRECTORY_SEPARATOR.$options['directory']; |
108
|
2 |
|
$file = $basePath.\DIRECTORY_SEPARATOR.$options['file']; |
109
|
|
|
|
110
|
2 |
|
if (preg_match('/\/\*(\..+)/', $directory, $matches)) { |
111
|
2 |
|
$suffix = $matches[1]; |
112
|
2 |
|
$directory = str_replace($matches[0], '', $directory); |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
$methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist'; |
116
|
2 |
|
if ('directory' === $type) { |
117
|
2 |
|
$definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]); |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
if ('file' === $type) { |
121
|
2 |
|
$definition->addMethodCall($method.'File'.$methodSuffix, [$file]); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|