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

api-platform / core / 10014117656

19 Jul 2024 08:44PM UTC coverage: 7.856% (-56.3%) from 64.185%
10014117656

push

github

soyuka
Merge branch 'sf/remove-flag'

0 of 527 new or added lines in 83 files covered. (0.0%)

10505 existing lines in 362 files now uncovered.

12705 of 161727 relevant lines covered (7.86%)

26.85 hits per line

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

99.33
/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\Elasticsearch\State\Options;
18
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19
use ApiPlatform\Metadata\ApiResource;
20
use ApiPlatform\Metadata\Post;
21
use ApiPlatform\Metadata\Put;
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

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');
57✔
UNCOV
55
        $rootNode = $treeBuilder->getRootNode();
57✔
56

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

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

UNCOV
146
        $this->addDoctrineOrmSection($rootNode);
57✔
UNCOV
147
        $this->addDoctrineMongoDbOdmSection($rootNode);
57✔
UNCOV
148
        $this->addOAuthSection($rootNode);
57✔
UNCOV
149
        $this->addGraphQlSection($rootNode);
57✔
UNCOV
150
        $this->addSwaggerSection($rootNode);
57✔
UNCOV
151
        $this->addHttpCacheSection($rootNode);
57✔
UNCOV
152
        $this->addMercureSection($rootNode);
57✔
UNCOV
153
        $this->addMessengerSection($rootNode);
57✔
UNCOV
154
        $this->addElasticsearchSection($rootNode);
57✔
UNCOV
155
        $this->addOpenApiSection($rootNode);
57✔
UNCOV
156
        $this->addMakerSection($rootNode);
57✔
157

UNCOV
158
        $this->addExceptionToStatusSection($rootNode);
57✔
159

UNCOV
160
        $this->addFormatSection($rootNode, 'formats', [
57✔
UNCOV
161
        ]);
57✔
UNCOV
162
        $this->addFormatSection($rootNode, 'patch_formats', [
57✔
UNCOV
163
            'json' => ['mime_types' => ['application/merge-patch+json']],
57✔
UNCOV
164
        ]);
57✔
UNCOV
165
        $this->addFormatSection($rootNode, 'docs_formats', [
57✔
UNCOV
166
            'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']],
57✔
UNCOV
167
            'yamlopenapi' => ['mime_types' => ['application/vnd.openapi+yaml']],
57✔
UNCOV
168
            'jsonld' => ['mime_types' => ['application/ld+json']],
57✔
UNCOV
169
            'html' => ['mime_types' => ['text/html']],
57✔
UNCOV
170
        ]);
57✔
UNCOV
171
        $this->addFormatSection($rootNode, 'error_formats', [
57✔
UNCOV
172
            'jsonld' => ['mime_types' => ['application/ld+json']],
57✔
UNCOV
173
            'jsonproblem' => ['mime_types' => ['application/problem+json']],
57✔
UNCOV
174
            'json' => ['mime_types' => ['application/problem+json', 'application/json']],
57✔
UNCOV
175
        ]);
57✔
UNCOV
176
        $rootNode
57✔
UNCOV
177
            ->children()
57✔
UNCOV
178
                ->arrayNode('jsonschema_formats')
57✔
UNCOV
179
                    ->scalarPrototype()->end()
57✔
UNCOV
180
                    ->defaultValue([])
57✔
UNCOV
181
                    ->info('The JSON formats to compute the JSON Schemas for.')
57✔
UNCOV
182
                ->end()
57✔
UNCOV
183
            ->end();
57✔
184

UNCOV
185
        $this->addDefaultsSection($rootNode);
57✔
186

UNCOV
187
        return $treeBuilder;
57✔
188
    }
189

190
    private function addDoctrineOrmSection(ArrayNodeDefinition $rootNode): void
