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

api-platform / core / 14532422117

18 Apr 2025 08:39AM UTC coverage: 22.324% (+13.8%) from 8.488%
14532422117

push

github

web-flow
ci: patch phpunit deprecations inside component (#7103)

10745 of 48132 relevant lines covered (22.32%)

30.5 hits per line

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

0.0
/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');
×
56
        $rootNode = $treeBuilder->getRootNode();
×
57

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

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

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

165
        $this->addExceptionToStatusSection($rootNode);
×
166

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

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

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

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

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

200
        $this->addDefaultsSection($rootNode);
×
201

202
        return $treeBuilder;
×
203
    }
204

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

622
                return $normalizedDefaults;
×
623
            });
×
624

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

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

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

© 2025 Coveralls, Inc