• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 13175309672

06 Feb 2025 09:04AM UTC coverage: 7.663% (-0.2%) from 7.841%
13175309672

push

github

web-flow
fix: ensure template files have a tpl file extension (#6826) (#6829)

2 of 5 new or added lines in 4 files covered. (40.0%)

3676 existing lines in 122 files now uncovered.

13073 of 170593 relevant lines covered (7.66%)

27.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.35
/src/Symfony/Bundle/DependencyInjection/Configuration.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.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
declare(strict_types=1);
13

14
namespace ApiPlatform\Symfony\Bundle\DependencyInjection;
15

16
use ApiPlatform\Doctrine\Common\Filter\OrderFilterInterface;
17
use ApiPlatform\Elasticsearch\State\Options;
18
use ApiPlatform\Metadata\ApiResource;
19
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
20
use ApiPlatform\Metadata\Post;
21
use ApiPlatform\Metadata\Put;
22
use ApiPlatform\Symfony\Controller\MainController;
23
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
24
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
25
use Doctrine\ORM\EntityManagerInterface;
26
use Doctrine\ORM\OptimisticLockException;
27
use GraphQL\GraphQL;
28
use Symfony\Bundle\FullStack;
29
use Symfony\Bundle\MakerBundle\MakerBundle;
30
use Symfony\Bundle\MercureBundle\MercureBundle;
31
use Symfony\Bundle\TwigBundle\TwigBundle;
32
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
33
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
34
use Symfony\Component\Config\Definition\ConfigurationInterface;
35
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
36
use Symfony\Component\HttpFoundation\Response;
37
use Symfony\Component\Messenger\MessageBusInterface;
38
use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface;
39
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
40
use Symfony\Component\Yaml\Yaml;
41

42
/**
43
 * The configuration of the bundle.
44
 *
45
 * @author Kévin Dunglas <dunglas@gmail.com>
46
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
47
 */
48
final class Configuration implements ConfigurationInterface
49
{
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getConfigTreeBuilder(): TreeBuilder
54
    {
55
        $treeBuilder = new TreeBuilder('api_platform');
57✔
56
        $rootNode = $treeBuilder->getRootNode();
57✔
57

58
        $rootNode
57✔
59
            ->beforeNormalization()
57✔
60
                ->ifTrue(static function ($v) {
57✔
61
                    return false === ($v['enable_swagger'] ?? null);
57✔
62
                })
57✔
63
                ->then(static function ($v) {
57✔
64
                    $v['swagger']['versions'] = [];
3✔
65

66
                    return $v;
3✔
67
                })
57✔
68
            ->end()
57✔
69
            ->children()
57✔
70
                ->scalarNode('title')
57✔
71
                    ->info('The title of the API.')
57✔
72
                    ->cannotBeEmpty()
57✔
73
                    ->defaultValue('')
57✔
74
                ->end()
57✔
75
                ->scalarNode('description')
57✔
76
                    ->info('The description of the API.')
57✔
77
                    ->cannotBeEmpty()
57✔
78
                    ->defaultValue('')
57✔
79
                ->end()
57✔
80
                ->scalarNode('version')
57✔
81
                    ->info('The version of the API.')
57✔
82
                    ->cannotBeEmpty()
57✔
83
                    ->defaultValue('0.0.0')
57✔
84
                ->end()
57✔
85
                ->booleanNode('show_webby')->defaultTrue()->info('If true, show Webby on the documentation page')->end()
57✔
86
                ->booleanNode('use_symfony_listeners')->defaultFalse()->info(sprintf('Uses Symfony event listeners instead of the %s.', MainController::class))->end()
57✔
87
                ->scalarNode('name_converter')->defaultNull()->info('Specify a name converter to use.')->end()
57✔
88
                ->scalarNode('asset_package')->defaultNull()->info('Specify an asset package name to use.')->end()
57✔
89
                ->scalarNode('path_segment_name_generator')->defaultValue('api_platform.metadata.path_segment_name_generator.underscore')->info('Specify a path name generator to use.')->end()
57✔
90
                ->scalarNode('inflector')->defaultValue('api_platform.metadata.inflector')->info('Specify an inflector to use.')->end()
57✔
91
                ->arrayNode('validator')
57✔
92
                    ->addDefaultsIfNotSet()
57✔
93
                    ->children()
57✔
94
                        ->variableNode('serialize_payload_fields')->defaultValue([])->info('Set to null to serialize all payload fields when a validation error is thrown, or set the fields you want to include explicitly.')->end()
57✔
95
                        ->booleanNode('query_parameter_validation')->defaultValue(true)->end()
57✔
96
                    ->end()
57✔
97
                ->end()
57✔
98
                ->arrayNode('eager_loading')
57✔
99
                    ->canBeDisabled()
57✔
100
                    ->addDefaultsIfNotSet()
57✔
101
                    ->children()
57✔
102
                        ->booleanNode('fetch_partial')->defaultFalse()->info('Fetch only partial data according to serialization groups. If enabled, Doctrine ORM entities will not work as expected if any of the other fields are used.')->end()
57✔
103
                        ->integerNode('max_joins')->defaultValue(30)->info('Max number of joined relations before EagerLoading throws a RuntimeException')->end()
57✔
104
                        ->booleanNode('force_eager')->defaultTrue()->info('Force join on every relation. If disabled, it will only join relations having the EAGER fetch mode.')->end()
57✔
105
                    ->end()
57✔
106
                ->end()
57✔
107
                ->booleanNode('handle_symfony_errors')->defaultFalse()->info('Allows to handle symfony exceptions.')->end()
57✔
108
                ->booleanNode('enable_swagger')->defaultTrue()->info('Enable the Swagger documentation and export.')->end()
57✔
109
                ->booleanNode('enable_swagger_ui')->defaultValue(class_exists(TwigBundle::class))->info('Enable Swagger UI')->end()
57✔
110
                ->booleanNode('enable_re_doc')->defaultValue(class_exists(TwigBundle::class))->info('Enable ReDoc')->end()
57✔
111
                ->booleanNode('enable_entrypoint')->defaultTrue()->info('Enable the entrypoint')->end()
57✔
112
                ->booleanNode('enable_docs')->defaultTrue()->info('Enable the docs')->end()
57✔
113
                ->booleanNode('enable_profiler')->defaultTrue()->info('Enable the data collector and the WebProfilerBundle integration.')->end()
57✔
114
                ->booleanNode('enable_link_security')->defaultFalse()->info('Enable security for Links (sub resources)')->end()
57✔
115
                ->arrayNode('collection')
57✔
116
                    ->addDefaultsIfNotSet()
57✔
117
                    ->children()
57✔
118
                        ->scalarNode('exists_parameter_name')->defaultValue('exists')->cannotBeEmpty()->info('The name of the query parameter to filter on nullable field values.')->end()
57✔
119
                        ->scalarNode('order')->defaultValue('ASC')->info('The default order of results.')->end() // Default ORDER is required for postgresql and mysql >= 5.7 when using LIMIT/OFFSET request
57✔
120
                        ->scalarNode('order_parameter_name')->defaultValue('order')->cannotBeEmpty()->info('The name of the query parameter to order results.')->end()
57✔
121
                        ->enumNode('order_nulls_comparison')->defaultNull()->values(interface_exists(OrderFilterInterface::class) ? array_merge(array_keys(OrderFilterInterface::NULLS_DIRECTION_MAP), [null]) : [null])->info('The nulls comparison strategy.')->end()
57✔
122
                        ->arrayNode('pagination')
57✔
123
                            ->canBeDisabled()
57✔
124
                            ->addDefaultsIfNotSet()
57✔
125
                            ->children()
57✔
126
                                ->scalarNode('page_parameter_name')->defaultValue('page')->cannotBeEmpty()->info('The default name of the parameter handling the page number.')->end()
57✔
127
                                ->scalarNode('enabled_parameter_name')->defaultValue('pagination')->cannotBeEmpty()->info('The name of the query parameter to enable or disable pagination.')->end()
57✔
128
                                ->scalarNode('items_per_page_parameter_name')->defaultValue('itemsPerPage')->cannotBeEmpty()->info('The name of the query parameter to set the number of items per page.')->end()
57✔
129
                                ->scalarNode('partial_parameter_name')->defaultValue('partial')->cannotBeEmpty()->info('The name of the query parameter to enable or disable partial pagination.')->end()
57✔
130
                            ->end()
57✔
131
                        ->end()
57✔
132
                    ->end()
57✔
133
                ->end()
57✔
134
                ->arrayNode('mapping')
57✔
135
                    ->addDefaultsIfNotSet()
57✔
136
                    ->children()
57✔
137
                        ->arrayNode('paths')
57✔
138
                            ->prototype('scalar')->end()
57✔
139
                        ->end()
57✔
140
                    ->end()
57✔
141
                ->end()
57✔
142
                ->arrayNode('resource_class_directories')
57✔
143
                    ->prototype('scalar')->end()
57✔
144
                ->end()
57✔
145
                ->arrayNode('serializer')
57✔
146
                    ->addDefaultsIfNotSet()
57✔
147
                    ->children()
57✔
148
                        ->booleanNode('hydra_prefix')->defaultFalse()->info('Use the "hydra:" prefix.')->end()
57✔
149
                    ->end()
57✔
150
                ->end()
57✔
151
            ->end();
57✔
152

153
        $this->addDoctrineOrmSection($rootNode);
57✔
154
        $this->addDoctrineMongoDbOdmSection($rootNode);
57✔
155
        $this->addOAuthSection($rootNode);
57✔
156
        $this->addGraphQlSection($rootNode);
57✔
157
        $this->addSwaggerSection($rootNode);
57✔
158
        $this->addHttpCacheSection($rootNode);
57✔
159
        $this->addMercureSection($rootNode);
57✔
160
        $this->addMessengerSection($rootNode);
57✔
161
        $this->addElasticsearchSection($rootNode);
57✔
162
        $this->addOpenApiSection($rootNode);
57✔
163
        $this->addMakerSection($rootNode);
57✔
164

165
        $this->addExceptionToStatusSection($rootNode);
57✔
166

167
        $this->addFormatSection($rootNode, 'formats', [
57✔
168
            'jsonld' => ['mime_types' => ['application/ld+json']]
57✔
169
        ]);
57✔
170
        $this->addFormatSection($rootNode, 'patch_formats', [
57✔
171
            'json' => ['mime_types' => ['application/merge-patch+json']],
57✔
172
        ]);
57✔
173

174
        $defaultDocFormats = [
57✔
175
            'jsonld' => ['mime_types' => ['application/ld+json']],
57✔
176
            'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']],
57✔
177
            'html' => ['mime_types' => ['text/html']],
57✔
178
        ];
57✔
179

180
        if (class_exists(Yaml::class)) {
57✔
181
            $defaultDocFormats['yamlopenapi'] = ['mime_types' => ['application/vnd.openapi+yaml']];
57✔
182
        }
183

184
        $this->addFormatSection($rootNode, 'docs_formats', $defaultDocFormats);
57✔
185

186
        $this->addFormatSection($rootNode, 'error_formats', [
57✔
187
            'jsonld' => ['mime_types' => ['application/ld+json']],
57✔
188
            'jsonproblem' => ['mime_types' => ['application/problem+json']],
57✔
189
            'json' => ['mime_types' => ['application/problem+json', 'application/json']],
57✔
190
        ]);
57✔
191
        $rootNode
57✔
192
            ->children()
57✔
193
                ->arrayNode('jsonschema_formats')
57✔
194
                    ->scalarPrototype()->end()
57✔
195
                    ->defaultValue([])
57✔
196
                    ->info('The JSON formats to compute the JSON Schemas for.')
57✔
197
                ->end()
57✔
198
            ->end();
57✔
199

200
        $this->addDefaultsSection($rootNode);
57✔
201

202
        return $treeBuilder;
57✔
203
    }