191
    {
UNCOV
192
        $rootNode
57✔
UNCOV
193
            ->children()
57✔
UNCOV
194
                ->arrayNode('doctrine')
57✔
UNCOV
195
                    ->{class_exists(DoctrineBundle::class) && interface_exists(EntityManagerInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
UNCOV
196
                ->end()
57✔
UNCOV
197
            ->end();
57✔
198
    }
199

200
    private function addDoctrineMongoDbOdmSection(ArrayNodeDefinition $rootNode): void
201
    {
UNCOV
202
        $rootNode
57✔
UNCOV
203
            ->children()
57✔
UNCOV
204
                ->arrayNode('doctrine_mongodb_odm')
57✔
UNCOV
205
                    ->{class_exists(DoctrineMongoDBBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
UNCOV
206
                ->end()
57✔
UNCOV
207
            ->end();
57✔
208
    }
209

210
    private function addOAuthSection(ArrayNodeDefinition $rootNode): void
211
    {
UNCOV
212
        $rootNode
57✔
UNCOV
213
            ->children()
57✔
UNCOV
214
                ->arrayNode('oauth')
57✔
UNCOV
215
                    ->canBeEnabled()
57✔
UNCOV
216
                    ->addDefaultsIfNotSet()
57✔
UNCOV
217
                    ->children()
57✔
UNCOV
218
                        ->scalarNode('clientId')->defaultValue('')->info('The oauth client id.')->end()
57✔
UNCOV
219
                        ->scalarNode('clientSecret')
57✔
UNCOV
220
                            ->defaultValue('')
57✔
UNCOV
221
                            ->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')
57✔
UNCOV
222
                        ->end()
57✔
UNCOV
223
                        ->booleanNode('pkce')->defaultFalse()->info('Enable the oauth PKCE.')->end()
57✔
UNCOV
224
                        ->scalarNode('type')->defaultValue('oauth2')->info('The oauth type.')->end()
57✔
UNCOV
225
                        ->scalarNode('flow')->defaultValue('application')->info('The oauth flow grant type.')->end()
57✔
UNCOV
226
                        ->scalarNode('tokenUrl')->defaultValue('')->info('The oauth token url.')->end()
57✔
UNCOV
227
                        ->scalarNode('authorizationUrl')->defaultValue('')->info('The oauth authentication url.')->end()
57✔
UNCOV
228
                        ->scalarNode('refreshUrl')->defaultValue('')->info('The oauth refresh url.')->end()
57✔
UNCOV
229
                        ->arrayNode('scopes')
57✔
UNCOV
230
                            ->prototype('scalar')->end()
57✔
UNCOV
231
                        ->end()
57✔
UNCOV
232
                    ->end()
57✔
UNCOV
233
                ->end()
57✔
UNCOV
234
            ->end();
57✔
235
    }
236

237
    private function addGraphQlSection(ArrayNodeDefinition $rootNode): void
238
    {
UNCOV
239
        $rootNode
57✔
UNCOV
240
            ->children()
57✔
UNCOV
241
                ->arrayNode('graphql')
57✔
UNCOV
242
                    ->{class_exists(GraphQL::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
UNCOV
243
                    ->addDefaultsIfNotSet()
57✔
UNCOV
244
                    ->children()
57✔
UNCOV
245
                        ->scalarNode('default_ide')->defaultValue('graphiql')->end()
57✔
UNCOV
246
                        ->arrayNode('graphiql')
57✔
UNCOV
247
                            ->{class_exists(GraphQL::class) && class_exists(TwigBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
UNCOV
248
                        ->end()
57✔
UNCOV
249
                        ->arrayNode('graphql_playground')
57✔
UNCOV
250
                            ->{class_exists(GraphQL::class) && class_exists(TwigBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
UNCOV
251
                        ->end()
57✔
UNCOV
252
                        ->arrayNode('introspection')
57✔
UNCOV
253
                            ->canBeDisabled()
57✔
UNCOV
254
                        ->end()
57✔
UNCOV
255
                        ->scalarNode('nesting_separator')->defaultValue('_')->info('The separator to use to filter nested fields.')->end()
57✔
UNCOV
256
                        ->arrayNode('collection')
57✔
UNCOV
257
                            ->addDefaultsIfNotSet()
57✔
UNCOV
258
                            ->children()
57✔
UNCOV
259
                                ->arrayNode('pagination')
57✔
UNCOV
260
                                    ->canBeDisabled()
57✔
UNCOV
261
                                ->end()
57✔
UNCOV
262
                            ->end()
57✔
UNCOV
263
                        ->end()
57✔
UNCOV
264
                    ->end()
57✔
UNCOV
265
                ->end()
57✔
UNCOV
266
            ->end();
57✔
267
    }
268

269
    private function addSwaggerSection(ArrayNodeDefinition $rootNode): void
270
    {
UNCOV
271
        $supportedVersions = [3];
57✔
272

UNCOV
273
        $rootNode
57✔
UNCOV
274
            ->children()
57✔
UNCOV
275
                ->arrayNode('swagger')
57✔
UNCOV
276
                    ->addDefaultsIfNotSet()
57✔
UNCOV
277
                    ->children()
57✔
UNCOV
278
                        ->arrayNode('versions')
57✔
UNCOV
279
                            ->info('The active versions of OpenAPI to be exported or used in Swagger UI. The first value is the default.')
57✔
UNCOV
280
                            ->defaultValue($supportedVersions)
57✔
UNCOV
281
                            ->beforeNormalization()
57✔
UNCOV
282
                                ->always(static function ($v): array {
57✔
283
                                    if (!\is_array($v)) {
6✔
UNCOV
284
                                        $v = [$v];
×
285
                                    }
286

UNCOV
287
                                    foreach ($v as &$version) {
6✔
UNCOV
288
                                        $version = (int) $version;
3✔
289
                                    }
290

UNCOV
291
                                    return $v;
6✔
UNCOV
292
                                })
57✔
UNCOV
293
                            ->end()
57✔
UNCOV
294
                            ->validate()
57✔
UNCOV
295
                                ->ifTrue(static fn ($v): bool => $v !== array_intersect($v, $supportedVersions))
57✔
UNCOV
296
                                ->thenInvalid(sprintf('Only the versions %s are supported. Got %s.', implode(' and ', $supportedVersions), '%s'))
57✔
UNCOV
297
                            ->end()
57✔
UNCOV
298
                            ->prototype('scalar')->end()
57✔
UNCOV
299
                        ->end()
57✔
UNCOV
300
                        ->arrayNode('api_keys')
57✔
UNCOV
301
                            ->useAttributeAsKey('key')
57✔
UNCOV
302
                            ->validate()
57✔
UNCOV
303
                                ->ifTrue(static fn ($v): bool => (bool) array_filter(array_keys($v), fn ($item) => !preg_match('/^[a-zA-Z0-9._-]+$/', $item)))
57✔
UNCOV
304
                                ->thenInvalid('The api keys "key" is not valid according to the pattern enforced by OpenAPI 3.1 ^[a-zA-Z0-9._-]+$.')
57✔
UNCOV
305
                            ->end()
57✔
UNCOV
306
                            ->prototype('array')
57✔
UNCOV
307
                                ->children()
57✔
UNCOV
308
                                    ->scalarNode('name')
57✔
UNCOV
309
                                        ->info('The name of the header or query parameter containing the api key.')
57✔
UNCOV
310
                                    ->end()
57✔
UNCOV
311
                                    ->enumNode('type')
57✔
UNCOV
312
                                        ->info('Whether the api key should be a query parameter or a header.')
57✔
UNCOV
313
                                        ->values(['query', 'header'])
57✔
UNCOV
314
                                    ->end()
57✔
UNCOV
315
                                ->end()
57✔
UNCOV
316
                            ->end()
57✔
UNCOV
317
                        ->end()
57✔
UNCOV
318
                        ->variableNode('swagger_ui_extra_configuration')
57✔
UNCOV
319
                            ->defaultValue([])
57✔
UNCOV
320
                            ->validate()
57✔
UNCOV
321
                                ->ifTrue(static fn ($v): bool => false === \is_array($v))
57✔
UNCOV
322
                                ->thenInvalid('The swagger_ui_extra_configuration parameter must be an array.')
57✔
UNCOV
323
                            ->end()
57✔
UNCOV
324
                            ->info('To pass extra configuration to Swagger UI, like docExpansion or filter.')
57✔
UNCOV
325
                        ->end()
57✔
UNCOV
326
                    ->end()
57✔
UNCOV
327
                ->end()
57✔
UNCOV
328
            ->end();
57✔
329
    }
330

331
    private function addHttpCacheSection(ArrayNodeDefinition $rootNode): void
332
    {
UNCOV
333
        $rootNode
57✔
UNCOV
334
            ->children()
57✔
UNCOV
335
                ->arrayNode('http_cache')
57✔
UNCOV
336
                    ->addDefaultsIfNotSet()
57✔
UNCOV
337
                    ->children()
57✔
UNCOV
338
                        ->booleanNode('public')->defaultNull()->info('To make all responses public by default.')->end()
57✔
UNCOV
339
                        ->arrayNode('invalidation')
57✔
UNCOV
340
                            ->info('Enable the tags-based cache invalidation system.')
57✔
UNCOV
341
                            ->canBeEnabled()
57✔
UNCOV
342
                            ->children()
57✔
UNCOV
343
                                ->arrayNode('varnish_urls')
57✔
UNCOV
344
                                    ->setDeprecated('api-platform/core', '3.0', 'The "varnish_urls" configuration is deprecated, use "urls" or "scoped_clients".')
57✔
UNCOV
345
                                    ->defaultValue([])
57✔
UNCOV
346
                                    ->prototype('scalar')->end()
57✔
UNCOV
347
                                    ->info('URLs of the Varnish servers to purge using cache tags when a resource is updated.')
57✔
UNCOV
348
                                ->end()
57✔
UNCOV
349
                                ->arrayNode('urls')
57✔
UNCOV
350
                                    ->defaultValue([])
57✔
UNCOV
351
                                    ->prototype('scalar')->end()
57✔
UNCOV
352
                                    ->info('URLs of the Varnish servers to purge using cache tags when a resource is updated.')
57✔
UNCOV
353
                                ->end()
57✔
UNCOV
354
                                ->arrayNode('scoped_clients')
57✔
UNCOV
355
                                    ->defaultValue([])
57✔
UNCOV
356
                                    ->prototype('scalar')->end()
57✔
UNCOV
357
                                    ->info('Service names of scoped client to use by the cache purger.')
57✔
UNCOV
358
                                ->end()
57✔
UNCOV
359
                                ->integerNode('max_header_length')
57✔
UNCOV
360
                                    ->defaultValue(7500)
57✔
UNCOV
361
                                    ->info('Max header length supported by the cache server.')
57✔
UNCOV
362
                                ->end()
57✔
UNCOV
363
                                ->variableNode('request_options')
57✔
UNCOV
364
                                    ->defaultValue([])
57✔
UNCOV
365
                                    ->validate()
57✔
UNCOV
366
                                        ->ifTrue(static fn ($v): bool => false === \is_array($v))
57✔
UNCOV
367
                                        ->thenInvalid('The request_options parameter must be an array.')
57✔
UNCOV
368
                                    ->end()
57✔
UNCOV
369
                                    ->info('To pass options to the client charged with the request.')
57✔
UNCOV
370
                                ->end()
57✔
UNCOV
371
                                ->scalarNode('purger')
57✔
UNCOV
372
                                    ->defaultValue('api_platform.http_cache.purger.varnish')
57✔
UNCOV
373
                                    ->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").')
57✔
UNCOV
374
                                ->end()
57✔
UNCOV
375
                                ->arrayNode('xkey')
57✔
UNCOV
376
                                    ->setDeprecated('api-platform/core', '3.0', 'The "xkey" configuration is deprecated, use your own purger to customize surrogate keys or the appropriate paramters.')
57✔
UNCOV
377
                                    ->addDefaultsIfNotSet()
57✔
UNCOV
378
                                    ->children()
57✔
UNCOV
379
                                        ->scalarNode('glue')
57✔
UNCOV
380
                                        ->defaultValue(' ')
57✔
UNCOV
381
                                        ->info('xkey glue between keys')
57✔
UNCOV
382
                                        ->end()
57✔
UNCOV
383
                                    ->end()
57✔
UNCOV
384
                                ->end()
57✔
UNCOV
385
                            ->end()
57✔
UNCOV
386
                        ->end()
57✔
UNCOV
387
                    ->end()
57✔
UNCOV
388
                ->end()
57✔
UNCOV
389
            ->end();
57✔
390
    }
391

392
    private function addMercureSection(ArrayNodeDefinition $rootNode): void
393
    {
UNCOV
394
        $rootNode
57✔
UNCOV
395
            ->children()
57✔
UNCOV
396
                ->arrayNode('mercure')
57✔
UNCOV
397
                    ->{class_exists(MercureBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
UNCOV
398
                    ->children()
57✔
UNCOV
399
                        ->scalarNode('hub_url')
57✔
UNCOV
400
                            ->defaultNull()
57✔
UNCOV
401
                            ->info('The URL sent in the Link HTTP header. If not set, will default to the URL for MercureBundle\'s default hub.')
57✔
UNCOV
402
                        ->end()
57✔
UNCOV
403
                        ->booleanNode('include_type')
57✔
UNCOV
404
                            ->defaultFalse()
57✔
UNCOV
405
                            ->info('Always include @type in updates (including delete ones).')
57✔
UNCOV
406
                        ->end()
57✔
UNCOV
407
                    ->end()
57✔
UNCOV
408
                ->end()
57✔
UNCOV
409
            ->end();
57✔
410
    }
411

412
    private function addMessengerSection(ArrayNodeDefinition $rootNode): void
413
    {
UNCOV
414
        $rootNode
57✔
UNCOV
415
            ->children()
57✔
UNCOV
416
                ->arrayNode('messenger')
57✔
UNCOV
417
                    ->{!class_exists(FullStack::class) && interface_exists(MessageBusInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
UNCOV
418
                ->end()
57✔
UNCOV
419
            ->end();
57✔
420
    }
421

422
    private function addElasticsearchSection(ArrayNodeDefinition $rootNode): void
423
    {
UNCOV
424
        $rootNode
57✔
UNCOV
425
            ->children()
57✔
UNCOV
426
                ->arrayNode('elasticsearch')
57✔
UNCOV
427
                    ->canBeEnabled()
57✔
UNCOV
428
                    ->addDefaultsIfNotSet()
57✔
UNCOV
429
                    ->children()
57✔
UNCOV
430
                        ->booleanNode('enabled')
57✔
UNCOV
431
                            ->defaultFalse()
57✔
UNCOV
432
                            ->validate()
57✔
UNCOV
433
                                ->ifTrue()
57✔
UNCOV
434
                                ->then(static function (bool $v): bool {
57✔
435
                                    if (!(class_exists(\Elasticsearch\Client::class) || class_exists(\Elastic\Elasticsearch\Client::class))) {
3✔
UNCOV
436
                                        throw new InvalidConfigurationException('The elasticsearch/elasticsearch package is required for Elasticsearch support.');
×
437
                                    }
438

UNCOV
439
                                    return $v;
3✔
UNCOV
440
                                })
57✔
UNCOV
441
                            ->end()
57✔
UNCOV
442
                        ->end()
57✔
UNCOV
443
                        ->arrayNode('hosts')
57✔
UNCOV
444
                            ->beforeNormalization()->castToArray()->end()
57✔
UNCOV
445
                            ->defaultValue([])
57✔
UNCOV
446
                            ->prototype('scalar')->end()
57✔
UNCOV
447
                        ->end()
57✔
UNCOV
448
                    ->end()
57✔
UNCOV
449
                ->end()
57✔
UNCOV
450
            ->end();
57✔
451
    }
452

453
    private function addOpenApiSection(ArrayNodeDefinition $rootNode): void
454
    {
UNCOV
455
        $rootNode
57✔
UNCOV
456
            ->children()
57✔
UNCOV
457
                ->arrayNode('openapi')
57✔
UNCOV
458
                    ->addDefaultsIfNotSet()
57✔
UNCOV
459
                        ->children()
57✔
UNCOV
460
                        ->arrayNode('contact')
57✔
UNCOV
461
                        ->addDefaultsIfNotSet()
57✔
UNCOV
462
                            ->children()
57✔
UNCOV
463
                                ->scalarNode('name')->defaultNull()->info('The identifying name of the contact person/organization.')->end()
57✔
UNCOV
464
                                ->scalarNode('url')->defaultNull()->info('The URL pointing to the contact information. MUST be in the format of a URL.')->end()
57✔
UNCOV
465
                                ->scalarNode('email')->defaultNull()->info('The email address of the contact person/organization. MUST be in the format of an email address.')->end()
57✔
UNCOV
466
                            ->end()
57✔
UNCOV
467
                        ->end()
57✔
UNCOV
468
                        ->scalarNode('termsOfService')->defaultNull()->info('A URL to the Terms of Service for the API. MUST be in the format of a URL.')->end()
57✔
UNCOV
469
                        ->arrayNode('license')
57✔
UNCOV
470
                        ->addDefaultsIfNotSet()
57✔
UNCOV
471
                            ->children()
57✔
UNCOV
472
                                ->scalarNode('name')->defaultNull()->info('The license name used for the API.')->end()
57✔
UNCOV
473
                                ->scalarNode('url')->defaultNull()->info('URL to the license used for the API. MUST be in the format of a URL.')->end()
57✔
UNCOV
474
                            ->end()
57✔
UNCOV
475
                        ->end()
57✔
UNCOV
476
                        ->variableNode('swagger_ui_extra_configuration')
57✔
UNCOV
477
                            ->defaultValue([])
57✔
UNCOV
478
                            ->validate()
57✔
UNCOV
479
                                ->ifTrue(static fn ($v): bool => false === \is_array($v))
57✔
UNCOV
480
                                ->thenInvalid('The swagger_ui_extra_configuration parameter must be an array.')
57✔
UNCOV
481
                            ->end()
57✔
UNCOV
482
                            ->info('To pass extra configuration to Swagger UI, like docExpansion or filter.')
57✔
UNCOV
483
                        ->end()
57✔
UNCOV
484
                        ->booleanNode('overrideResponses')->defaultTrue()->info('Whether API Platform adds automatic responses to the OpenAPI documentation.')
57✔
UNCOV
485
                    ->end()
57✔
UNCOV
486
                ->end()
57✔
UNCOV
487
            ->end();
57✔
488
    }
489

490
    /**
491
     * @throws InvalidConfigurationException
492
     */
493
    private function addExceptionToStatusSection(ArrayNodeDefinition $rootNode): void
494
    {
UNCOV
495
        $rootNode
57✔
UNCOV
496
            ->children()
57✔
UNCOV
497
                ->arrayNode('exception_to_status')
57✔
UNCOV
498
                    ->defaultValue([
57✔
UNCOV
499
                        SerializerExceptionInterface::class => Response::HTTP_BAD_REQUEST,
57✔
UNCOV
500
                        InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
57✔
UNCOV
501
                        OptimisticLockException::class => Response::HTTP_CONFLICT,
57✔
UNCOV
502
                    ])
57✔
UNCOV
503
                    ->info('The list of exceptions mapped to their HTTP status code.')
57✔
UNCOV
504
                    ->normalizeKeys(false)
57✔
UNCOV
505
                    ->useAttributeAsKey('exception_class')
57✔
UNCOV
506
                    ->prototype('integer')->end()
57✔
UNCOV
507
                    ->validate()
57✔
UNCOV
508
                        ->ifArray()
57✔
UNCOV
509
                        ->then(static function (array $exceptionToStatus): array {
57✔
UNCOV
510
                            foreach ($exceptionToStatus as $httpStatusCode) {
15✔
UNCOV
511
                                if ($httpStatusCode < 100 || $httpStatusCode >= 600) {
15✔
UNCOV
512
                                    throw new InvalidConfigurationException(sprintf('The HTTP status code "%s" is not valid.', $httpStatusCode));
12✔
513
                                }
514
                            }
515

UNCOV
516
                            return $exceptionToStatus;
3✔
UNCOV
517
                        })
57✔
UNCOV
518
                    ->end()
57✔
UNCOV
519
                ->end()
57✔
UNCOV
520
            ->end();
57✔
521
    }
522

523
    private function addFormatSection(ArrayNodeDefinition $rootNode, string $key, array $defaultValue): void
524
    {
UNCOV
525
        $rootNode
57✔
UNCOV
526
            ->children()
57✔
UNCOV
527
                ->arrayNode($key)
57✔
UNCOV
528
                    ->defaultValue($defaultValue)
57✔
UNCOV
529
                    ->info('The list of enabled formats. The first one will be the default.')
57✔
UNCOV
530
                    ->normalizeKeys(false)
57✔
UNCOV
531
                    ->useAttributeAsKey('format')
57✔
UNCOV
532
                    ->beforeNormalization()
57✔
UNCOV
533
                        ->ifArray()
57✔
UNCOV
534
                        ->then(static function ($v) {
57✔
UNCOV
535
                            foreach ($v as $format => $value) {
3✔
UNCOV
536
                                if (isset($value['mime_types'])) {
3✔
UNCOV
537
                                    continue;
×
538
                                }
539

UNCOV
540
                                $v[$format] = ['mime_types' => $value];
3✔
541
                            }
542

UNCOV
543
                            return $v;
3✔
UNCOV
544
                        })
57✔
UNCOV
545
                    ->end()
57✔
UNCOV
546
                    ->prototype('array')
57✔
UNCOV
547
                        ->children()
57✔
UNCOV
548
                            ->arrayNode('mime_types')->prototype('scalar')->end()->end()
57✔
UNCOV
549
                        ->end()
57✔
UNCOV
550
                    ->end()
57✔
UNCOV
551
                ->end()
57✔
UNCOV
552
            ->end();
57✔
553
    }
554

555
    private function addDefaultsSection(ArrayNodeDefinition $rootNode): void
556
    {
UNCOV
557
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();
57✔
UNCOV
558
        $defaultsNode = $rootNode->children()->arrayNode('defaults');
57✔
559

UNCOV
560
        $defaultsNode
57✔
UNCOV
561
            ->ignoreExtraKeys(false)
57✔
UNCOV
562
            ->beforeNormalization()
57✔
UNCOV
563
            ->always(static function (array $defaults) use ($nameConverter): array {
57✔
UNCOV
564
                $normalizedDefaults = [];
3✔
UNCOV
565
                foreach ($defaults as $option => $value) {
3✔
UNCOV
566
                    $option = $nameConverter->normalize($option);
3✔
UNCOV
567
                    $normalizedDefaults[$option] = $value;
3✔
568
                }
569

UNCOV
570
                return $normalizedDefaults;
3✔
UNCOV
571
            });
57✔
572

UNCOV
573
        $this->defineDefault($defaultsNode, new \ReflectionClass(ApiResource::class), $nameConverter);
57✔
UNCOV
574
        $this->defineDefault($defaultsNode, new \ReflectionClass(Put::class), $nameConverter);
57✔
UNCOV
575
        $this->defineDefault($defaultsNode, new \ReflectionClass(Post::class), $nameConverter);
57✔
576
    }
577

578
    private function addMakerSection(ArrayNodeDefinition $rootNode): void
579
    {
UNCOV
580
        $rootNode
57✔
UNCOV
581
            ->children()
57✔
UNCOV
582
                ->arrayNode('maker')
57✔
UNCOV
583
                    ->{class_exists(MakerBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
57✔
UNCOV
584
                ->end()
57✔
UNCOV
585
            ->end();
57✔
586
    }
587

588
    private function defineDefault(ArrayNodeDefinition $defaultsNode, \ReflectionClass $reflectionClass, CamelCaseToSnakeCaseNameConverter $nameConverter): void
589
    {
UNCOV
590
        foreach ($reflectionClass->getConstructor()->getParameters() as $parameter) {
57✔
UNCOV
591
            $defaultsNode->children()->variableNode($nameConverter->normalize($parameter->getName()));
57✔
592
        }
593
    }
594
}
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