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; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\FileLocator; |
15
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
18
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author Vladimir Turnaev <[email protected]> |
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
class TviMonitorExtension extends Extension implements CompilerPassInterface |
24
|
|
|
{ |
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @var Configuration |
27
|
|
|
*/ |
28
|
|
|
private $configuration; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
|
|
|
|
31
|
|
|
* Loads the services based on your application configuration. |
32
|
|
|
*/ |
|
|
|
|
33
|
56 |
|
public function load(array $configs, ContainerBuilder $container) |
34
|
|
|
{ |
35
|
|
|
/* |
36
|
|
|
dump($configs); exit; |
37
|
|
|
//*/ |
38
|
|
|
|
39
|
56 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
40
|
|
|
|
41
|
56 |
|
$loader->load('service.yml'); |
42
|
56 |
|
$loader->load('command.yml'); |
43
|
|
|
|
44
|
56 |
|
$checksSearchPaths = []; |
45
|
56 |
|
if (isset($configs[1]['checks_search_paths'])) { |
46
|
9 |
|
$checksSearchPaths = $configs[1]['checks_search_paths'] ?? []; |
47
|
47 |
|
} elseif (isset($configs[0]['checks_search_paths'])) { |
48
|
1 |
|
$checksSearchPaths = $configs[0]['checks_search_paths'] ?? []; |
49
|
|
|
} |
50
|
|
|
|
51
|
56 |
|
$checkPluginFinderDefinition = $container->getDefinition('tvi_monitor.checks.plugin_finder'); |
52
|
56 |
|
$checkPluginFinderDefinition->setArguments([$checksSearchPaths]); |
53
|
|
|
|
54
|
56 |
|
$pluginFinder = $container->get('tvi_monitor.checks.plugin_finder'); |
55
|
|
|
|
56
|
56 |
|
$configuration = new Configuration($pluginFinder); |
57
|
|
|
|
58
|
56 |
|
$config = $this->processConfiguration($configuration, $configs); |
59
|
|
|
|
60
|
|
|
/* |
61
|
|
|
dump($config); exit; |
62
|
|
|
//*/ |
63
|
|
|
|
64
|
50 |
|
$this->configureUIViewTemplate($config, $container); |
65
|
50 |
|
$this->configureTags($config, $container); |
66
|
50 |
|
$this->configureGroups($config, $container); |
67
|
50 |
|
$this->configureReporters($config, $container, $loader); |
68
|
|
|
|
69
|
50 |
|
$this->configureChecks($config, $container, $loader, $configuration->getCheckPlugins()); |
70
|
|
|
|
71
|
50 |
|
$loader->load('controller.yml'); |
72
|
50 |
|
$loader->load('generator.yml'); |
73
|
|
|
|
74
|
50 |
|
$this->configuration = $configuration; |
75
|
50 |
|
} |
76
|
|
|
|
77
|
1 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
|
|
|
|
78
|
|
|
{ |
79
|
1 |
|
return $this->configuration; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
|
|
|
|
85
|
49 |
|
public function process(ContainerBuilder $container) |
86
|
|
|
{ |
87
|
49 |
|
} |
88
|
|
|
|
89
|
50 |
|
private function configureUIViewTemplate(array $config, ContainerBuilder $container) |
|
|
|
|
90
|
|
|
{ |
91
|
50 |
|
$container->setParameter(sprintf('%s.ui.view.template', $this->getAlias()), $config['ui_view_template']); |
92
|
50 |
|
} |
93
|
|
|
|
94
|
50 |
|
private function configureTags(array $config, ContainerBuilder $container) |
|
|
|
|
95
|
|
|
{ |
96
|
50 |
|
$container->setParameter(sprintf('%s.conf.tags', $this->getAlias()), $config['tags']); |
97
|
50 |
|
} |
98
|
|
|
|
99
|
50 |
|
private function configureGroups(array $config, ContainerBuilder $container) |
|
|
|
|
100
|
|
|
{ |
101
|
50 |
|
$container->setParameter(sprintf('%s.conf.groups', $this->getAlias()), $config['groups']); |
102
|
50 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
|
|
|
|
105
|
|
|
* @param string[] $checkPlugins |
|
|
|
|
106
|
|
|
* |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
|
|
|
|
109
|
50 |
|
private function configureChecks(array $config, ContainerBuilder $container, YamlFileLoader $loader, array $checkPlugins) |
|
|
|
|
110
|
|
|
{ |
111
|
50 |
|
$containerParams = []; |
112
|
|
|
|
113
|
50 |
|
if (isset($config['checks'])) { |
114
|
41 |
|
$config['checks'] = array_filter($config['checks'], static function ($i) { |
|
|
|
|
115
|
41 |
|
return $i; |
116
|
41 |
|
}); |
|
|
|
|
117
|
|
|
|
118
|
41 |
|
$containerParams = []; |
119
|
41 |
|
$checksLoaded = []; |
120
|
|
|
|
121
|
41 |
|
foreach ($config['checks'] as $checkName => &$checkSettings) { |
122
|
41 |
|
$checkPlugin = $checkPlugins[$checkName]; |
123
|
41 |
|
$service = $checkPlugin['service']; |
124
|
|
|
|
125
|
41 |
|
$checkServicePath = $checkPlugin['checkServicePath']; |
126
|
|
|
|
127
|
41 |
|
if (!\in_array($checkServicePath, $checksLoaded, true)) { |
128
|
41 |
|
$checksLoaded[] = $checkServicePath; |
129
|
|
|
|
130
|
41 |
|
$loader->load($checkServicePath); |
131
|
41 |
|
$checkPlugin['pligin']->checkRequirements($checkSettings); |
132
|
|
|
} |
133
|
|
|
|
134
|
41 |
|
if (isset($checkSettings['items'])) { |
135
|
39 |
|
$items = $checkSettings['items']; |
136
|
|
|
|
137
|
39 |
|
foreach ($items as $itemName => &$item) { |
138
|
39 |
|
$item['tags'] = array_unique(array_merge($item['tags'], $checkSettings['tags'])); |
139
|
|
|
|
140
|
39 |
|
if (null === $item['label'] && null !== $checkSettings['label']) { |
141
|
1 |
|
$val = $checkSettings['label']; |
142
|
1 |
|
$val = sprintf($val, $itemName); |
143
|
1 |
|
$item['label'] = $val; |
144
|
|
|
} |
145
|
|
|
|
146
|
39 |
|
if (null === $item['descr'] && null !== $checkSettings['descr']) { |
147
|
|
|
$val = $checkSettings['descr']; |
148
|
|
|
$val = sprintf($val, $itemName); |
149
|
|
|
$item['descr'] = $val; |
150
|
|
|
} |
151
|
|
|
|
152
|
39 |
|
if (empty($item['group']) && !empty($checkSettings['group'])) { |
153
|
2 |
|
$val = $checkSettings['group']; |
154
|
2 |
|
$item['group'] = $val; |
155
|
|
|
} |
156
|
|
|
|
157
|
39 |
|
if (empty($item['importance']) && !empty($checkSettings['importance'])) { |
158
|
1 |
|
$val = $checkSettings['importance']; |
159
|
1 |
|
$item['importance'] = $val; |
160
|
|
|
} |
161
|
|
|
|
162
|
39 |
|
if (empty($item['group']) && empty($checkSettings['group'])) { |
163
|
37 |
|
$item['group'] = $item['_group']; |
164
|
|
|
} |
165
|
|
|
|
166
|
39 |
|
unset($item['_group']); |
167
|
|
|
} |
168
|
|
|
|
169
|
39 |
|
$containerParams[$service]['_multi'] = $items; |
170
|
|
|
} else { |
171
|
39 |
|
if (empty($checkSettings['group'])) { |
172
|
38 |
|
$checkSettings['group'] = $checkSettings['_group']; |
173
|
|
|
} |
174
|
|
|
|
175
|
39 |
|
unset($checkSettings['_group']); |
176
|
41 |
|
$containerParams[$service]['_singl'] = $checkSettings; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
50 |
|
$container->setParameter(sprintf('%s.conf.checks', $this->getAlias()), $containerParams); |
182
|
50 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
|
|
|
|
185
|
|
|
* @throws \Exception |
186
|
|
|
*/ |
|
|
|
|
187
|
50 |
|
private function configureReporters(array $config, ContainerBuilder $container, YamlFileLoader $loader) |
|
|
|
|
188
|
|
|
{ |
189
|
50 |
|
$loader->load('reporter/reporters.yml'); |
190
|
|
|
|
191
|
50 |
|
if (isset($config['reporters']['mailer'])) { |
192
|
2 |
|
$loader->load('reporter/mailer.yml'); |
193
|
|
|
|
194
|
2 |
|
foreach ($config['reporters']['mailer'] as $key => $value) { |
195
|
2 |
|
$container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value); |
196
|
|
|
} |
197
|
|
|
} |
198
|
50 |
|
} |
199
|
|
|
} |
200
|
|
|
|