1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Liip\MonitorBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
|
8
|
|
|
class AddGroupsCompilerPass implements CompilerPassInterface |
9
|
|
|
{ |
10
|
|
|
const SERVICE_ID_PREFIX = 'liip_monitor.check.'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param ContainerBuilder $container |
14
|
|
|
*/ |
15
|
|
|
public function process(ContainerBuilder $container) |
16
|
|
|
{ |
17
|
|
|
if (!$container->hasParameter('liip_monitor.checks')) { |
18
|
|
|
return; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$checkConfig = $container->getParameter('liip_monitor.checks'); |
22
|
|
|
|
23
|
|
|
list($checks, $checkCollections) = $this->parseGroups($container, $checkConfig['groups']); |
24
|
|
|
|
25
|
|
|
$this->addGroupTags($container, $checks, 'liip_monitor.check'); |
26
|
|
|
$this->addGroupTags($container, $checkCollections, 'liip_monitor.check_collection'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param ContainerBuilder $container |
31
|
|
|
* @param array $data |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
private function parseGroups(ContainerBuilder $container, array $data) |
36
|
|
|
{ |
37
|
|
|
$checks = array(); |
38
|
|
|
$checkCollections = array(); |
39
|
|
|
|
40
|
|
|
foreach ($data as $group => $groupChecks) { |
41
|
|
|
foreach (array_keys($groupChecks) as $checkName) { |
42
|
|
|
$serviceId = self::SERVICE_ID_PREFIX.$checkName; |
43
|
|
|
$checkDefinition = $container->getDefinition($serviceId); |
44
|
|
|
|
45
|
|
|
if ($checkDefinition->hasTag('liip_monitor.check')) { |
46
|
|
|
$checks[$checkName][] = $group; |
47
|
|
|
} elseif ($checkDefinition->hasTag('liip_monitor.check_collection')) { |
48
|
|
|
$checkCollections[$checkName][] = $group; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return array($checks, $checkCollections); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* This Method completes the service definitions of each check for a configured group. |
58
|
|
|
* |
59
|
|
|
* For every configured check (per group) a parameter has been generated in LiipMonitorExtension::setParameters. |
60
|
|
|
* So the finally generated parameters have to be injected into each check service definition. |
61
|
|
|
* (see the preg_match part). |
62
|
|
|
* |
63
|
|
|
* @param ContainerBuilder $container |
64
|
|
|
* @param array $checks |
65
|
|
|
* @param string $tag |
66
|
|
|
*/ |
67
|
|
|
private function addGroupTags(ContainerBuilder $container, array $checks, $tag) |
68
|
|
|
{ |
69
|
|
|
foreach ($checks as $checkName => $groups) { |
70
|
|
|
$serviceId = self::SERVICE_ID_PREFIX.$checkName; |
71
|
|
|
$serviceDefinition = $container->getDefinition($serviceId); |
72
|
|
|
$serviceDefinition->clearTag($tag); |
73
|
|
|
|
74
|
|
|
foreach ($groups as $group) { |
75
|
|
|
$tmpDefinition = clone $serviceDefinition; |
76
|
|
|
$tmpDefinition->addTag($tag, array('group' => $group, 'alias' => $checkName)); |
77
|
|
|
|
78
|
|
|
foreach ($tmpDefinition->getArguments() as $argumentIndex => $argument) { |
79
|
|
|
if (is_string($argument) && preg_match('/^%%(.*)%%$/', $argument, $matches)) { |
80
|
|
|
$newArgument = $container->getParameter($matches[1].'.'.$group); |
81
|
|
|
$tmpDefinition->replaceArgument($argumentIndex, $newArgument); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$container->setDefinition($serviceId.'.'.$group, $tmpDefinition); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$container->removeDefinition($serviceId); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|