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