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

api-platform / core / 15186284751

22 May 2025 12:14PM UTC coverage: 27.348%. Remained the same
15186284751

push

github

soyuka
docs: changelog 4.1.10

13482 of 49298 relevant lines covered (27.35%)

74.57 hits per line

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

99.39
/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\Metadata\ApiResource;
18
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19
use ApiPlatform\Metadata\Post;
20
use ApiPlatform\Metadata\Put;
21
use ApiPlatform\OpenApi\Model\Tag;
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');
42✔
56
        $rootNode = $treeBuilder->getRootNode();
42✔
57

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

66
                    return $v;
2✔
67
                })
42✔
68
            ->end()
42✔
69
            ->children()
42✔
70
                ->scalarNode('title')
42✔
71
                    ->info('The title of the API.')
42✔
72
                    ->cannotBeEmpty()
42✔
73
                    ->defaultValue('')
42✔
74
                ->end()
42✔
75
                ->scalarNode('description')
42✔
76
                    ->info('The description of the API.')
42✔
77
                    ->cannotBeEmpty()
42✔
78
                    ->defaultValue('')
42✔
79
                ->end()
42✔
80
                ->scalarNode('version')
42✔
81
                    ->info('The version of the API.')
42✔
82
                    ->cannotBeEmpty()
42✔
83
                    ->defaultValue('0.0.0')
42✔
84
                ->end()
42✔
85
                ->booleanNode('show_webby')->defaultTrue()->info('If true, show Webby on the documentation page')->end()
42✔
86
                ->booleanNode('use_symfony_listeners')->defaultFalse()->info(sprintf('Uses Symfony event listeners instead of the %s.', MainController::class))->end()
42✔
87
                ->scalarNode('name_converter')->defaultNull()->info('Specify a name converter to use.')->end()
42✔
88
                ->scalarNode('asset_package')->defaultNull()->info('Specify an asset package name to use.')->end()
42✔
89
                ->scalarNode('path_segment_name_generator')->defaultValue('api_platform.metadata.path_segment_name_generator.underscore')->info('Specify a path name generator to use.')->end()
42✔
90
                ->scalarNode('inflector')->defaultValue('api_platform.metadata.inflector')->info('Specify an inflector to use.')->end()
42✔
91
                ->arrayNode('validator')
42✔
92
                    ->addDefaultsIfNotSet()
42✔
93
                    ->children()
42✔
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()
42✔
95
                        ->booleanNode('query_parameter_validation')->defaultValue(true)->end()
42✔
96
                    ->end()
42✔
97
                ->end()
42✔
98
                ->arrayNode('eager_loading')
42✔
99
                    ->canBeDisabled()
42✔
100
                    ->addDefaultsIfNotSet()
42✔
101
                    ->children()
42✔
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()
42✔
103
                        ->integerNode('max_joins')->defaultValue(30)->info('Max number of joined relations before EagerLoading throws a RuntimeException')->end()
42✔
104
                        ->booleanNode('force_eager')->defaultTrue()->info('Force join on every relation. If disabled, it will only join relations having the EAGER fetch mode.')->end()
42✔
105
                    ->end()
42✔
106
                ->end()
42✔
107
                ->booleanNode('handle_symfony_errors')->defaultFalse()->info('Allows to handle symfony exceptions.')->end()
42✔
108
                ->booleanNode('enable_swagger')->defaultTrue()->info('Enable the Swagger documentation and export.')->end()
42✔
109
                ->booleanNode('enable_swagger_ui')->defaultValue(class_exists(TwigBundle::class))->info('Enable Swagger UI')->end()
42✔
110
                ->booleanNode('enable_re_doc')->defaultValue(class_exists(TwigBundle::class))->info('Enable ReDoc')->end()
42✔
111
                ->booleanNode('enable_entrypoint')->defaultTrue()->info('Enable the entrypoint')->end()
42✔
112
                ->booleanNode('enable_docs')->defaultTrue()->info('Enable the docs')->end()
42✔
113
                ->booleanNode('enable_profiler')->defaultTrue()->info('Enable the data collector and the WebProfilerBundle integration.')->end()
42✔
114
                ->booleanNode('enable_link_security')->defaultFalse()->info('Enable security for Links (sub resources)')->end()
42✔
115
                ->arrayNode('collection')
42✔
116
                    ->addDefaultsIfNotSet()
42✔
117
                    ->children()
