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

api-platform / core / 18223414080

03 Oct 2025 01:18PM UTC coverage: 0.0% (-22.0%) from 21.956%
18223414080

Pull #7397

github

web-flow
Merge 69d085182 into 0b8237918
Pull Request #7397: fix(jsonschema/jsonld): make `@id` and `@type` properties required only in the JSON-LD schema for output

0 of 18 new or added lines in 2 files covered. (0.0%)

12304 existing lines in 405 files now uncovered.

0 of 53965 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\FrameworkBundle\Controller\ControllerHelper;
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\JsonStreamer\JsonStreamWriter;
38
use Symfony\Component\Messenger\MessageBusInterface;
39
use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface;
40
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
41
use Symfony\Component\Yaml\Yaml;
42

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

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

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

UNCOV
162
        $this->addDoctrineOrmSection($rootNode);
×
UNCOV
163
        $this->addDoctrineMongoDbOdmSection($rootNode);
×
UNCOV
164
        $this->addOAuthSection($rootNode);
×
UNCOV
165
        $this->addGraphQlSection($rootNode);
×
UNCOV
166
        $this->addSwaggerSection($rootNode);
×
UNCOV
167
        $this->addHttpCacheSection($rootNode);
×
UNCOV
168
        $this->addMercureSection($rootNode);
×
UNCOV
169
        $this->addMessengerSection($rootNode);
×
UNCOV
170
        $this->addElasticsearchSection($rootNode);
×
UNCOV
171
        $this->addOpenApiSection($rootNode);
×
UNCOV
172
        $this->addMakerSection($rootNode);
×
173

UNCOV
174
        $this->addExceptionToStatusSection($rootNode);
×
175

UNCOV
176
        $this->addFormatSection($rootNode, 'formats', [
×
UNCOV
177
            'jsonld' => ['mime_types' => ['application/ld+json']],
×
UNCOV
178
        ]);
×
UNCOV
179
        $this->addFormatSection($rootNode, 'patch_formats', [
×
UNCOV
180
            'json' => ['mime_types' => ['application/merge-patch+json']],
×
UNCOV
181
        ]);
×
182

UNCOV
183
        $defaultDocFormats = [
×
UNCOV
184
            'jsonld' => ['mime_types' => ['application/ld+json']],
×
UNCOV
185
            'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']],
×
UNCOV
186
            'html' => ['mime_types' => ['text/html']],
×
UNCOV
187
        ];
×
188

UNCOV
189
        if (class_exists(Yaml::class)) {
×
UNCOV
190
            $defaultDocFormats['yamlopenapi'] = ['mime_types' => ['application/vnd.openapi+yaml']];
×
191
        }
192

UNCOV
193
        $this->addFormatSection($rootNode, 'docs_formats', $defaultDocFormats);
×
194

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

UNCOV
209
        $this->addDefaultsSection($rootNode);
×
210

UNCOV
211
        return $treeBuilder;
×
212
    }
213

214
    private function addDoctrineOrmSection(ArrayNodeDefinition $rootNode): void
