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