42✔
118
                        ->scalarNode('exists_parameter_name')->defaultValue('exists')->cannotBeEmpty()->info('The name of the query parameter to filter on nullable field values.')->end()
42✔
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
42✔
120
                        ->scalarNode('order_parameter_name')->defaultValue('order')->cannotBeEmpty()->info('The name of the query parameter to order results.')->end()
42✔
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()
42✔
122
                        ->arrayNode('pagination')
42✔
123
                            ->canBeDisabled()
42✔
124
                            ->addDefaultsIfNotSet()
42✔
125
                            ->children()
42✔
126
                                ->scalarNode('page_parameter_name')->defaultValue('page')->cannotBeEmpty()->info('The default name of the parameter handling the page number.')->end()
42✔
127
                                ->scalarNode('enabled_parameter_name')->defaultValue('pagination')->cannotBeEmpty()->info('The name of the query parameter to enable or disable pagination.')->end()
42✔
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()
42✔
129
                                ->scalarNode('partial_parameter_name')->defaultValue('partial')->cannotBeEmpty()->info('The name of the query parameter to enable or disable partial pagination.')->end()
42✔
130
                            ->end()
42✔
131
                        ->end()
42✔
132
                    ->end()
42✔
133
                ->end()
42✔
134
                ->arrayNode('mapping')
42✔
135
                    ->addDefaultsIfNotSet()
42✔
136
                    ->children()
42✔
137
                        ->arrayNode('paths')
42✔
138
                            ->prototype('scalar')->end()
42✔
139
                        ->end()
42✔
140
                    ->end()
42✔
141
                ->end()
42✔
142
                ->arrayNode('resource_class_directories')
42✔
143
                    ->prototype('scalar')->end()
42✔
144
                ->end()
42✔
145
                ->arrayNode('serializer')
42✔
146
                    ->addDefaultsIfNotSet()
42✔
147
                    ->children()
42✔
148
                        ->booleanNode('hydra_prefix')->defaultFalse()->info('Use the "hydra:" prefix.')->end()
42✔
149
                    ->end()
42✔
150
                ->end()
42✔
151
            ->end();
42✔
152

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

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

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

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

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

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

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

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

202
        return $treeBuilder;
42✔
203
    }
204

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

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

225
    private function addOAuthSection(ArrayNodeDefinition $rootNode): void
226
    {
227
        $rootNode
42✔
228
            ->children()
42✔
229
                ->arrayNode('oauth')
42✔
230
                    ->canBeEnabled()
42✔
231
                    ->addDefaultsIfNotSet()
42✔
232
                    ->children()
42✔
233
                        ->scalarNode('clientId')->defaultValue('')->info('The oauth client id.')->end()
42✔
234
                        ->scalarNode('clientSecret')
42✔
235
                            ->defaultValue('')
42✔
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')
42✔
237
                        ->end()
42✔
238
                        ->booleanNode('pkce')->defaultFalse()->info('Enable the oauth PKCE.')->end()
42✔
239
                        ->scalarNode('type')->defaultValue('oauth2')->info('The oauth type.')->end()
42✔
240
                        ->scalarNode('flow')->defaultValue('application')->info('The oauth flow grant type.')->end()
42✔
241
                        ->scalarNode('tokenUrl')->defaultValue('')->info('The oauth token url.')->end()
42✔
242
                        ->scalarNode('authorizationUrl')->defaultValue('')->info('The oauth authentication url.')->end()
42✔
243
                        ->scalarNode('refreshUrl')->defaultValue('')->info('The oauth refresh url.')->end()
42✔
244
                        ->arrayNode('scopes')
42✔
245
                            ->prototype('scalar')->end()
42✔
246
                        ->end()
42✔
247
                    ->end()
42✔
248
                ->end()
42✔
249
            ->end();
42✔
250
    }
251

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

288
    private function addSwaggerSection(ArrayNodeDefinition $rootNode): void
289
    {
290
        $supportedVersions = [3];
42✔
291

292
        $rootNode
42✔
293
            ->children()
42✔
294
                ->arrayNode('swagger')
42✔
295
                    ->addDefaultsIfNotSet()
42✔
296
                    ->children()
42✔
297
                        ->booleanNode('persist_authorization')->defaultValue(false)->info('Persist the SwaggerUI Authorization in the localStorage.')->end()
42✔
298
                        ->arrayNode('versions')
42✔
299
                            ->info('The active versions of OpenAPI to be exported or used in Swagger UI. The first value is the default.')
42✔
300
                            ->defaultValue($supportedVersions)
42✔
301
                            ->beforeNormalization()
42✔
302
                                ->always(static function ($v): array {
42✔
303
                                    if (!\is_array($v)) {
4✔
304
                                        $v = [$v];
×
305
                                    }
306

307
                                    foreach ($v as &$version) {
4✔
308
                                        $version = (int) $version;
2✔
309
                                    }
310

311
                                    return $v;
4✔
312
                                })
42✔
313
                            ->end()
42✔
314
                            ->validate()
42✔
315
                                ->ifTrue(static fn ($v): bool => $v !== array_intersect($v, $supportedVersions))
42✔
316
                                ->thenInvalid(sprintf('Only the versions %s are supported. Got %s.', implode(' and ', $supportedVersions), '%s'))
42✔
317
                            ->end()
42✔
318
                            ->prototype('scalar')->end()
42✔
319
                        ->end()
42✔
320
                        ->arrayNode('api_keys')
42✔
321
                            ->useAttributeAsKey('key')
42✔
322
                            ->validate()
42✔
323
                                ->ifTrue(static fn ($v): bool => (bool) array_filter(array_keys($v), fn ($item) => !preg_match('/^[a-zA-Z0-9._-]+$/', $item)))
42✔
324
                                ->thenInvalid('The api keys "key" is not valid according to the pattern enforced by OpenAPI 3.1 ^[a-zA-Z0-9._-]+$.')
42✔
325
                            ->end()
42✔
326
                            ->prototype('array')
42✔
327
                                ->children()
42✔
328
                                    ->scalarNode('name')
42✔
329
                                        ->info('The name of the header or query parameter containing the api key.')
42✔
330
                                    ->end()
42✔
331
                                    ->enumNode('type')
42✔
332
                                        ->info('Whether the api key should be a query parameter or a header.')
42✔
333
                                        ->values(['query', 'header'])
42✔
334
                                    ->end()
42✔
335
                                ->end()
42✔
336
                            ->end()
42✔
337
                        ->end()
42✔
338
                        ->arrayNode('http_auth')
42✔
339
                            ->info('Creates http security schemes for OpenAPI.')
42✔
340
                            ->useAttributeAsKey('key')
42✔
341
                            ->validate()
42✔
342
                                ->ifTrue(static fn ($v): bool => (bool) array_filter(array_keys($v), fn ($item) => !preg_match('/^[a-zA-Z0-9._-]+$/', $item)))
42✔
343
                                ->thenInvalid('The api keys "key" is not valid according to the pattern enforced by OpenAPI 3.1 ^[a-zA-Z0-9._-]+$.')
42✔
344
                            ->end()
42✔
345
                            ->prototype('array')
42✔
346
                                ->children()
42✔
347
                                    ->scalarNode('scheme')
42✔
348
                                        ->info('The OpenAPI HTTP auth scheme, for example "bearer"')
42✔
349
                                    ->end()
42✔
350
                                    ->scalarNode('bearerFormat')
42✔
351
                                        ->info('The OpenAPI HTTP bearer format')
42✔
352
                                    ->end()
42✔
353
                                ->end()
42✔
354
                            ->end()
42✔
355
                        ->end()
42✔
356
                        ->variableNode('swagger_ui_extra_configuration')
42✔
357
                            ->defaultValue([])
42✔
358
                            ->validate()
42✔
359
                                ->ifTrue(static fn ($v): bool => false === \is_array($v))
42✔
360
                                ->thenInvalid('The swagger_ui_extra_configuration parameter must be an array.')
42✔
361
                            ->end()
42✔
362
                            ->info('To pass extra configuration to Swagger UI, like docExpansion or filter.')
42✔
363
                        ->end()
42✔
364
                    ->end()
42✔
365
                ->end()
42✔
366
            ->end();
42✔
367
    }
368

369
    private function addHttpCacheSection(ArrayNodeDefinition $rootNode): void
