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\Definition\Builder\ArrayNodeDefinition; |
15
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
16
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
17
|
|
|
use Tvi\MonitorBundle\Check\CheckPluginFinder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This class contains the configuration information for the bundle. |
21
|
|
|
* |
22
|
|
|
* This information is solely responsible for how the different configuration |
23
|
|
|
* sections are normalized, and merged. |
24
|
|
|
* |
25
|
|
|
* @author Vladimir Turnaev <[email protected]> |
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class Configuration implements ConfigurationInterface |
28
|
|
|
{ |
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $checkPlugins = []; |
|
|
|
|
33
|
|
|
|
34
|
|
|
private $checkPluginClasses = []; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* Configuration constructor. |
38
|
|
|
*/ |
39
|
57 |
|
public function __construct(CheckPluginFinder $pluginFinder) |
40
|
|
|
{ |
41
|
57 |
|
$this->checkPluginClasses = $pluginFinder->find(); |
42
|
57 |
|
} |
43
|
|
|
|
44
|
51 |
|
public function getCheckPlugins(): array |
|
|
|
|
45
|
|
|
{ |
46
|
51 |
|
return $this->checkPlugins; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Generates the configuration tree. |
51
|
|
|
* |
52
|
|
|
* @return TreeBuilder |
53
|
|
|
*/ |
54
|
57 |
|
public function getConfigTreeBuilder() |
55
|
|
|
{ |
56
|
57 |
|
$treeBuilder = new TreeBuilder(); |
57
|
|
|
|
58
|
57 |
|
$treeBuilder->root('tvi_monitor', 'array') |
59
|
57 |
|
->children() |
60
|
57 |
|
->append($this->addUIViewTemplate()) |
|
|
|
|
61
|
57 |
|
->append($this->addChecksSearchPaths()) |
|
|
|
|
62
|
57 |
|
->append($this->addGroups()) |
|
|
|
|
63
|
57 |
|
->append($this->addTags()) |
|
|
|
|
64
|
57 |
|
->append($this->addReporers()) |
|
|
|
|
65
|
57 |
|
->append($this->addChecks()) |
|
|
|
|
66
|
57 |
|
->end() |
67
|
57 |
|
->end(); |
|
|
|
|
68
|
|
|
|
69
|
57 |
|
return $treeBuilder; |
70
|
|
|
} |
71
|
|
|
|
72
|
57 |
|
private function addChecks(): ArrayNodeDefinition |
|
|
|
|
73
|
|
|
{ |
74
|
57 |
|
$builder = new TreeBuilder(); |
75
|
|
|
|
76
|
57 |
|
$checkPligins = $this->checkPlugins; |
77
|
|
|
|
78
|
57 |
|
$addChecks = function ($rootNode) use ($checkPligins, $builder) { |
|
|
|
|
79
|
57 |
|
foreach ($this->checkPluginClasses as $checkPluginClass) { |
80
|
57 |
|
$checkPlugin = new $checkPluginClass(); |
81
|
|
|
|
82
|
57 |
|
$confMethods = array_filter(get_class_methods($checkPlugin), static function ($n) { |
|
|
|
|
83
|
57 |
|
return preg_match('/Conf$/', $n); |
84
|
57 |
|
}); |
|
|
|
|
85
|
|
|
|
86
|
57 |
|
foreach ($confMethods as $confMethod) { |
87
|
|
|
|
88
|
|
|
/* @var ArrayNodeDefinition $node */ |
89
|
57 |
|
$node = $checkPlugin->$confMethod($builder); |
90
|
57 |
|
$checkName = $node->getNode(true)->getName(); |
91
|
57 |
|
$serviceName = preg_replace('/_factory$/', '', $checkName); |
92
|
|
|
|
93
|
57 |
|
$this->checkPlugins[$checkName] = [ |
94
|
57 |
|
'checkServicePath' => $checkPlugin::PATH.\DIRECTORY_SEPARATOR.'check.yml', |
95
|
57 |
|
'service' => $serviceName, |
96
|
57 |
|
'plugin' => $checkPlugin, |
97
|
|
|
]; |
98
|
|
|
|
99
|
57 |
|
$rootNode->append($node); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
57 |
|
return $rootNode; |
104
|
57 |
|
}; |
105
|
|
|
|
106
|
|
|
$node = $builder |
107
|
57 |
|
->root('checks', 'array') |
108
|
57 |
|
->beforeNormalization() |
109
|
57 |
|
->always(static function ($value) { |
|
|
|
|
110
|
46 |
|
$value = $value ? $value : []; |
111
|
46 |
|
foreach ($value as $k => $v) { |
112
|
46 |
|
$newK = str_replace('(s)', '_factory', $k); |
113
|
46 |
|
if ($newK !== $k) { |
114
|
41 |
|
$value[$newK] = $value[$k]; |
115
|
46 |
|
unset($value[$k]); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
46 |
|
return $value; |
120
|
57 |
|
}) |
|
|
|
|
121
|
57 |
|
->end() |
122
|
57 |
|
->children(); //-- |
123
|
57 |
|
$node = $addChecks($node) |
124
|
57 |
|
->end(); |
125
|
|
|
|
126
|
57 |
|
return $node; |
127
|
|
|
} |
128
|
|
|
|
129
|
57 |
|
private function addReporers(): ArrayNodeDefinition |
|
|
|
|
130
|
|
|
{ |
131
|
57 |
|
return (new TreeBuilder()) |
132
|
57 |
|
->root('reporters', 'array') |
133
|
57 |
|
->children() |
134
|
57 |
|
->arrayNode('mailer') |
|
|
|
|
135
|
57 |
|
->children() |
|
|
|
|
136
|
57 |
|
->scalarNode('recipient') |
|
|
|
|
137
|
57 |
|
->isRequired() |
|
|
|
|
138
|
57 |
|
->cannotBeEmpty() |
|
|
|
|
139
|
57 |
|
->end() |
|
|
|
|
140
|
57 |
|
->scalarNode('sender') |
|
|
|
|
141
|
57 |
|
->isRequired() |
|
|
|
|
142
|
57 |
|
->cannotBeEmpty() |
|
|
|
|
143
|
57 |
|
->end() |
|
|
|
|
144
|
57 |
|
->scalarNode('subject') |
|
|
|
|
145
|
57 |
|
->isRequired() |
|
|
|
|
146
|
57 |
|
->cannotBeEmpty() |
|
|
|
|
147
|
57 |
|
->end() |
|
|
|
|
148
|
57 |
|
->booleanNode('send_on_warning') |
|
|
|
|
149
|
57 |
|
->defaultTrue() |
|
|
|
|
150
|
57 |
|
->end() |
|
|
|
|
151
|
57 |
|
->end() |
|
|
|
|
152
|
57 |
|
->end() |
|
|
|
|
153
|
57 |
|
->end(); |
154
|
|
|
} |
155
|
|
|
|
156
|
57 |
|
private function addTags(): ArrayNodeDefinition |
|
|
|
|
157
|
|
|
{ |
158
|
57 |
|
return (new TreeBuilder()) |
159
|
57 |
|
->root('tags', 'array') |
160
|
57 |
|
->prototype('array') |
|
|
|
|
161
|
57 |
|
->children() |
|
|
|
|
162
|
57 |
|
->scalarNode('name')->end() |
|
|
|
|
163
|
57 |
|
->scalarNode('descr')->end() |
|
|
|
|
164
|
57 |
|
->end() |
|
|
|
|
165
|
57 |
|
->end(); |
166
|
|
|
} |
167
|
|
|
|
168
|
57 |
|
private function addGroups(): ArrayNodeDefinition |
|
|
|
|
169
|
|
|
{ |
170
|
57 |
|
return (new TreeBuilder()) |
171
|
57 |
|
->root('groups', 'array') |
172
|
57 |
|
->prototype('array') |
173
|
57 |
|
->children() |
|
|
|
|
174
|
57 |
|
->scalarNode('name')->end() |
|
|
|
|
175
|
57 |
|
->scalarNode('descr')->end() |
|
|
|
|
176
|
57 |
|
->end() |
|
|
|
|
177
|
57 |
|
->end(); |
178
|
|
|
} |
179
|
|
|
|
180
|
57 |
|
private function addChecksSearchPaths(): ArrayNodeDefinition |
|
|
|
|
181
|
|
|
{ |
182
|
57 |
|
return (new TreeBuilder()) |
183
|
57 |
|
->root('checks_search_paths', 'array') |
184
|
57 |
|
->prototype('scalar')->end(); |
185
|
|
|
} |
186
|
|
|
|
187
|
57 |
|
private function addUIViewTemplate() |
|
|
|
|
188
|
|
|
{ |
189
|
57 |
|
return (new TreeBuilder()) |
190
|
57 |
|
->root('ui_view_template', 'scalar') |
191
|
57 |
|
->cannotBeEmpty() |
|
|
|
|
192
|
57 |
|
->defaultValue('@TviMonitor/UI/index.b4.html.twig'); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|