215
    {
UNCOV
216
        $rootNode
×
UNCOV
217
            ->children()
×
UNCOV
218
                ->arrayNode('doctrine')
×
UNCOV
219
                    ->{class_exists(DoctrineBundle::class) && interface_exists(EntityManagerInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
×
UNCOV
220
                ->end()
×
UNCOV
221
            ->end();
×
222
    }
223

224
    private function addDoctrineMongoDbOdmSection(ArrayNodeDefinition $rootNode): void
225
    {
UNCOV
226
        $rootNode
×
UNCOV
227
            ->children()
×
UNCOV
228
                ->arrayNode('doctrine_mongodb_odm')
×
UNCOV
229
                    ->{class_exists(DoctrineMongoDBBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
×
UNCOV
230
                ->end()
×
UNCOV
231
            ->end();
×
232
    }
233

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

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

297
    private function addSwaggerSection(ArrayNodeDefinition $rootNode): void
298
    {
UNCOV
299
        $supportedVersions = [3];
×
300

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

UNCOV
316
                                    foreach ($v as &$version) {
×
UNCOV
317
                                        $version = (int) $version;
×
318
                                    }
319

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

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

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

459
    private function addMessengerSection(ArrayNodeDefinition $rootNode): void
460
    {
UNCOV
461
        $rootNode
×
UNCOV
462
            ->children()
×
UNCOV
463
                ->arrayNode('messenger')
×
UNCOV
464
                    ->{!class_exists(FullStack::class) && interface_exists(MessageBusInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
×
UNCOV
465
                ->end()
×
UNCOV
466
            ->end();
×
467
    }
468

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

UNCOV
491
                                    return $v;
×
UNCOV
492
                                })
×
UNCOV
493
                            ->end()
×
UNCOV
494
                        ->end()
×
UNCOV
495
                        ->arrayNode('hosts')
×
UNCOV
496
                            ->beforeNormalization()->castToArray()->end()
×
UNCOV
497
                            ->defaultValue([])
×
UNCOV
498
                            ->prototype('scalar')->end()
×
UNCOV
499
                        ->end()
×
UNCOV
500
                    ->end()
×
UNCOV
501
                ->end()
×
UNCOV
502
            ->end();
×
503
    }
504

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

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

UNCOV
580
                            return $exceptionToStatus;
×
UNCOV
581
                        })
×
UNCOV
582
                    ->end()
×
UNCOV
583
                ->end()
×
UNCOV
584
            ->end();
×
585
    }
586

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

UNCOV
604
                                $v[$format] = ['mime_types' => $value];
×
605
                            }
606

UNCOV
607
                            return $v;
×
UNCOV
608
                        })
×
UNCOV
609
                    ->end()
×
UNCOV
610
                    ->prototype('array')
×
UNCOV
611
                        ->children()
×
UNCOV
612
                            ->arrayNode('mime_types')->prototype('scalar')->end()->end()
×
UNCOV
613
                        ->end()
×
UNCOV
614
                    ->end()
×
UNCOV
615
                ->end()
×
UNCOV
616
            ->end();
×
617
    }
618

619
    private function addDefaultsSection(ArrayNodeDefinition $rootNode): void
620
    {
UNCOV
621
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();
×
UNCOV
622
        $defaultsNode = $rootNode->children()->arrayNode('defaults');
×
623

UNCOV
624
        $defaultsNode
×
UNCOV
625
            ->ignoreExtraKeys(false)
×
UNCOV
626
            ->beforeNormalization()
×
UNCOV
627
            ->always(static function (array $defaults) use ($nameConverter): array {
×
UNCOV
628
                $normalizedDefaults = [];
×
UNCOV
629
                foreach ($defaults as $option => $value) {
×
UNCOV
630
                    $option = $nameConverter->normalize($option);
×
UNCOV
631
                    $normalizedDefaults[$option] = $value;
×
632
                }
633

UNCOV
634
                return $normalizedDefaults;
×
UNCOV
635
            });
×
636

UNCOV
637
        $this->defineDefault($defaultsNode, new \ReflectionClass(ApiResource::class), $nameConverter);
×
UNCOV
638
        $this->defineDefault($defaultsNode, new \ReflectionClass(Put::class), $nameConverter);
×
UNCOV
639
        $this->defineDefault($defaultsNode, new \ReflectionClass(Post::class), $nameConverter);
×
640
    }
641

642
    private function addMakerSection(ArrayNodeDefinition $rootNode): void
643
    {
UNCOV
644
        $rootNode
×
UNCOV
645
            ->children()
×
UNCOV
646
                ->arrayNode('maker')
×
UNCOV
647
                    ->{class_exists(MakerBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
×
UNCOV
648
                ->end()
×
UNCOV
649
            ->end();
×
650
    }
651

652
    private function defineDefault(ArrayNodeDefinition $defaultsNode, \ReflectionClass $reflectionClass, CamelCaseToSnakeCaseNameConverter $nameConverter): void
653
    {
UNCOV
654
        foreach ($reflectionClass->getConstructor()->getParameters() as $parameter) {
×
UNCOV
655
            $defaultsNode->children()->variableNode($nameConverter->normalize($parameter->getName()));
×
656
        }
657
    }
658
}
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