1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the doyo/behat-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\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 ReportPass implements CompilerPassInterface |
22
|
|
|
{ |
23
|
4 |
|
public function process(ContainerBuilder $container) |
24
|
|
|
{ |
25
|
4 |
|
$services = $container->findTaggedServiceIds('doyo.coverage.report'); |
26
|
|
|
|
27
|
|
|
// configure processors |
28
|
4 |
|
foreach ($container->findTaggedServiceIds('doyo.coverage.report.processor') as $id => $tagArguments) { |
29
|
4 |
|
$this->configureReportProcessor($container, $id, $tagArguments); |
30
|
|
|
} |
31
|
|
|
// configure report |
32
|
4 |
|
foreach ($services as $id => $tagArguments) { |
33
|
4 |
|
$this->configureReport($container, $id, $tagArguments); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
private function configureReportProcessor(ContainerBuilder $container, $id, array $tagArguments) |
38
|
|
|
{ |
39
|
4 |
|
$definition = $container->getDefinition($id); |
40
|
4 |
|
$format = $tagArguments[0]['format']; |
41
|
4 |
|
$options = $container->getParameterBag()->get('doyo.coverage.report.'.$format); |
42
|
4 |
|
$class = $container->getParameterBag()->get($id.'.class'); |
43
|
4 |
|
$hasOptions = ['html', 'text', 'crap4j']; |
44
|
|
|
|
45
|
4 |
|
unset($options['target']); |
46
|
4 |
|
$definition->setClass($class); |
47
|
4 |
|
if (!empty($options) && \in_array($format, $hasOptions, true)) { |
48
|
4 |
|
$this->configureReportProcessorOptions($definition, $class, $options); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
private function configureReportProcessorOptions(Definition $definition, $class, $options) |
53
|
|
|
{ |
54
|
4 |
|
$r = new \ReflectionClass($class); |
55
|
4 |
|
$constructor = $r->getConstructor(); |
56
|
4 |
|
$parameters = []; |
57
|
|
|
|
58
|
4 |
|
foreach ($constructor->getParameters() as $reflectionParameter) { |
59
|
4 |
|
$paramName = $reflectionParameter->getName(); |
60
|
4 |
|
if (!$reflectionParameter->isDefaultValueAvailable()) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
4 |
|
$value = $reflectionParameter->getDefaultValue(); |
64
|
4 |
|
$position = $reflectionParameter->getPosition(); |
65
|
4 |
|
if (isset($options[$paramName])) { |
66
|
4 |
|
$value = $options[$paramName]; |
67
|
|
|
} |
68
|
4 |
|
$parameters[$position] = $value; |
69
|
|
|
} |
70
|
4 |
|
$definition->setArguments($parameters); |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
private function configureReport(ContainerBuilder $container, $id, array $tagArguments) |
74
|
|
|
{ |
75
|
4 |
|
$definition = $container->getDefinition($id); |
76
|
4 |
|
$format = $tagArguments[0]['format']; |
77
|
4 |
|
$type = $tagArguments[0]['type']; |
78
|
4 |
|
$config = $container->getParameterBag()->get('doyo.coverage.report.'.$format); |
79
|
4 |
|
$class = $container->getParameterBag()->get('doyo.coverage.report.class'); |
80
|
4 |
|
$basePath = $container->getParameterBag()->get('paths.base'); |
81
|
4 |
|
$dispatcher = $container->getDefinition('doyo.coverage.dispatcher'); |
82
|
|
|
|
83
|
4 |
|
$definition->setClass($class); |
84
|
|
|
|
85
|
4 |
|
if (isset($config['target'])) { |
86
|
4 |
|
$target = $basePath.'/'.$config['target']; |
87
|
4 |
|
$definition->addMethodCall('setTarget', [$target]); |
88
|
4 |
|
$definition->addMethodCall('setReportProcessor', [new Reference($id.'.processor')]); |
89
|
4 |
|
$definition->addMethodCall('setName', [$format]); |
90
|
4 |
|
$dispatcher->addMethodCall('addSubscriber', [new Reference($id)]); |
91
|
4 |
|
$this->ensureDir($type, $target); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
private function ensureDir($type, $target) |
96
|
|
|
{ |
97
|
4 |
|
$dir = $target; |
98
|
4 |
|
if ('file' === $type) { |
99
|
4 |
|
$dir = \dirname($target); |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
if (!is_dir($dir)) { |
103
|
1 |
|
mkdir($dir, 0775, true); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|