370
    {
371
        $rootNode
42✔
372
            ->children()
42✔
373
                ->arrayNode('http_cache')
42✔
374
                    ->addDefaultsIfNotSet()
42✔
375
                    ->children()
42✔
376
                        ->booleanNode('public')->defaultNull()->info('To make all responses public by default.')->end()
42✔
377
                        ->arrayNode('invalidation')
42✔
378
                            ->info('Enable the tags-based cache invalidation system.')
42✔
379
                            ->canBeEnabled()
42✔
380
                            ->children()
42✔
381
                                ->arrayNode('varnish_urls')
42✔
382
                                    ->setDeprecated('api-platform/core', '3.0', 'The "varnish_urls" configuration is deprecated, use "urls" or "scoped_clients".')
42✔
383
                                    ->defaultValue([])
42✔
384
                                    ->prototype('scalar')->end()
42✔
385
                                    ->info('URLs of the Varnish servers to purge using cache tags when a resource is updated.')
42✔
386
                                ->end()
42✔
387
                                ->arrayNode('urls')
42✔
388
                                    ->defaultValue([])
42✔
389
                                    ->prototype('scalar')->end()
42✔
390
                                    ->info('URLs of the Varnish servers to purge using cache tags when a resource is updated.')
42✔
391
                                ->end()
42✔
392
                                ->arrayNode('scoped_clients')
42✔
393
                                    ->defaultValue([])
42✔
394
                                    ->prototype('scalar')->end()
42✔
395
                                    ->info('Service names of scoped client to use by the cache purger.')
42✔
396
                                ->end()
42✔
397
                                ->integerNode('max_header_length')
42✔
398
                                    ->defaultValue(7500)
42✔
399
                                    ->info('Max header length supported by the cache server.')
42✔
400
                                ->end()
42✔
401
                                ->variableNode('request_options')
42✔
402
                                    ->defaultValue([])
42✔
403
                                    ->validate()
42✔
404
                                        ->ifTrue(static fn ($v): bool => false === \is_array($v))
42✔
405
                                        ->thenInvalid('The request_options parameter must be an array.')
42✔
406
                                    ->end()
42✔
407
                                    ->info('To pass options to the client charged with the request.')
42✔
408
                                ->end()
42✔
409
                                ->scalarNode('purger')
42✔
410
                                    ->defaultValue('api_platform.http_cache.purger.varnish')
42✔
411
                                    ->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").')
42✔
412
                                ->end()
42✔
413
                                ->arrayNode('xkey')
42✔
414
                                    ->setDeprecated('api-platform/core', '3.0', 'The "xkey" configuration is deprecated, use your own purger to customize surrogate keys or the appropriate paramters.')
42✔
415
                                    ->addDefaultsIfNotSet()
42✔
416
                                    ->children()
42✔
417
                                        ->scalarNode('glue')
42✔
418
                                        ->defaultValue(' ')
42✔
419
                                        ->info('xkey glue between keys')
42✔
420
                                        ->end()
42✔
421
                                    ->end()
42✔
422
                                ->end()
42✔
423
                            ->end()
42✔
424
                        ->end()
42✔
425
                    ->end()
42✔
426
                ->end()
42✔
427
            ->end();
42✔
428
    }
429

430
    private function addMercureSection(ArrayNodeDefinition $rootNode): void