204

205
    private function addDoctrineOrmSection(ArrayNodeDefinition $rootNode): void
206
    {
207
        $rootNode
57✔
208
            ->children()
57✔
209
                ->arrayNode('doctrine')
57✔
210
                    ->{class_exists(DoctrineBundle::class) && interface_exists(EntityManagerInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
211
                ->end()
57✔
212
            ->end();
57✔
213
    }
214

215
    private function addDoctrineMongoDbOdmSection(ArrayNodeDefinition $rootNode): void
216
    {
217
        $rootNode
57✔
218
            ->children()
57✔
219
                ->arrayNode('doctrine_mongodb_odm')
57✔
220
                    ->{class_exists(DoctrineMongoDBBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
221
                ->end()
57✔
222
            ->end();
57✔
223
    }
224

225
    private function addOAuthSection(ArrayNodeDefinition $rootNode): void
226
    {
227
        $rootNode
57✔
228
            ->children()
57✔
229
                ->arrayNode('oauth')
57✔
230
                    ->canBeEnabled()
57✔
231
                    ->addDefaultsIfNotSet()
57✔
232
                    ->children()
57✔
233
                        ->scalarNode('clientId')->defaultValue('')->info('The oauth client id.')->end()
57✔
234
                        ->scalarNode('clientSecret')
57✔
235
                            ->defaultValue('')
57✔
236
                            ->info('The OAuth client secret. Never use this parameter in your production environment. It exposes crucial security information. This feature is intended for dev/test environments only. Enable "oauth.pkce" instead')
57✔
237
                        ->end()
57✔
238
                        ->booleanNode('pkce')->defaultFalse()->info('Enable the oauth PKCE.')->end()
57✔
239
                        ->scalarNode('type')->defaultValue('oauth2')->info('The oauth type.')->end()
57✔
240
                        ->scalarNode('flow')->defaultValue('application')->info('The oauth flow grant type.')->end()
57✔
241
                        ->scalarNode('tokenUrl')->defaultValue('')->info('The oauth token url.')->end()
57✔
242
                        ->scalarNode('authorizationUrl')->defaultValue('')->info('The oauth authentication url.')->end()
57✔
243
                        ->scalarNode('refreshUrl')->defaultValue('')->info('The oauth refresh url.')->end()
57✔
244
                        ->arrayNode('scopes')
57✔
245
                            ->prototype('scalar')->end()
57✔
246
                        ->end()
57✔
247
                    ->end()
57✔
248
                ->end()
57✔
249
            ->end();
57✔
250
    }
251

252
    private function addGraphQlSection(ArrayNodeDefinition $rootNode): void
253
    {
254
        $rootNode
57✔
255
            ->children()
57✔
256
                ->arrayNode('graphql')
57✔
257
                    ->{class_exists(GraphQL::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
258
                    ->addDefaultsIfNotSet()
57✔
259
                    ->children()
57✔
260
                        ->scalarNode('default_ide')->defaultValue('graphiql')->end()
57✔
261
                        ->arrayNode('graphiql')
57✔
262
                            ->{class_exists(GraphQL::class) && class_exists(TwigBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
263
                        ->end()
57✔
264
                        ->arrayNode('graphql_playground')
57✔
265
                            ->{class_exists(GraphQL::class) && class_exists(TwigBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
266
                        ->end()
57✔
267
                        ->arrayNode('introspection')
57✔
268
                            ->canBeDisabled()
57✔
269
                        ->end()
57✔
270
                        ->scalarNode('nesting_separator')->defaultValue('_')->info('The separator to use to filter nested fields.')->end()
57✔
271
                        ->arrayNode('collection')
57✔
272
                            ->addDefaultsIfNotSet()
57✔
273
                            ->children()
57✔
274
                                ->arrayNode('pagination')
57✔
275
                                    ->canBeDisabled()
57✔
276
                                ->end()
57✔
277
                            ->end()
57✔
278
                        ->end()
57✔
279
                    ->end()
57✔
280
                ->end()
57✔
281
            ->end();
57✔
282
    }
283

284
    private function addSwaggerSection(ArrayNodeDefinition $rootNode): void
285
    {
286
        $supportedVersions = [3];
57✔
287

288
        $rootNode
57✔
289
            ->children()
57✔
290
                ->arrayNode('swagger')
57✔
291
                    ->addDefaultsIfNotSet()
57✔
292
                    ->children()
57✔
293
                        ->arrayNode('versions')
57✔
294
                            ->info('The active versions of OpenAPI to be exported or used in Swagger UI. The first value is the default.')
57✔
295
                            ->defaultValue($supportedVersions)
57✔
296
                            ->beforeNormalization()
57✔
297
                                ->always(static function ($v): array {
57✔
298
                                    if (!\is_array($v)) {
6✔
UNCOV
299
                                        $v = [$v];
×
300
                                    }
301

302
                                    foreach ($v as &$version) {
6✔
303
                                        $version = (int) $version;
3✔
304
                                    }
305

306
                                    return $v;
6✔
307
                                })
57✔
308
                            ->end()
57✔
309
                            ->validate()
57✔
310
                                ->ifTrue(static fn ($v): bool => $v !== array_intersect($v, $supportedVersions))
57✔
311
                                ->thenInvalid(sprintf('Only the versions %s are supported. Got %s.', implode(' and ', $supportedVersions), '%s'))
57✔
312
                            ->end()
57✔
313
                            ->prototype('scalar')->end()
57✔
314
                        ->end()
57✔
315
                        ->arrayNode('api_keys')
57✔
316
                            ->useAttributeAsKey('key')
57✔
317
                            ->validate()
57✔
318
                                ->ifTrue(static fn ($v): bool => (bool) array_filter(array_keys($v), fn ($item) => !preg_match('/^[a-zA-Z0-9._-]+$/', $item)))
57✔
319
                                ->thenInvalid('The api keys "key" is not valid according to the pattern enforced by OpenAPI 3.1 ^[a-zA-Z0-9._-]+$.')
57✔
320
                            ->end()
57✔
321
                            ->prototype('array')
57✔
322
                                ->children()
57✔
323
                                    ->scalarNode('name')
57✔
324
                                        ->info('The name of the header or query parameter containing the api key.')
57✔
325
                                    ->end()
57✔
326
                                    ->enumNode('type')
57✔
327
                                        ->info('Whether the api key should be a query parameter or a header.')
57✔
328
                                        ->values(['query', 'header'])
57✔
329
                                    ->end()
57✔
330
                                ->end()
57✔
331
                            ->end()
57✔
332
                        ->end()
57✔
333
                        ->variableNode('swagger_ui_extra_configuration')
57✔
334
                            ->defaultValue([])
57✔
335
                            ->validate()
57✔
336
                                ->ifTrue(static fn ($v): bool => false === \is_array($v))
57✔
337
                                ->thenInvalid('The swagger_ui_extra_configuration parameter must be an array.')
57✔
338
                            ->end()
57✔
339
                            ->info('To pass extra configuration to Swagger UI, like docExpansion or filter.')
57✔
340
                        ->end()
57✔
341
                    ->end()
57✔
342
                ->end()
57✔
343
            ->end();
57✔
344
    }
345

346
    private function addHttpCacheSection(ArrayNodeDefinition $rootNode): void
347
    {
348
        $rootNode
57✔
349
            ->children()
57✔
350
                ->arrayNode('http_cache')
57✔
351
                    ->addDefaultsIfNotSet()
57✔
352
                    ->children()
57✔
353
                        ->booleanNode('public')->defaultNull()->info('To make all responses public by default.')->end()
57✔
354
                        ->arrayNode('invalidation')
57✔
355
                            ->info('Enable the tags-based cache invalidation system.')
57✔
356
                            ->canBeEnabled()
57✔
357
                            ->children()
57✔
358
                                ->arrayNode('varnish_urls')
57✔
359
                                    ->setDeprecated('api-platform/core', '3.0', 'The "varnish_urls" configuration is deprecated, use "urls" or "scoped_clients".')
57✔
360
                                    ->defaultValue([])
57✔
361
                                    ->prototype('scalar')->end()
57✔
362
                                    ->info('URLs of the Varnish servers to purge using cache tags when a resource is updated.')
57✔
363
                                ->end()
57✔
364
                                ->arrayNode('urls')
57✔
365
                                    ->defaultValue([])
57✔
366
                                    ->prototype('scalar')->end()
57✔
367
                                    ->info('URLs of the Varnish servers to purge using cache tags when a resource is updated.')
57✔
368
                                ->end()
57✔
369
                                ->arrayNode('scoped_clients')
57✔
370
                                    ->defaultValue([])
57✔
371
                                    ->prototype('scalar')->end()
57✔
372
                                    ->info('Service names of scoped client to use by the cache purger.')
57✔
373
                                ->end()
57✔
374
                                ->integerNode('max_header_length')
57✔
375
                                    ->defaultValue(7500)
57✔
376
                                    ->info('Max header length supported by the cache server.')
57✔
377
                                ->end()
57✔
378
                                ->variableNode('request_options')
57✔
379
                                    ->defaultValue([])
57✔
380
                                    ->validate()
57✔
381
                                        ->ifTrue(static fn ($v): bool => false === \is_array($v))
57✔
382
                                        ->thenInvalid('The request_options parameter must be an array.')
57✔
383
                                    ->end()
57✔
384
                                    ->info('To pass options to the client charged with the request.')
57✔
385
                                ->end()
57✔
386
                                ->scalarNode('purger')
57✔
387
                                    ->defaultValue('api_platform.http_cache.purger.varnish')
57✔
388
                                    ->info('Specify a purger to use (available values: "api_platform.http_cache.purger.varnish.ban", "api_platform.http_cache.purger.varnish.xkey", "api_platform.http_cache.purger.souin").')
57✔
389
                                ->end()
57✔
390
                                ->arrayNode('xkey')
57✔
391
                                    ->setDeprecated('api-platform/core', '3.0', 'The "xkey" configuration is deprecated, use your own purger to customize surrogate keys or the appropriate paramters.')
57✔
392
                                    ->addDefaultsIfNotSet()
57✔
393
                                    ->children()
57✔
394
                                        ->scalarNode('glue')
57✔
395
                                        ->defaultValue(' ')
57✔
396
                                        ->info('xkey glue between keys')
57✔
397
                                        ->end()
57✔
398
                                    ->end()
57✔
399
                                ->end()
57✔
400
                            ->end()
57✔
401
                        ->end()
57✔
402
                    ->end()
57✔
403
                ->end()
57✔
404
            ->end();
57✔
405
    }
406

407
    private function addMercureSection(ArrayNodeDefinition $rootNode): void
408
    {
409
        $rootNode
57✔
410
            ->children()
57✔
411
                ->arrayNode('mercure')
57✔
412
                    ->{class_exists(MercureBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
413
                    ->children()
57✔
414
                        ->scalarNode('hub_url')
57✔
415
                            ->defaultNull()
57✔
416
                            ->info('The URL sent in the Link HTTP header. If not set, will default to the URL for MercureBundle\'s default hub.')
57✔
417
                        ->end()
57✔
418
                        ->booleanNode('include_type')
57✔
419
                            ->defaultFalse()
57✔
420
                            ->info('Always include @type in updates (including delete ones).')
57✔
421
                        ->end()
57✔
422
                    ->end()
57✔
423
                ->end()
57✔
424
            ->end();
57✔
425
    }
426

427
    private function addMessengerSection(ArrayNodeDefinition $rootNode): void
428
    {
429
        $rootNode
57✔
430
            ->children()
57✔
431
                ->arrayNode('messenger')
57✔
432
                    ->{!class_exists(FullStack::class) && interface_exists(MessageBusInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
433
                ->end()
57✔
434
            ->end();
57✔
435
    }
436

437
    private function addElasticsearchSection(ArrayNodeDefinition $rootNode): void
438
    {
439
        $rootNode
57✔
440
            ->children()
57✔
441
                ->arrayNode('elasticsearch')
57✔
442
                    ->canBeEnabled()
57✔
443
                    ->addDefaultsIfNotSet()
57✔
444
                    ->children()
57✔
445
                        ->booleanNode('enabled')
57✔
446
                            ->defaultFalse()
57✔
447
                            ->validate()
57✔
448
                                ->ifTrue()
57✔
449
                                ->then(static function (bool $v): bool {
57✔
450
                                    if (!(class_exists(\Elasticsearch\Client::class) || class_exists(\Elastic\Elasticsearch\Client::class))) {
3✔
UNCOV
451
                                        throw new InvalidConfigurationException('The elasticsearch/elasticsearch package is required for Elasticsearch support.');
×
452
                                    }
453

454
                                    return $v;
3✔
455
                                })
57✔
456
                            ->end()
57✔
457
                        ->end()
57✔
458
                        ->arrayNode('hosts')
57✔
459
                            ->beforeNormalization()->castToArray()->end()
57✔
460
                            ->defaultValue([])
57✔
461
                            ->prototype('scalar')->end()
57✔
462
                        ->end()
57✔
463
                    ->end()
57✔
464
                ->end()
57✔
465
            ->end();
57✔
466
    }
467

468
    private function addOpenApiSection(ArrayNodeDefinition $rootNode): void
469
    {
470
        $rootNode
57✔
471
            ->children()
57✔
472
                ->arrayNode('openapi')
57✔
473
                    ->addDefaultsIfNotSet()
57✔
474
                        ->children()
57✔
475
                        ->arrayNode('contact')
57✔
476
                        ->addDefaultsIfNotSet()
57✔
477
                            ->children()
57✔
478
                                ->scalarNode('name')->defaultNull()->info('The identifying name of the contact person/organization.')->end()
57✔
479
                                ->scalarNode('url')->defaultNull()->info('The URL pointing to the contact information. MUST be in the format of a URL.')->end()
57✔
480
                                ->scalarNode('email')->defaultNull()->info('The email address of the contact person/organization. MUST be in the format of an email address.')->end()
57✔
481
                            ->end()
57✔
482
                        ->end()
57✔
483
                        ->scalarNode('termsOfService')->defaultNull()->info('A URL to the Terms of Service for the API. MUST be in the format of a URL.')->end()
57✔
484
                        ->arrayNode('license')
57✔
485
                        ->addDefaultsIfNotSet()
57✔
486
                            ->children()
57✔
487
                                ->scalarNode('name')->defaultNull()->info('The license name used for the API.')->end()
57✔
488
                                ->scalarNode('url')->defaultNull()->info('URL to the license used for the API. MUST be in the format of a URL.')->end()
57✔
489
                            ->end()
57✔
490
                        ->end()
57✔
491
                        ->variableNode('swagger_ui_extra_configuration')
57✔
492
                            ->defaultValue([])
57✔
493
                            ->validate()
57✔
494
                                ->ifTrue(static fn ($v): bool => false === \is_array($v))
57✔
495
                                ->thenInvalid('The swagger_ui_extra_configuration parameter must be an array.')
57✔
496
                            ->end()
57✔
497
                            ->info('To pass extra configuration to Swagger UI, like docExpansion or filter.')
57✔
498
                        ->end()
57✔
499
                        ->booleanNode('overrideResponses')->defaultTrue()->info('Whether API Platform adds automatic responses to the OpenAPI documentation.')
57✔
500
                    ->end()
57✔
501
                ->end()
57✔
502
            ->end();
57✔
503
    }
504

505
    /**
506
     * @throws InvalidConfigurationException
507
     */
508
    private function addExceptionToStatusSection(ArrayNodeDefinition $rootNode): void
509
    {
510
        $rootNode
57✔
511
            ->children()
57✔
512
                ->arrayNode('exception_to_status')
57✔
513
                    ->defaultValue([
57✔
514
                        SerializerExceptionInterface::class => Response::HTTP_BAD_REQUEST,
57✔
515
                        InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
57✔
516
                        OptimisticLockException::class => Response::HTTP_CONFLICT,
57✔
517
                    ])
57✔
518
                    ->info('The list of exceptions mapped to their HTTP status code.')
57✔
519
                    ->normalizeKeys(false)
57✔
520
                    ->useAttributeAsKey('exception_class')
57✔
521
                    ->prototype('integer')->end()
57✔
522
                    ->validate()
57✔
523
                        ->ifArray()
57✔
524
                        ->then(static function (array $exceptionToStatus): array {
57✔
525
                            foreach ($exceptionToStatus as $httpStatusCode) {
15✔
526
                                if ($httpStatusCode < 100 || $httpStatusCode >= 600) {
15✔
527
                                    throw new InvalidConfigurationException(sprintf('The HTTP status code "%s" is not valid.', $httpStatusCode));
12✔
528
                                }
529
                            }
530

531
                            return $exceptionToStatus;
3✔
532
                        })
57✔
533
                    ->end()
57✔
534
                ->end()
57✔
535
            ->end();
57✔
536
    }
537

538
    private function addFormatSection(ArrayNodeDefinition $rootNode, string $key, array $defaultValue): void
539
    {
540
        $rootNode
57✔
541
            ->children()
57✔
542
                ->arrayNode($key)
57✔
543
                    ->defaultValue($defaultValue)
57✔
544
                    ->info('The list of enabled formats. The first one will be the default.')
57✔
545
                    ->normalizeKeys(false)
57✔
546
                    ->useAttributeAsKey('format')
57✔
547
                    ->beforeNormalization()
57✔
548
                        ->ifArray()
57✔
549
                        ->then(static function ($v) {
57✔
550
                            foreach ($v as $format => $value) {
3✔
551
                                if (isset($value['mime_types'])) {
3✔
UNCOV
552
                                    continue;
×
553
                                }
554

555
                                $v[$format] = ['mime_types' => $value];
3✔
556
                            }
557

558
                            return $v;
3✔
559
                        })
57✔
560
                    ->end()
57✔
561
                    ->prototype('array')
57✔
562
                        ->children()
57✔
563
                            ->arrayNode('mime_types')->prototype('scalar')->end()->end()
57✔
564
                        ->end()
57✔
565
                    ->end()
57✔
566
                ->end()
57✔
567
            ->end();
57✔
568
    }
569

570
    private function addDefaultsSection(ArrayNodeDefinition $rootNode): void
571
    {
572
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();
57✔
573
        $defaultsNode = $rootNode->children()->arrayNode('defaults');
57✔
574

575
        $defaultsNode
57✔
576
            ->ignoreExtraKeys(false)
57✔
577
            ->beforeNormalization()
57✔
578
            ->always(static function (array $defaults) use ($nameConverter): array {
57✔
579
                $normalizedDefaults = [];
3✔
580
                foreach ($defaults as $option => $value) {
3✔
581
                    $option = $nameConverter->normalize($option);
3✔
582
                    $normalizedDefaults[$option] = $value;
3✔
583
                }
584

585
                return $normalizedDefaults;
3✔
586
            });
57✔
587

588
        $this->defineDefault($defaultsNode, new \ReflectionClass(ApiResource::class), $nameConverter);
57✔
589
        $this->defineDefault($defaultsNode, new \ReflectionClass(Put::class), $nameConverter);
57✔
590
        $this->defineDefault($defaultsNode, new \ReflectionClass(Post::class), $nameConverter);
57✔
591
    }
592

593
    private function addMakerSection(ArrayNodeDefinition $rootNode): void
594
    {
595
        $rootNode
57✔
596
            ->children()
57✔
597
                ->arrayNode('maker')
57✔
598
                    ->{class_exists(MakerBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
599
                ->end()
57✔
600
            ->end();
57✔
601
    }
602

603
    private function defineDefault(ArrayNodeDefinition $defaultsNode, \ReflectionClass $reflectionClass, CamelCaseToSnakeCaseNameConverter $nameConverter): void
604
    {
605
        foreach ($reflectionClass->getConstructor()->getParameters() as $parameter) {
57✔
606
            $defaultsNode->children()->variableNode($nameConverter->normalize($parameter->getName()));
57✔
607
        }
608
    }
609
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc