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
|
|
|
* @param string[]|null $checksSearchPaths |
|
|
|
|
40
|
|
|
*/ |
41
|
53 |
|
public function __construct(CheckPluginFinder $pluginFinder) |
42
|
|
|
{ |
43
|
53 |
|
$this->checkPluginClasses = $pluginFinder->find(); |
44
|
53 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
|
|
|
|
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
47 |
|
public function getCheckPlugins(): array |
50
|
|
|
{ |
51
|
47 |
|
return $this->checkPlugins; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Generates the configuration tree. |
56
|
|
|
* |
57
|
|
|
* @return TreeBuilder |
58
|
|
|
*/ |
59
|
53 |
|
public function getConfigTreeBuilder() |
60
|
|
|
{ |
61
|
53 |
|
$treeBuilder = new TreeBuilder(); |
62
|
|
|
|
63
|
53 |
|
$treeBuilder->root('tvi_monitor', 'array') |
64
|
53 |
|
->children() |
65
|
53 |
|
->append($this->addChecksSearchPaths()) |
|
|
|
|
66
|
53 |
|
->append($this->addTags()) |
|
|
|
|
67
|
53 |
|
->append($this->addReporers()) |
|
|
|
|
68
|
53 |
|
->append($this->addChecks()) |
|
|
|
|
69
|
53 |
|
->end() |
70
|
53 |
|
->end(); |
|
|
|
|
71
|
|
|
|
72
|
53 |
|
return $treeBuilder; |
73
|
|
|
} |
74
|
|
|
|
75
|
53 |
|
private function addChecks(): ArrayNodeDefinition |
|
|
|
|
76
|
|
|
{ |
77
|
53 |
|
$builder = new TreeBuilder(); |
78
|
|
|
|
79
|
53 |
|
$checkPligins = $this->checkPlugins; |
80
|
|
|
|
81
|
53 |
|
$addChecks = function ($rootNode) use ($checkPligins, $builder) { |
|
|
|
|
82
|
53 |
|
foreach ($this->checkPluginClasses as $checkPluginClass) { |
83
|
53 |
|
$checkPligin = new $checkPluginClass(); |
84
|
|
|
|
85
|
53 |
|
$confMethods = array_filter(get_class_methods($checkPligin), static function ($n) { |
|
|
|
|
86
|
53 |
|
return preg_match('/Conf$/', $n); |
87
|
53 |
|
}); |
|
|
|
|
88
|
|
|
|
89
|
53 |
|
foreach ($confMethods as $confMethod) { |
90
|
|
|
|
91
|
|
|
/* @var ArrayNodeDefinition $node */ |
92
|
53 |
|
$node = $checkPligin->$confMethod($builder); |
93
|
53 |
|
$checkName = $node->getNode(true)->getName(); |
94
|
53 |
|
$serviceName = preg_replace('/_factory$/', '', $checkName); |
95
|
|
|
|
96
|
53 |
|
$this->checkPlugins[$checkName] = [ |
97
|
53 |
|
'checkServicePath' => $checkPligin::PATH.\DIRECTORY_SEPARATOR.'check.yml', |
98
|
53 |
|
'service' => $serviceName, |
99
|
53 |
|
'pligin' => $checkPligin, |
100
|
|
|
]; |
101
|
|
|
|
102
|
53 |
|
$rootNode->append($node); |
103
|
|
|
} |
104
|
|
|
} |
105
|
53 |
|
return $rootNode; |
106
|
53 |
|
}; |
107
|
|
|
|
108
|
|
|
$node = $builder |
109
|
53 |
|
->root('checks', 'array') |
110
|
53 |
|
->beforeNormalization() |
111
|
53 |
|
->always(static function ($value) { |
|
|
|
|
112
|
42 |
|
$value = $value ? $value : []; |
113
|
42 |
|
foreach ($value as $k => $v) { |
114
|
42 |
|
$newK = str_replace('(s)', '_factory', $k); |
115
|
42 |
|
if ($newK !== $k) { |
116
|
37 |
|
$value[$newK] = $value[$k]; |
117
|
42 |
|
unset($value[$k]); |
118
|
|
|
} |
119
|
|
|
} |
120
|
42 |
|
return $value; |
121
|
53 |
|
}) |
|
|
|
|
122
|
53 |
|
->end() |
123
|
53 |
|
->children(); //-- |
124
|
53 |
|
$node = $addChecks($node) |
125
|
53 |
|
->end(); |
126
|
|
|
|
127
|
53 |
|
return $node; |
128
|
|
|
} |
129
|
|
|
|
130
|
53 |
|
private function addReporers(): ArrayNodeDefinition |
|
|
|
|
131
|
|
|
{ |
132
|
53 |
|
return (new TreeBuilder()) |
133
|
53 |
|
->root('reporters', 'array') |
134
|
53 |
|
->children() |
135
|
53 |
|
->arrayNode('mailer') |
|
|
|
|
136
|
53 |
|
->children() |
|
|
|
|
137
|
53 |
|
->scalarNode('recipient') |
|
|
|
|
138
|
53 |
|
->isRequired() |
|
|
|
|
139
|
53 |
|
->cannotBeEmpty() |
|
|
|
|
140
|
53 |
|
->end() |
|
|
|
|
141
|
53 |
|
->scalarNode('sender') |
|
|
|
|
142
|
53 |
|
->isRequired() |
|
|
|
|
143
|
53 |
|
->cannotBeEmpty() |
|
|
|
|
144
|
53 |
|
->end() |
|
|
|
|
145
|
53 |
|
->scalarNode('subject') |
|
|
|
|
146
|
53 |
|
->isRequired() |
|
|
|
|
147
|
53 |
|
->cannotBeEmpty() |
|
|
|
|
148
|
53 |
|
->end() |
|
|
|
|
149
|
53 |
|
->booleanNode('send_on_warning') |
|
|
|
|
150
|
53 |
|
->defaultTrue() |
|
|
|
|
151
|
53 |
|
->end() |
|
|
|
|
152
|
53 |
|
->end() |
|
|
|
|
153
|
53 |
|
->end() |
|
|
|
|
154
|
53 |
|
->end(); |
155
|
|
|
} |
156
|
|
|
|
157
|
53 |
|
private function addTags(): ArrayNodeDefinition |
|
|
|
|
158
|
|
|
{ |
159
|
53 |
|
return (new TreeBuilder()) |
160
|
53 |
|
->root('tags', 'array') |
161
|
53 |
|
->prototype('scalar')->end(); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
53 |
|
private function addChecksSearchPaths(): ArrayNodeDefinition |
|
|
|
|
165
|
|
|
{ |
166
|
53 |
|
return (new TreeBuilder()) |
167
|
53 |
|
->root('checks_search_paths', 'array') |
168
|
53 |
|
->prototype('scalar')->end(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|