Completed
Pull Request — 2.x (#2202)
by Christian
03:59
created

Configuration::addBodyListenerSection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 43
cts 43
cp 1
rs 9.1127
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\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 Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
use Symfony\Component\Serializer\Encoder\XmlEncoder;
21
22
/**
23
 * This class contains the configuration information for the bundle.
24
 *
25
 * This information is solely responsible for how the different configuration
26
 * sections are normalized, and merged.
27
 *
28
 * @author Lukas Kahwe Smith <[email protected]>
29
 *
30
 * @internal
31
 */
32
final class Configuration implements ConfigurationInterface
33
{
34
    /**
35
     * Default debug mode value.
36
     *
37
     * @var bool
38
     */
39
    private $debug;
40
41 83
    public function __construct(bool $debug)
42
    {
43 83
        $this->debug = $debug;
44 83
    }
45
46 82
    public function getConfigTreeBuilder(): TreeBuilder
47
    {
48 82
        $treeBuilder = new TreeBuilder('fos_rest');
49
50 82
        if (method_exists($treeBuilder, 'getRootNode')) {
51 82
            $rootNode = $treeBuilder->getRootNode();
52
        } else {
53
            $rootNode = $treeBuilder->root('fos_rest');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
54
        }
55
56
        $rootNode
57 82
            ->children()
58 82
                ->scalarNode('disable_csrf_role')->defaultNull()->end()
59 82
                ->arrayNode('access_denied_listener')
60 82
                    ->canBeEnabled()
61 82
                    ->beforeNormalization()
62 View Code Duplication
                        ->ifArray()->then(function ($v) {
63
                            if (!empty($v) && empty($v['formats'])) {
64
                                unset($v['enabled']);
65
                                $v = ['enabled' => true, 'formats' => $v];
66
                            }
67
68
                            return $v;
69 82
                        })
70 82
                    ->end()
71 82
                    ->fixXmlConfig('format', 'formats')
72 82
                    ->children()
73 82
                        ->scalarNode('service')->defaultNull()->end()
74 82
                        ->arrayNode('formats')
75 82
                            ->useAttributeAsKey('name')
76 82
                            ->prototype('boolean')->end()
77 82
                        ->end()
78 82
                    ->end()
79 82
                ->end()
80 82
                ->scalarNode('unauthorized_challenge')->defaultNull()->end()
81 82
                ->arrayNode('param_fetcher_listener')
82 82
                    ->beforeNormalization()
83 82
                        ->ifString()
84 View Code Duplication
                        ->then(function ($v) {
85 1
                            return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v];
86 82
                        })
87 82
                    ->end()
88 82
                    ->canBeEnabled()
89 82
                    ->children()
90 82
                        ->booleanNode('force')->defaultFalse()->end()
91 82
                        ->scalarNode('service')->defaultNull()->end()
92 82
                    ->end()
93 82
                ->end()
94 82
                ->scalarNode('cache_dir')->cannotBeEmpty()->defaultValue('%kernel.cache_dir%/fos_rest')->end()
95 82
                ->arrayNode('allowed_methods_listener')
96 82
                    ->canBeEnabled()
97 82
                    ->children()
98 82
                        ->scalarNode('service')->defaultNull()->end()
99 82
                    ->end()
100 82
                ->end()
101 82
                ->arrayNode('routing_loader')
102 82
                    ->addDefaultsIfNotSet()
103 82
                    ->beforeNormalization()
104
                        ->ifTrue(function ($v) { return isset($v['enabled']) && false !== $v['enabled']; })
105
                        ->then(function ($v) {
106
                            @trigger_error('Enabling the route generation feature is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
107
108
                            return $v;
109 82
                        })
110 82
                    ->end()
111 82
                    ->beforeNormalization()
112
                        ->ifTrue(function ($v) { return is_bool($v); })
113
                        ->then(function ($v) {
114
                            return [
115 65
                                'enabled' => $v,
116
                            ];
117 82
                        })
118 82
                    ->end()
119 82
                    ->children()
120 82
                        ->booleanNode('enabled')
121
                            ->defaultValue(function () {
122 6
                                @trigger_error('Enabling the route generation feature is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
123
124 6
                                return true;
125 82
                            })
126 82
                        ->end()
127 82
                        ->scalarNode('default_format')->defaultNull()->end()
128 82
                        ->scalarNode('prefix_methods')->defaultTrue()->end()
129 82
                        ->scalarNode('include_format')->defaultTrue()->end()
130 82
                    ->end()
131 82
                ->end()
132 82
                ->arrayNode('body_converter')
133 82
                    ->canBeEnabled()
134 82
                    ->children()
135 82
                        ->scalarNode('validate')
136 82
                            ->defaultFalse()
137 82
                            ->beforeNormalization()
138 82
                                ->ifTrue()
139
                                ->then(function ($value) {
140 3
                                    if (!class_exists(OptionsResolver::class)) {
141
                                        throw new InvalidConfigurationException("'body_converter.validate: true' requires OptionsResolver component installation ( composer require symfony/options-resolver )");
142
                                    }
143
144 3
                                    return $value;
145 82
                                })
146 82
                            ->end()
147 82
                        ->end()
148 82
                        ->scalarNode('validation_errors_argument')->defaultValue('validationErrors')->end()
149 82
                    ->end()
150 82
                ->end()
151 82
                ->arrayNode('service')
152 82
                    ->addDefaultsIfNotSet()
153 82
                    ->children()
154 82
                        ->scalarNode('router')->defaultValue('router')->setDeprecated('The "%path%.%node%" configuration key has been deprecated in FOSRestBundle 2.8.')->end()
155 82
                        ->scalarNode('templating')
156 82
                            ->defaultValue('templating')
157 82
                            ->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
158 82
                        ->end()
159 82
                        ->scalarNode('serializer')->defaultNull()->end()
160 82
                        ->scalarNode('view_handler')->defaultValue('fos_rest.view_handler.default')->end()
161 82
                        ->scalarNode('inflector')
162 82
                            ->defaultValue('fos_rest.inflector.doctrine')
163 82
                            ->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
164 82
                        ->end()
165 82
                        ->scalarNode('validator')->defaultValue('validator')->end()
166 82
                    ->end()
167 82
                ->end()
168 82
                ->arrayNode('serializer')
169 82
                    ->addDefaultsIfNotSet()
170 82
                    ->children()
171 82
                        ->scalarNode('version')->defaultNull()->end()
172 82
                        ->arrayNode('groups')
173 82
                            ->prototype('scalar')->end()
174 82
                        ->end()
175 82
                        ->booleanNode('serialize_null')->defaultFalse()->end()
176 82
                    ->end()
177 82
                ->end()
178 82
                ->arrayNode('zone')
179 82
                    ->cannotBeOverwritten()
180 82
                    ->prototype('array')
181 82
                    ->fixXmlConfig('ip')
182 82
                    ->children()
183 82
                        ->scalarNode('path')
184 82
                            ->defaultNull()
185 82
                            ->info('use the urldecoded format')
186 82
                            ->example('^/path to resource/')
187 82
                        ->end()
188 82
                        ->scalarNode('host')->defaultNull()->end()
189 82
                        ->arrayNode('methods')
190
                            ->beforeNormalization()->ifString()->then(function ($v) {
191
                                return preg_split('/\s*,\s*/', $v);
192 82
                            })->end()
193 82
                            ->prototype('scalar')->end()
194 82
                        ->end()
195 82
                        ->arrayNode('ips')
196
                            ->beforeNormalization()->ifString()->then(function ($v) {
197 1
                                return array($v);
198 82
                            })->end()
199 82
                            ->prototype('scalar')->end()
200 82
                        ->end()
201 82
                    ->end()
202 82
                ->end()
203 82
            ->end()
204 82
        ->end();
205
206 82
        $this->addViewSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
207 82
        $this->addExceptionSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
208 82
        $this->addBodyListenerSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
209 82
        $this->addFormatListenerSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
210 82
        $this->addVersioningSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
211
212 82
        return $treeBuilder;
213
    }
214
215 82
    private function addViewSection(ArrayNodeDefinition $rootNode)
216
    {
217
        $rootNode
218 82
            ->children()
219 82
                ->arrayNode('view')
220 82
                    ->fixXmlConfig('format', 'formats')
221 82
                    ->fixXmlConfig('mime_type', 'mime_types')
222 82
                    ->fixXmlConfig('templating_format', 'templating_formats')
223 82
                    ->fixXmlConfig('force_redirect', 'force_redirects')
224 82
                    ->addDefaultsIfNotSet()
225 82
                    ->children()
226 82
                        ->scalarNode('default_engine')
227
                            ->defaultValue(static function () {
228 5
                                @trigger_error('Not setting the "fos_rest.view.default_engine" configuration option to "null" is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
229
230 5
                                return 'twig';
231 82
                            })
232 82
                            ->validate()
233
                                ->ifTrue(static function ($v) { return $v; })
234
                                ->then(static function ($v) {
235
                                    @trigger_error('Not setting the "fos_rest.view.default_engine" configuration option to "null" is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
236
237
                                    return $v;
238 82
                                })
239 82
                            ->end()
240 82
                        ->end()
241 82
                        ->arrayNode('force_redirects')
242 82
                            ->useAttributeAsKey('name')
243 82
                            ->defaultValue(['html' => true])
244 82
                            ->prototype('boolean')->end()
245 82
                        ->end()
246 82
                        ->arrayNode('mime_types')
247 82
                            ->canBeEnabled()
248 82
                            ->beforeNormalization()
249 View Code Duplication
                                ->ifArray()->then(function ($v) {
250 1
                                    if (!empty($v) && empty($v['formats'])) {
251 1
                                        unset($v['enabled']);
252 1
                                        $v = ['enabled' => true, 'formats' => $v];
253
                                    }
254
255 1
                                    return $v;
256 82
                                })
257 82
                            ->end()
258 82
                            ->fixXmlConfig('format', 'formats')
259 82
                            ->children()
260 82
                                ->scalarNode('service')->defaultNull()->end()
261 82
                                ->arrayNode('formats')
262 82
                                    ->useAttributeAsKey('name')
263 82
                                    ->prototype('array')
264 82
                                        ->beforeNormalization()
265 82
                                            ->ifString()
266
                                            ->then(function ($v) { return array($v); })
267 82
                                        ->end()
268 82
                                        ->prototype('scalar')->end()
269 82
                                    ->end()
270 82
                                ->end()
271 82
                            ->end()
272 82
                        ->end()
273 82
                        ->arrayNode('formats')
274 82
                            ->useAttributeAsKey('name')
275 82
                            ->defaultValue(['json' => true, 'xml' => true])
276 82
                            ->prototype('boolean')->end()
277 82
                        ->end()
278 82
                        ->arrayNode('templating_formats')
279 82
                            ->setDeprecated('The "%path%.%node%" configuration key has been deprecated in FOSRestBundle 2.8.')
280 82
                            ->useAttributeAsKey('name')
281 82
                            ->defaultValue(['html' => true])
282 82
                            ->prototype('boolean')->end()
283 82
                        ->end()
284 82
                        ->arrayNode('view_response_listener')
285 82
                            ->beforeNormalization()
286 82
                                ->ifString()
287 View Code Duplication
                                ->then(function ($v) {
288 9
                                    return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v];
289 82
                                })
290 82
                            ->end()
291 82
                            ->canBeEnabled()
292 82
                            ->children()
293 82
                                ->booleanNode('force')->defaultFalse()->end()
294 82
                                ->scalarNode('service')->defaultNull()->end()
295 82
                            ->end()
296 82
                        ->end()
297 82
                        ->scalarNode('failed_validation')->defaultValue(Response::HTTP_BAD_REQUEST)->end()
298 82
                        ->scalarNode('empty_content')->defaultValue(Response::HTTP_NO_CONTENT)->end()
299 82
                        ->booleanNode('serialize_null')->defaultFalse()->end()
300 82
                        ->arrayNode('jsonp_handler')
301 82
                            ->canBeUnset()
302 82
                            ->children()
303 82
                                ->scalarNode('callback_param')->defaultValue('callback')->end()
304 82
                                ->scalarNode('mime_type')->defaultValue('application/javascript+jsonp')->end()
305 82
                            ->end()
306 82
                        ->end()
307 82
                    ->end()
308 82
                ->end()
309 82
            ->end();
310 82
    }
311
312 82
    private function addBodyListenerSection(ArrayNodeDefinition $rootNode)
313
    {
314 82
        $decodersDefaultValue = ['json' => 'fos_rest.decoder.json'];
315 82
        if (class_exists(XmlEncoder::class)) {
316 82
            $decodersDefaultValue['xml'] = 'fos_rest.decoder.xml';
317
        }
318
        $rootNode
319 82
            ->children()
320 82
                ->arrayNode('body_listener')
321 82
                    ->fixXmlConfig('decoder', 'decoders')
322 82
                    ->addDefaultsIfNotSet()
323 82
                    ->canBeUnset()
324 82
                    ->treatFalseLike(['enabled' => false])
325 82
                    ->treatTrueLike(['enabled' => true])
326 82
                    ->treatNullLike(['enabled' => true])
327 82
                    ->children()
328 82
                        ->booleanNode('enabled')
329
                            ->defaultValue(function () {
330 6
                                @trigger_error('The body_listener config has been enabled by default and will be disabled by default in FOSRestBundle 3.0. Please enable or disable it explicitly.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
331
332 6
                                return true;
333 82
                            })
334 82
                        ->end()
335 82
                        ->scalarNode('service')->defaultNull()->end()
336 82
                        ->scalarNode('default_format')->defaultNull()->end()
337 82
                        ->booleanNode('throw_exception_on_unsupported_content_type')
338 82
                            ->defaultFalse()
339 82
                        ->end()
340 82
                        ->arrayNode('decoders')
341 82
                            ->useAttributeAsKey('name')
342 82
                            ->defaultValue($decodersDefaultValue)
343 82
                            ->prototype('scalar')->end()
344 82
                        ->end()
345 82
                        ->arrayNode('array_normalizer')
346 82
                            ->addDefaultsIfNotSet()
347 82
                            ->beforeNormalization()
348
                                ->ifString()->then(function ($v) {
349 1
                                    return ['service' => $v];
350 82
                                })
351 82
                            ->end()
352 82
                            ->children()
353 82
                                ->scalarNode('service')->defaultNull()->end()
354 82
                                ->booleanNode('forms')->defaultFalse()->end()
355 82
                            ->end()
356 82
                        ->end()
357 82
                    ->end()
358 82
                ->end()
359 82
            ->end();
360 82
    }
361
362 82
    private function addFormatListenerSection(ArrayNodeDefinition $rootNode)
363
    {
364
        $rootNode
365 82
            ->children()
366 82
                ->arrayNode('format_listener')
367 82
                    ->fixXmlConfig('rule', 'rules')
368 82
                    ->addDefaultsIfNotSet()
369 82
                    ->canBeUnset()
370 82
                    ->beforeNormalization()
371
                        ->ifTrue(function ($v) {
372
                            // check if we got an assoc array in rules
373 9
                            return isset($v['rules'])
374 9
                                && is_array($v['rules'])
375 9
                                && array_keys($v['rules']) !== range(0, count($v['rules']) - 1);
376 82
                        })
377
                        ->then(function ($v) {
378 1
                            $v['rules'] = [$v['rules']];
379
380 1
                            return $v;
381 82
                        })
382 82
                    ->end()
383 82
                    ->canBeEnabled()
384 82
                    ->children()
385 82
                        ->scalarNode('service')->defaultNull()->end()
386 82
                        ->arrayNode('rules')
387 82
                            ->performNoDeepMerging()
388 82
                            ->prototype('array')
389 82
                                ->fixXmlConfig('priority', 'priorities')
390 82
                                ->fixXmlConfig('attribute', 'attributes')
391 82
                                ->children()
392 82
                                    ->scalarNode('path')->defaultNull()->info('URL path info')->end()
393 82
                                    ->scalarNode('host')->defaultNull()->info('URL host name')->end()
394 82
                                    ->variableNode('methods')->defaultNull()->info('Method for URL')->end()
395 82
                                    ->arrayNode('attributes')
396 82
                                        ->useAttributeAsKey('name')
397 82
                                        ->prototype('variable')->end()
398 82
                                    ->end()
399 82
                                    ->booleanNode('stop')->defaultFalse()->end()
400 82
                                    ->booleanNode('prefer_extension')->defaultTrue()->end()
401 82
                                    ->scalarNode('fallback_format')->defaultValue('html')->end()
402 82
                                    ->arrayNode('priorities')
403
                                        ->beforeNormalization()->ifString()->then(function ($v) {
404
                                            return preg_split('/\s*,\s*/', $v);
405 82
                                        })->end()
406 82
                                        ->prototype('scalar')->end()
407 82
                                    ->end()
408 82
                                ->end()
409 82
                            ->end()
410 82
                        ->end()
411 82
                    ->end()
412 82
                ->end()
413 82
            ->end();
414 82
    }
415
416 82
    private function addVersioningSection(ArrayNodeDefinition $rootNode)
417
    {
418
        $rootNode
419 82
        ->children()
420 82
            ->arrayNode('versioning')
421 82
                ->canBeEnabled()
422 82
                ->children()
423 82
                    ->scalarNode('default_version')->defaultNull()->end()
424 82
                    ->arrayNode('resolvers')
425 82
                        ->addDefaultsIfNotSet()
426 82
                        ->children()
427 82
                            ->arrayNode('query')
428 82
                                ->canBeDisabled()
429 82
                                ->children()
430 82
                                    ->scalarNode('parameter_name')->defaultValue('version')->end()
431 82
                                ->end()
432 82
                            ->end()
433 82
                            ->arrayNode('custom_header')
434 82
                                ->canBeDisabled()
435 82
                                ->children()
436 82
                                    ->scalarNode('header_name')->defaultValue('X-Accept-Version')->end()
437 82
                                ->end()
438 82
                            ->end()
439 82
                            ->arrayNode('media_type')
440 82
                                ->canBeDisabled()
441 82
                                ->children()
442 82
                                    ->scalarNode('regex')->defaultValue('/(v|version)=(?P<version>[0-9\.]+)/')->end()
443 82
                                ->end()
444 82
                            ->end()
445 82
                        ->end()
446 82
                    ->end()
447 82
                    ->arrayNode('guessing_order')
448 82
                        ->defaultValue(['query', 'custom_header', 'media_type'])
449 82
                        ->validate()
450
                            ->ifTrue(function ($v) {
451
                                foreach ($v as $resolver) {
452
                                    if (!in_array($resolver, ['query', 'custom_header', 'media_type'])) {
453
                                        return true;
454
                                    }
455
                                }
456 82
                            })
457 82
                            ->thenInvalid('Versioning guessing order can only contain "query", "custom_header", "media_type".')
458 82
                        ->end()
459 82
                        ->prototype('scalar')->end()
460 82
                    ->end()
461 82
                ->end()
462 82
            ->end()
463 82
        ->end();
464 82
    }
465
466 82
    private function addExceptionSection(ArrayNodeDefinition $rootNode)
467
    {
468
        $rootNode
469 82
            ->children()
470 82
                ->arrayNode('exception')
471 82
                    ->fixXmlConfig('code', 'codes')
472 82
                    ->fixXmlConfig('message', 'messages')
473 82
                    ->addDefaultsIfNotSet()
474 82
                    ->canBeEnabled()
475 82
                    ->children()
476 82
                        ->booleanNode('map_exception_codes')
477 82
                            ->defaultFalse()
478 82
                            ->info('Enables an event listener that maps exception codes to response status codes based on the map configured with the "fos_rest.exception.codes" option.')
479 82
                        ->end()
480 82
                        ->booleanNode('exception_listener')
481
                            ->defaultValue(function () {
482 14
                                @trigger_error('Enabling the "fos_rest.exception.exception_listener" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
483
484 14
                                return true;
485 82
                            })
486 82
                            ->beforeNormalization()
487 82
                                ->ifTrue()
488
                                ->then(function ($v) {
489
                                    @trigger_error('Enabling the "fos_rest.exception.exception_listener" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
490
491
                                    return $v;
492 82
                                })
493 82
                            ->end()
494 82
                        ->end()
495 82
                        ->scalarNode('exception_controller')
496 82
                            ->defaultNull()
497 82
                            ->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
498 82
                        ->end()
499 82
                        ->booleanNode('serialize_exceptions')
500
                            ->defaultValue(function () {
501 9
                                @trigger_error('Enabling the "fos_rest.exception.serialize_exceptions" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
502
503 9
                                return true;
504 82
                            })
505 82
                            ->beforeNormalization()
506 82
                                ->ifTrue()
507
                                ->then(function ($v) {
508 8
                                    @trigger_error('Enabling the "fos_rest.exception.serialize_exceptions" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
509
510 8
                                    return $v;
511 82
                                })
512 82
                            ->end()
513 82
                        ->end()
514 82
                        ->enumNode('flatten_exception_format')
515 82
                            ->defaultValue('legacy')
516 82
                            ->values(['legacy', 'rfc7807'])
517 82
                        ->end()
518 82
                        ->scalarNode('service')
519 82
                            ->defaultNull()
520 82
                            ->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
521 82
                        ->end()
522 82
                        ->booleanNode('serializer_error_renderer')->defaultValue(false)->end()
523 82
                        ->arrayNode('codes')
524 82
                            ->useAttributeAsKey('name')
525 82
                            ->beforeNormalization()
526 82
                                ->ifArray()
527
                                ->then(function (array $items) {
528 13
                                    foreach ($items as &$item) {
529 13
                                        if (is_int($item)) {
530 3
                                            continue;
531
                                        }
532
533 10
                                        if (!defined(sprintf('%s::%s', Response::class, $item))) {
534 9
                                            throw new InvalidConfigurationException(sprintf('Invalid HTTP code in fos_rest.exception.codes, see %s for all valid codes.', Response::class));
535
                                        }
536
537 1
                                        $item = constant(sprintf('%s::%s', Response::class, $item));
538
                                    }
539
540 4
                                    return $items;
541 82
                                })
542 82
                            ->end()
543 82
                            ->prototype('integer')->end()
544
545 82
                            ->validate()
546 82
                            ->ifArray()
547
                                ->then(function (array $items) {
548 4
                                    foreach ($items as $class => $code) {
549 4
                                        $this->testExceptionExists($class);
550
                                    }
551
552 3
                                    return $items;
553 82
                                })
554 82
                            ->end()
555 82
                        ->end()
556 82
                        ->arrayNode('messages')
557 82
                            ->useAttributeAsKey('name')
558 82
                            ->prototype('boolean')->end()
559 82
                            ->validate()
560 82
                                ->ifArray()
561
                                ->then(function (array $items) {
562 12
                                    foreach ($items as $class => $nomatter) {
563 12
                                        $this->testExceptionExists($class);
564
                                    }
565
566 11
                                    return $items;
567 82
                                })
568 82
                            ->end()
569 82
                        ->end()
570 82
                        ->booleanNode('debug')
571 82
                            ->defaultValue($this->debug)
572 82
                        ->end()
573 82
                    ->end()
574 82
                ->end()
575 82
            ->end();
576 82
    }
577
578 16
    private function testExceptionExists(string $throwable)
579
    {
580 16
        if (!is_subclass_of($throwable, \Throwable::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Throwable::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
581 2
            throw new InvalidConfigurationException(sprintf('FOSRestBundle exception mapper: Could not load class "%s" or the class does not extend from "%s". Most probably this is a configuration problem.', $throwable, \Throwable::class));
582
        }
583 14
    }
584
}
585