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

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

0 of 387 new or added lines in 28 files covered. (0.0%)

11394 existing lines in 372 files now uncovered.

0 of 51334 relevant lines covered (0.0%)

0.0 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\Symfony\Controller\MainController;
22
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
23
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
24
use Doctrine\ORM\EntityManagerInterface;
25
use Doctrine\ORM\OptimisticLockException;
26
use GraphQL\GraphQL;
27
use Symfony\Bundle\FullStack;
28
use Symfony\Bundle\MakerBundle\MakerBundle;
29
use Symfony\Bundle\MercureBundle\MercureBundle;
30
use Symfony\Bundle\TwigBundle\TwigBundle;
31
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
32
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
33
use Symfony\Component\Config\Definition\ConfigurationInterface;
34
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
35
use Symfony\Component\HttpFoundation\Response;
36
use Symfony\Component\Messenger\MessageBusInterface;
37
use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface;
38
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
39
use Symfony\Component\Yaml\Yaml;
40

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

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

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

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

UNCOV
168
        $this->addExceptionToStatusSection($rootNode);
×
169

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

UNCOV
177
        $defaultDocFormats = [
×
UNCOV
178
            'jsonld' => ['mime_types' => ['application/ld+json']],
×
UNCOV
179
            'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']],
×
UNCOV
180
            'html' => ['mime_types' => ['text/html']],
×
UNCOV
181
        ];
×
182

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

UNCOV
187
        $this->addFormatSection($rootNode, 'docs_formats', $defaultDocFormats);
×
188

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

UNCOV
203
        $this->addDefaultsSection($rootNode);
×
204

UNCOV
205
        return $treeBuilder;
×
206
    }
207

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

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

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

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

291
    private function addSwaggerSection(ArrayNodeDefinition $rootNode): void
292
    {
UNCOV
293
        $supportedVersions = [3];
×
294

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

UNCOV
310
                                    foreach ($v as &$version) {
×
UNCOV
311
                                        $version = (int) $version;
×
312
                                    }
313

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

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

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

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

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

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

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

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

UNCOV
574
                            return $exceptionToStatus;
×
UNCOV
575
                        })
×
UNCOV
576
                    ->end()
×
UNCOV
577
                ->end()
×
UNCOV
578
            ->end();
×
579
    }
580

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

UNCOV
598
                                $v[$format] = ['mime_types' => $value];
×
599
                            }
600

UNCOV
601
                            return $v;
×
UNCOV
602
                        })
×
UNCOV
603
                    ->end()
×
UNCOV
604
                    ->prototype('array')
×
UNCOV
605
                        ->children()
×
UNCOV
606
                            ->arrayNode('mime_types')->prototype('scalar')->end()->end()
×
UNCOV
607
                        ->end()
×
UNCOV
608
                    ->end()
×
UNCOV
609
                ->end()
×
UNCOV
610
            ->end();
×
611
    }
612

613
    private function addDefaultsSection(ArrayNodeDefinition $rootNode): void
614
    {
UNCOV
615
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();
×
UNCOV
616
        $defaultsNode = $rootNode->children()->arrayNode('defaults');
×
617

UNCOV
618
        $defaultsNode
×
UNCOV
619
            ->ignoreExtraKeys(false)
×
UNCOV
620
            ->beforeNormalization()
×
UNCOV
621
            ->always(static function (array $defaults) use ($nameConverter): array {
×
UNCOV
622
                $normalizedDefaults = [];
×
UNCOV
623
                foreach ($defaults as $option => $value) {
×
UNCOV
624
                    $option = $nameConverter->normalize($option);
×
UNCOV
625
                    $normalizedDefaults[$option] = $value;
×
626
                }
627

UNCOV
628
                return $normalizedDefaults;
×
UNCOV
629
            });
×
630

UNCOV
631
        $this->defineDefault($defaultsNode, new \ReflectionClass(ApiResource::class), $nameConverter);
×
UNCOV
632
        $this->defineDefault($defaultsNode, new \ReflectionClass(Put::class), $nameConverter);
×
UNCOV
633
        $this->defineDefault($defaultsNode, new \ReflectionClass(Post::class), $nameConverter);
×
634
    }
635

636
    private function addMakerSection(ArrayNodeDefinition $rootNode): void
637
    {
UNCOV
638
        $rootNode
×
UNCOV
639
            ->children()
×
UNCOV
640
                ->arrayNode('maker')
×
UNCOV
641
                    ->{class_exists(MakerBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
×
UNCOV
642
                ->end()
×
UNCOV
643
            ->end();
×
644
    }
645

646
    private function defineDefault(ArrayNodeDefinition $defaultsNode, \ReflectionClass $reflectionClass, CamelCaseToSnakeCaseNameConverter $nameConverter): void
647
    {
UNCOV
648
        foreach ($reflectionClass->getConstructor()->getParameters() as $parameter) {
×
UNCOV
649
            $defaultsNode->children()->variableNode($nameConverter->normalize($parameter->getName()));
×
650
        }
651
    }
652
}
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