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

api-platform / core / 14726067612

29 Apr 2025 07:47AM UTC coverage: 23.443% (+15.2%) from 8.252%
14726067612

push

github

web-flow
feat(symfony): Autoconfigure classes using `#[ApiResource]` attribute (#6943)

0 of 12 new or added lines in 4 files covered. (0.0%)

3578 existing lines in 159 files now uncovered.

11517 of 49127 relevant lines covered (23.44%)

54.29 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
    {
UNCOV
55
        $treeBuilder = new TreeBuilder('api_platform');
×
UNCOV
56
        $rootNode = $treeBuilder->getRootNode();
×
57

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

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

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

UNCOV
166
        $this->addExceptionToStatusSection($rootNode);
×
167

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

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

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

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

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

UNCOV
201
        $this->addDefaultsSection($rootNode);
×
202

UNCOV
203
        return $treeBuilder;
×
204
    }
205

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
623
                return $normalizedDefaults;
×
UNCOV
624
            });
×
625

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

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

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