Completed
Pull Request — master (#123)
by Sascha
04:41
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 46
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 38
Bugs 3 Features 22
Metric Value
c 38
b 3
f 22
dl 0
loc 46
rs 8.4752
cc 5
eloc 36
nc 1
nop 0
1
<?php
2
3
namespace Liip\MonitorBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6
use Symfony\Component\Config\Definition\ConfigurationInterface;
7
8
/**
9
 * This class contains the configuration information for the bundle.
10
 *
11
 * This information is solely responsible for how the different configuration
12
 * sections are normalized, and merged.
13
 *
14
 * @author Lukas Kahwe Smith <[email protected]>
15
 */
16
class Configuration implements ConfigurationInterface
17
{
18
    /**
19
     * Generates the configuration tree.
20
     *
21
     * @return TreeBuilder
22
     */
23
    public function getConfigTreeBuilder()
24
    {
25
        $treeBuilder = new TreeBuilder();
26
        $rootNode = $treeBuilder->root('liip_monitor', 'array');
27
28
        $rootNode
29
            ->beforeNormalization()
30
                ->always(function ($v) {
31
                    if (empty($v['default_group'])) {
32
                        $v['default_group'] = 'default';
33
                    }
34
35
                    if (isset($v['checks']) && is_array($v['checks']) && !isset($v['checks']['groups'])) {
36
                        $checks = $v['checks'];
37
                        unset($v['checks']);
38
39
                        $v['checks']['groups'][$v['default_group']] = $checks;
40
                    }
41
42
                    return $v;
43
                })
44
            ->end()
45
            ->children()
46
                ->booleanNode('enable_controller')->defaultFalse()->end()
47
                ->scalarNode('view_template')->defaultNull()->end()
48
                ->arrayNode('mailer')
49
                    ->canBeEnabled()
50
                    ->children()
51
                        ->scalarNode('recipient')->isRequired()->cannotBeEmpty()->end()
52
                        ->scalarNode('sender')->isRequired()->cannotBeEmpty()->end()
53
                        ->scalarNode('subject')->isRequired()->cannotBeEmpty()->end()
54
                        ->booleanNode('send_on_warning')->defaultTrue()->end()
55
                    ->end()
56
                ->end()
57
                ->scalarNode('default_group')->defaultValue('default')->end()
58
                ->arrayNode('checks')
59
                    ->canBeUnset()
60
                    ->children()
61
                        ->append($this->createGroupsNode())
62
                    ->end()
63
                ->end()
64
            ->end()
65
        ->end();
66
67
        return $treeBuilder;
68
    }
69
70
    private function createGroupsNode()
71
    {
72
        $builder = new TreeBuilder();
73
74
        $node = $builder->root('groups', 'array');
75
        $node
76
            ->requiresAtLeastOneElement()
77
            ->info('Grouping checks')
78
            ->useAttributeAsKey('name')
79
            ->prototype('array')
80
                ->children()
81
                    ->arrayNode('php_extensions')
82
                        ->info('Validate that a named extension or a collection of extensions is available')
83
                        ->example('session.use_only_cookies: false')
84
                        ->prototype('scalar')->end()
85
                    ->end()
86
                    ->arrayNode('php_flags')
87
                        ->info('Pairs of a PHP setting and an expected value')
88
                        ->example('session.use_only_cookies: false')
89
                        ->useAttributeAsKey('setting')
90
                        ->prototype('scalar')->defaultValue(true)->end()
91
                    ->end()
92
                    ->arrayNode('php_version')
93
                        ->info('Pairs of a version and a comparison operator')
94
                        ->example('5.4.15: >=')
95
                        ->useAttributeAsKey('version')
96
                        ->prototype('scalar')->end()
97
                    ->end()
98
                    ->variableNode('process_running')
99
                        ->info('Process name/pid or an array of process names/pids')
100
                        ->example('[apache, foo]')
101
                    ->end()
102
                    ->arrayNode('readable_directory')
103
                        ->info('Validate that a given path (or a collection of paths) is a dir and is readable')
104
                        ->example('["%kernel.cache_dir%"]')
105
                        ->prototype('scalar')->end()
106
                    ->end()
107
                    ->arrayNode('writable_directory')
108
                        ->info('Validate that a given path (or a collection of paths) is a dir and is writable')
109
                        ->example('["%kernel.cache_dir%"]')
110
                        ->prototype('scalar')->end()
111
                    ->end()
112
                    ->arrayNode('class_exists')
113
                        ->info('Validate that a class or a collection of classes is available')
114
                        ->example('["Lua", "My\Fancy\Class"]')
115
                        ->prototype('scalar')->end()
116
                    ->end()
117
                    ->scalarNode('cpu_performance')
118
                        ->info('Benchmark CPU performance and return failure if it is below the given ratio')
119
                        ->example('1.0 # This is the power of an EC2 micro instance')
120
                    ->end()
121
                    ->arrayNode('disk_usage')
122
                        ->info('Checks to see if the disk usage is below warning/critical percent thresholds')
123
                        ->children()
124
                            ->integerNode('warning')->defaultValue(70)->end()
125
                            ->integerNode('critical')->defaultValue(90)->end()
126
                            ->scalarNode('path')->defaultValue('%kernel.cache_dir%')->end()
127
                        ->end()
128
                    ->end()
129
                    ->arrayNode('symfony_requirements')
130
                        ->info('Checks Symfony2 requirements file')
131
                        ->children()
132
                            ->scalarNode('file')->defaultValue('%kernel.root_dir%/SymfonyRequirements.php')->end()
133
                        ->end()
134
                    ->end()
135
                    ->arrayNode('opcache_memory')
136
                        ->info('Checks to see if the OpCache memory usage is below warning/critical thresholds')
137
                        ->children()
138
                            ->integerNode('warning')->defaultValue(70)->end()
139
                            ->integerNode('critical')->defaultValue(90)->end()
140
                        ->end()
141
                    ->end()
142
                    ->arrayNode('apc_memory')
143
                        ->info('Checks to see if the APC memory usage is below warning/critical thresholds')
144
                        ->children()
145
                            ->integerNode('warning')->defaultValue(70)->end()
146
                            ->integerNode('critical')->defaultValue(90)->end()
147
                        ->end()
148
                    ->end()
149
                    ->arrayNode('apc_fragmentation')
150
                        ->info('Checks to see if the APC fragmentation is below warning/critical thresholds')
151
                        ->children()
152
                            ->integerNode('warning')->defaultValue(70)->end()
153
                            ->integerNode('critical')->defaultValue(90)->end()
154
                        ->end()
155
                    ->end()
156
                    ->variableNode('doctrine_dbal')
157
                        ->defaultNull()
158
                        ->info('Connection name or an array of connection names')
159
                        ->example('[default, crm]')
160
                    ->end()
161
                    ->arrayNode('memcache')
162
                        ->info('Check if MemCache extension is loaded and given server is reachable')
163
                        ->useAttributeAsKey('name')
164
                        ->prototype('array')
165
                            ->children()
166
                                ->scalarNode('host')->defaultValue('localhost')->end()
167
                                ->integerNode('port')->defaultValue(11211)->end()
168
                            ->end()
169
                        ->end()
170
                    ->end()
171
                    ->arrayNode('redis')
172
                        ->info('Validate that a Redis service is running')
173
                        ->useAttributeAsKey('name')
174
                        ->prototype('array')
175
                            ->children()
176
                                ->scalarNode('host')->defaultValue('localhost')->end()
177
                                ->integerNode('port')->defaultValue(6379)->end()
178
                            ->end()
179
                        ->end()
180
                    ->end()
181
                    ->arrayNode('http_service')
182
                        ->info('Attempt connection to given HTTP host and (optionally) check status code and page content')
183
                        ->useAttributeAsKey('name')
184
                        ->prototype('array')
185
                            ->children()
186
                                ->scalarNode('host')->defaultValue('localhost')->end()
187
                                ->integerNode('port')->defaultValue(80)->end()
188
                                ->scalarNode('path')->defaultValue('/')->end()
189
                                ->integerNode('status_code')->defaultValue(200)->end()
190
                                ->scalarNode('content')->defaultNull()->end()
191
                            ->end()
192
                        ->end()
193
                    ->end()
194
                    ->arrayNode('guzzle_http_service')
195
                        ->info('Attempt connection using Guzzle to given HTTP host and (optionally) check status code and page content')
196
                        ->useAttributeAsKey('name')
197
                        ->prototype('array')
198
                            ->children()
199
                                ->scalarNode('url')->defaultValue('localhost')->end()
200
                                ->variableNode('headers')->defaultValue(array())->end()
201
                                ->variableNode('options')->defaultValue(array())->end()
202
                                ->integerNode('status_code')->defaultValue(200)->end()
203
                                ->scalarNode('content')->defaultNull()->end()
204
                                ->scalarNode('method')->defaultValue('GET')->end()
205
                                ->scalarNode('body')->defaultNull()->end()
206
                            ->end()
207
                        ->end()
208
                    ->end()
209
                    ->arrayNode('rabbit_mq')
210
                        ->info('Validate that a RabbitMQ service is running')
211
                        ->useAttributeAsKey('name')
212
                        ->prototype('array')
213
                            ->children()
214
                                ->scalarNode('host')->defaultValue('localhost')->end()
215
                                ->integerNode('port')->defaultValue(5672)->end()
216
                                ->scalarNode('user')->defaultValue('guest')->end()
217
                                ->scalarNode('password')->defaultValue('guest')->end()
218
                                ->scalarNode('vhost')->defaultValue('/')->end()
219
                            ->end()
220
                        ->end()
221
                    ->end()
222
                    ->booleanNode('symfony_version')
223
                        ->info('Checks the version of this app against the latest stable release')
224
                    ->end()
225
                    ->arrayNode('custom_error_pages')
226
                        ->info('Checks if error pages have been customized for given error codes')
227
                        ->children()
228
                            ->arrayNode('error_codes')
229
                                ->isRequired()
230
                                ->requiresAtLeastOneElement()
231
                                ->prototype('scalar')->end()
232
                            ->end()
233
                            ->scalarNode('path')->defaultValue('%kernel.root_dir%')->end()
234
                            ->scalarNode('controller')->defaultValue('%twig.exception_listener.controller%')->end()
235
                        ->end()
236
                    ->end()
237
                    ->arrayNode('security_advisory')
238
                        ->info('Checks installed composer dependencies against the SensioLabs Security Advisory database')
239
                        ->children()
240
                            ->scalarNode('lock_file')->defaultValue('%kernel.root_dir%'.'/../composer.lock')->end()
241
                        ->end()
242
                    ->end()
243
                    ->arrayNode('stream_wrapper_exists')
244
                        ->info('Validate that a stream wrapper or collection of stream wrappers exists')
245
                        ->example('[\'zlib\', \'bzip2\', \'zip\']')
246
                        ->prototype('scalar')->end()
247
                    ->end()
248
                    ->arrayNode('file_ini')
249
                        ->info('Find and validate INI files')
250
                        ->example('[\'path/to/my.ini\']')
251
                        ->prototype('scalar')->end()
252
                    ->end()
253
                    ->arrayNode('file_json')
254
                        ->info('Find and validate JSON files')
255
                        ->example('[\'path/to/my.json\']')
256
                        ->prototype('scalar')->end()
257
                    ->end()
258
                    ->arrayNode('file_xml')
259
                        ->info('Find and validate XML files')
260
                        ->example('[\'path/to/my.xml\']')
261
                        ->prototype('scalar')->end()
262
                    ->end()
263
                    ->arrayNode('file_yaml')
264
                        ->info('Find and validate YAML files')
265
                        ->example('[\'path/to/my.yml\']')
266
                        ->prototype('scalar')->end()
267
                    ->end()
268
                    ->arrayNode('expressions')
269
                        ->useAttributeAsKey('alias')
270
                        ->info('Checks that fail/warn when given expression is false (expressions are evaluated with symfony/expression-language)')
271
                        ->example(array(
272
                            'opcache' => array(
273
                                'label' => 'OPcache',
274
                                'warning_expression' => "ini('opcache.revalidate_freq') > 0",
275
                                'critical_expression' => "ini('opcache.enable')",
276
                                'warning_message' => 'OPcache not optimized for production',
277
                                'critical_message' => 'OPcache not enabled',
278
                            ),
279
                        ))
280
                        ->prototype('array')
281
                            ->addDefaultsIfNotSet()
282
                            ->validate()
283
                                ->ifTrue(function ($value) { return !$value['warning_expression'] && !$value['critical_expression']; })
284
                                ->thenInvalid('A warning_expression or a critical_expression must be set.')
285
                            ->end()
286
                            ->children()
287
                                ->scalarNode('label')->isRequired()->end()
288
                                ->scalarNode('warning_expression')
289
                                ->defaultNull()
290
                                ->example('ini(\'apc.stat\') == 0')
291
                            ->end()
292
                            ->scalarNode('critical_expression')
293
                            ->defaultNull()
294
                            ->example('ini(\'short_open_tag\') == 1')
295
                        ->end()
296
                        ->scalarNode('warning_message')->defaultNull()->end()
297
                        ->scalarNode('critical_message')->defaultNull()->end()
298
                    ->end()
299
                ->end()
300
            ->end()
301
        ;
302
303
        return $node;
304
    }
305
}
306