431
    {
432
        $rootNode
42✔
433
            ->children()
42✔
434
                ->arrayNode('mercure')
42✔
435
                    ->{class_exists(MercureBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
42✔
436
                    ->children()
42✔
437
                        ->scalarNode('hub_url')
42✔
438
                            ->defaultNull()
42✔
439
                            ->info('The URL sent in the Link HTTP header. If not set, will default to the URL for MercureBundle\'s default hub.')
42✔
440
                        ->end()
42✔
441
                        ->booleanNode('include_type')
42✔
442
                            ->defaultFalse()
42✔
443
                            ->info('Always include @type in updates (including delete ones).')
42✔
444
                        ->end()
42✔
445
                    ->end()
42✔
446
                ->end()
42✔
447
            ->end();
42✔
448
    }
449

450
    private function addMessengerSection(ArrayNodeDefinition $rootNode): void
451
    {
452
        $rootNode
42✔
453
            ->children()
42✔
454
                ->arrayNode('messenger')
42✔
455
                    ->{!class_exists(FullStack::class) && interface_exists(MessageBusInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
42✔
456
                ->end()
42✔
457
            ->end();
42✔
458
    }
459

460
    private function addElasticsearchSection(ArrayNodeDefinition $rootNode): void
461
    {
462
        $rootNode
42✔
463
            ->children()
42✔
464
                ->arrayNode('elasticsearch')
42✔
465
                    ->canBeEnabled()
42✔
466
                    ->addDefaultsIfNotSet()
42✔
467
                    ->children()
42✔
468
                        ->booleanNode('enabled')
42✔
469
                            ->defaultFalse()
42✔
470
                            ->validate()
42✔
471
                                ->ifTrue()
42✔
472
                                ->then(static function (bool $v): bool {
42✔
473
                                    if (
474
                                        // ES v7
475
                                        !class_exists(\Elasticsearch\Client::class)
2✔
476
                                        // ES v8 and up
477
                                        && !class_exists(\Elastic\Elasticsearch\Client::class)
2✔
478
                                    ) {
479
                                        throw new InvalidConfigurationException('The elasticsearch/elasticsearch package is required for Elasticsearch support.');
×
480
                                    }
481

482
                                    return $v;
2✔
483
                                })
42✔
484
                            ->end()
42✔
485
                        ->end()
42✔
486
                        ->arrayNode('hosts')
42✔
487
                            ->beforeNormalization()->castToArray()->end()
42✔
488
                            ->defaultValue([])
42✔
489
                            ->prototype('scalar')->end()
42✔
490
                        ->end()
42✔
491
                    ->end()
42✔
492
                ->end()
42✔
493
            ->end();
42✔
494
    }
495

496
    private function addOpenApiSection(ArrayNodeDefinition $rootNode): void
497
    {
498
        $rootNode
42✔
499
            ->children()
42✔
500
                ->arrayNode('openapi')
42✔
501
                    ->addDefaultsIfNotSet()
42✔
502
                        ->children()
42✔
503
                        ->arrayNode('contact')
42✔
504
                        ->addDefaultsIfNotSet()
42✔
505
                            ->children()
42✔
506
                                ->scalarNode('name')->defaultNull()->info('The identifying name of the contact person/organization.')->end()
42✔
507
                                ->scalarNode('url')->defaultNull()->info('The URL pointing to the contact information. MUST be in the format of a URL.')->end()
42✔
508
                                ->scalarNode('email')->defaultNull()->info('The email address of the contact person/organization. MUST be in the format of an email address.')->end()
42✔
509
                            ->end()
42✔
510
                        ->end()
42✔
511
                        ->scalarNode('termsOfService')->defaultNull()->info('A URL to the Terms of Service for the API. MUST be in the format of a URL.')->end()
42✔
512
                        ->arrayNode('tags')
42✔
513
                            ->info('Global OpenApi tags overriding the default computed tags if specified.')
42✔
514
                            ->prototype('array')
42✔
515
                                ->children()
42✔
516
                                    ->scalarNode('name')->isRequired()->end()
42✔
517
                                    ->scalarNode('description')->defaultNull()->end()
42✔
518
                                ->end()
42✔
519
                            ->end()
42✔
520
                        ->end()
42✔
521
                        ->arrayNode('license')
42✔
522
                        ->addDefaultsIfNotSet()
42✔
523
                            ->children()
42✔
524
                                ->scalarNode('name')->defaultNull()->info('The license name used for the API.')->end()
42✔
525
                                ->scalarNode('url')->defaultNull()->info('URL to the license used for the API. MUST be in the format of a URL.')->end()
42✔
526
                            ->end()
42✔
527
                        ->end()
42✔
528
                        ->variableNode('swagger_ui_extra_configuration')
42✔
529
                            ->defaultValue([])
42✔
530
                            ->validate()
42✔
531
                                ->ifTrue(static fn ($v): bool => false === \is_array($v))
42✔
532
                                ->thenInvalid('The swagger_ui_extra_configuration parameter must be an array.')
42✔
533
                            ->end()
42✔
534
                            ->info('To pass extra configuration to Swagger UI, like docExpansion or filter.')
42✔
535
                        ->end()
42✔
536
                        ->booleanNode('overrideResponses')->defaultTrue()->info('Whether API Platform adds automatic responses to the OpenAPI documentation.')
42✔
537
                    ->end()
42✔
538
                ->end()
42✔
539
            ->end();
42✔
540
    }
541

542
    /**
543
     * @throws InvalidConfigurationException
544
     */
545
    private function addExceptionToStatusSection(ArrayNodeDefinition $rootNode): void
546
    {
547
        $rootNode
42✔
548
            ->children()
42✔
549
                ->arrayNode('exception_to_status')
42✔
550
                    ->defaultValue([
42✔
551
                        SerializerExceptionInterface::class => Response::HTTP_BAD_REQUEST,
42✔
552
                        InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
42✔
553
                        OptimisticLockException::class => Response::HTTP_CONFLICT,
42✔
554
                    ])
42✔
555
                    ->info('The list of exceptions mapped to their HTTP status code.')
42✔
556
                    ->normalizeKeys(false)
42✔
557
                    ->useAttributeAsKey('exception_class')
42✔
558
                    ->prototype('integer')->end()
42✔
559
                    ->validate()
42✔
560
                        ->ifArray()
42✔
561
                        ->then(static function (array $exceptionToStatus): array {
42✔
562
                            foreach ($exceptionToStatus as $httpStatusCode) {
10✔
563
                                if ($httpStatusCode < 100 || $httpStatusCode >= 600) {
10✔
564
                                    throw new InvalidConfigurationException(sprintf('The HTTP status code "%s" is not valid.', $httpStatusCode));
8✔
565
                                }
566
                            }
567

568
                            return $exceptionToStatus;
2✔
569
                        })
42✔
570
                    ->end()
42✔
571
                ->end()
42✔
572
            ->end();
42✔
573
    }
574

575
    private function addFormatSection(ArrayNodeDefinition $rootNode, string $key, array $defaultValue): void
576
    {
577
        $rootNode
42✔
578
            ->children()
42✔
579
                ->arrayNode($key)
42✔
580
                    ->defaultValue($defaultValue)
42✔
581
                    ->info('The list of enabled formats. The first one will be the default.')
42✔
582
                    ->normalizeKeys(false)
42✔
583
                    ->useAttributeAsKey('format')
42✔
584
                    ->beforeNormalization()
42✔
585
                        ->ifArray()
42✔
586
                        ->then(static function ($v) {
42✔
587
                            foreach ($v as $format => $value) {
2✔
588
                                if (isset($value['mime_types'])) {
2✔
589
                                    continue;
×
590
                                }
591

592
                                $v[$format] = ['mime_types' => $value];
2✔
593
                            }
594

595
                            return $v;
2✔
596
                        })
42✔
597
                    ->end()
42✔
598
                    ->prototype('array')
42✔
599
                        ->children()
42✔
600
                            ->arrayNode('mime_types')->prototype('scalar')->end()->end()
42✔
601
                        ->end()
42✔
602
                    ->end()
42✔
603
                ->end()
42✔
604
            ->end();
42✔
605
    }
606

607
    private function addDefaultsSection(ArrayNodeDefinition $rootNode): void
608
    {
609
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();
42✔
610
        $defaultsNode = $rootNode->children()->arrayNode('defaults');
42✔
611

612
        $defaultsNode
42✔
613
            ->ignoreExtraKeys(false)
42✔
614
            ->beforeNormalization()
42✔
615
            ->always(static function (array $defaults) use ($nameConverter): array {
42✔
616
                $normalizedDefaults = [];
2✔
617
                foreach ($defaults as $option => $value) {
2✔
618
                    $option = $nameConverter->normalize($option);
2✔
619
                    $normalizedDefaults[$option] = $value;
2✔
620
                }
621

622
                return $normalizedDefaults;
2✔
623
            });
42✔
624

625
        $this->defineDefault($defaultsNode, new \ReflectionClass(ApiResource::class), $nameConverter);
42✔
626
        $this->defineDefault($defaultsNode, new \ReflectionClass(Put::class), $nameConverter);
42✔
627
        $this->defineDefault($defaultsNode, new \ReflectionClass(Post::class), $nameConverter);
42✔
628
    }
629

630
    private function addMakerSection(ArrayNodeDefinition $rootNode): void
631
    {
632
        $rootNode
42✔
633
            ->children()
42✔
634
                ->arrayNode('maker')
42✔
635
                    ->{class_exists(MakerBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
42✔
636
                ->end()
42✔
637
            ->end();
42✔
638
    }
639

640
    private function defineDefault(ArrayNodeDefinition $defaultsNode, \ReflectionClass $reflectionClass, CamelCaseToSnakeCaseNameConverter $nameConverter): void
641
    {
642
        foreach ($reflectionClass->getConstructor()->getParameters() as $parameter) {
42✔
643
            $defaultsNode->children()->variableNode($nameConverter->normalize($parameter->getName()));
42✔
644
        }
645
    }
646
}
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

© 2026 Coveralls, Inc