1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
17
|
|
|
use Tvi\MonitorBundle\DependencyInjection\DiTags; |
18
|
|
|
|
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* @author Vladimir Turnaev <[email protected]> |
21
|
|
|
*/ |
|
|
|
|
22
|
|
|
class AddCheckPluginsCompilerPass implements CompilerPassInterface |
23
|
|
|
{ |
24
|
|
|
const SERVICE_ID_FORMAT = 'tvi_monitor.check.%s'; |
25
|
|
|
|
26
|
|
|
/** |
|
|
|
|
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $checkServiceConfs = []; |
30
|
|
|
|
31
|
50 |
|
public function process(ContainerBuilder $container) |
|
|
|
|
32
|
|
|
{ |
33
|
50 |
|
$this->processChecks($container); |
34
|
50 |
|
} |
35
|
|
|
|
36
|
50 |
|
protected function processChecks(ContainerBuilder $container) |
|
|
|
|
37
|
|
|
{ |
38
|
50 |
|
$checkConfigs = $container->getParameter('tvi_monitor.conf.checks'); |
39
|
|
|
|
40
|
50 |
|
$container->setParameter('tvi_monitor.checks.conf', null); |
41
|
|
|
|
42
|
50 |
|
$checkServiceIds = $container->findTaggedServiceIds(DiTags::CHECK_PLUGIN); |
43
|
|
|
|
44
|
50 |
|
foreach ($checkServiceIds as $checkServiceId => $null) { |
45
|
42 |
|
$checkDefinitionTpl = $container->getDefinition($checkServiceId); |
46
|
|
|
|
47
|
42 |
|
$container->removeDefinition($checkServiceId); |
48
|
|
|
|
49
|
42 |
|
$checkTag = $checkDefinitionTpl->getTag(DiTags::CHECK_PLUGIN); |
50
|
42 |
|
$checkPluginAlias = $checkTag[0]['alias']; |
51
|
|
|
|
52
|
42 |
|
$checkConfig = $checkConfigs[$checkPluginAlias]; |
53
|
|
|
|
54
|
|
|
|
55
|
42 |
|
if (isset($checkConfig['_singl'])) { |
56
|
38 |
|
$this->addCheckPlugin($container, $checkDefinitionTpl, $checkConfig['_singl'], $checkPluginAlias); |
57
|
|
|
} |
58
|
|
|
|
59
|
42 |
|
if (isset($checkConfig['_multi'])) { |
60
|
40 |
|
foreach ($checkConfig['_multi'] as $pref => $conf) { |
61
|
42 |
|
$this->addCheckPlugin($container, $checkDefinitionTpl, $conf, $checkPluginAlias, $pref); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
50 |
|
$registryDefinition = $container->getDefinition('tvi_monitor.checks.manager'); |
67
|
|
|
|
68
|
50 |
|
$tagConfs = $container->getParameter('tvi_monitor.conf.tags'); |
69
|
50 |
|
$container->setParameter('tvi_monitor.tags', null); |
70
|
|
|
|
71
|
50 |
|
$groupConfs = $container->getParameter('tvi_monitor.conf.groups'); |
72
|
50 |
|
$container->setParameter('tvi_monitor.conf.groups', null); |
73
|
|
|
|
74
|
50 |
|
$registryDefinition->addMethodCall('add', [$this->checkServiceConfs, $tagConfs, $groupConfs]); |
75
|
50 |
|
} |
76
|
|
|
|
77
|
42 |
|
protected function addCheckPlugin(ContainerBuilder $container, Definition $checkPluginDefinitionTpl, array $conf, string $checkPluginAlias, string $checkPluginPref = null) |
|
|
|
|
78
|
|
|
{ |
79
|
42 |
|
$checkPluginDefinition = clone $checkPluginDefinitionTpl; |
80
|
|
|
|
81
|
42 |
|
if (null !== $checkPluginPref) { |
82
|
40 |
|
$checkPluginAlias .= '.'.$checkPluginPref; |
83
|
|
|
} |
84
|
|
|
|
85
|
42 |
|
if (isset($conf['importance'])) { |
86
|
1 |
|
$conf['tags'][] = $conf['importance']; |
87
|
|
|
} |
88
|
|
|
|
89
|
42 |
|
foreach ($checkPluginDefinition->getArguments() as $argumentIndex => $argument) { |
90
|
33 |
|
$argument = str_replace('%%', '', $argument); |
91
|
|
|
|
92
|
33 |
|
if (array_key_exists($argument, $conf['check'])) { |
93
|
33 |
|
$checkPluginDefinition->replaceArgument($argumentIndex, $conf['check'][$argument]); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
42 |
|
$methodCalls = $checkPluginDefinition->getMethodCalls(); |
98
|
|
|
|
99
|
42 |
|
foreach ($methodCalls as &$methodCall) { |
100
|
42 |
|
if ('setAdditionParams' === $methodCall[0]) { |
101
|
42 |
|
$conf['id'] = $checkPluginAlias; |
102
|
42 |
|
$methodCall[1][0] = $conf; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
42 |
|
$checkPluginDefinition->setMethodCalls($methodCalls); |
107
|
|
|
|
108
|
42 |
|
$checkServiceId = sprintf(self::SERVICE_ID_FORMAT, sha1($checkPluginAlias)); |
109
|
|
|
|
110
|
42 |
|
$container->setDefinition($checkServiceId, $checkPluginDefinition); |
111
|
|
|
|
112
|
42 |
|
$this->checkServiceConfs[$checkPluginAlias] = ['serviceId' => $checkServiceId, 'group' => $conf['group'], 'tags' => $conf['tags']]; |
113
|
42 |
|
} |
114
|
|
|
} |
115
|
|
|
|