Passed
Push — master ( dfc177...74ee7e )
by Kévin
13:45 queued 09:32
created

ApiPlatformExtension::normalizeDefaults()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 0
loc 20
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\Bridge\Symfony\Bundle\DependencyInjection;
15
16
use ApiPlatform\Core\Api\FilterInterface;
17
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationCollectionExtensionInterface;
18
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationItemExtensionInterface;
19
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\AbstractFilter as DoctrineMongoDbOdmAbstractFilter;
20
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\EagerLoadingExtension;
21
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\FilterEagerLoadingExtension;
22
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface as DoctrineQueryCollectionExtensionInterface;
23
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
24
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter as DoctrineOrmAbstractContextAwareFilter;
25
use ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\Extension\RequestBodySearchCollectionExtensionInterface;
26
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
27
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
28
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
29
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
30
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
31
use ApiPlatform\Core\GraphQl\Resolver\MutationResolverInterface;
32
use ApiPlatform\Core\GraphQl\Resolver\QueryCollectionResolverInterface;
33
use ApiPlatform\Core\GraphQl\Resolver\QueryItemResolverInterface;
34
use ApiPlatform\Core\GraphQl\Type\Definition\TypeInterface as GraphQlTypeInterface;
35
use Doctrine\Common\Annotations\Annotation;
36
use phpDocumentor\Reflection\DocBlockFactoryInterface;
37
use Ramsey\Uuid\Uuid;
38
use Symfony\Component\BrowserKit\AbstractBrowser;
39
use Symfony\Component\Cache\Adapter\ArrayAdapter;
40
use Symfony\Component\Config\FileLocator;
41
use Symfony\Component\Config\Resource\DirectoryResource;
42
use Symfony\Component\DependencyInjection\ChildDefinition;
43
use Symfony\Component\DependencyInjection\ContainerBuilder;
44
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
45
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
46
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
47
use Symfony\Component\DependencyInjection\Reference;
48
use Symfony\Component\Finder\Finder;
49
use Symfony\Component\HttpClient\HttpClientTrait;
50
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
51
use Symfony\Component\Validator\Validator\ValidatorInterface;
52
use Symfony\Component\Yaml\Yaml;
53
54
/**
55
 * The extension of this bundle.
56
 *
57
 * @author Kévin Dunglas <[email protected]>
58
 */
59
final class ApiPlatformExtension extends Extension implements PrependExtensionInterface
60
{
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function prepend(ContainerBuilder $container): void
65
    {
66
        if (isset($container->getExtensions()['framework'])) {
67
            $container->prependExtensionConfig('framework', [
68
                'serializer' => [
69
                    'enabled' => true,
70
                ],
71
            ]);
72
            $container->prependExtensionConfig('framework', [
73
                'property_info' => [
74
                    'enabled' => true,
75
                ],
76
            ]);
77
        }
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function load(array $configs, ContainerBuilder $container): void
84
    {
85
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
86
87
        $configuration = new Configuration();
88
        $config = $this->processConfiguration($configuration, $configs);
89
90
        $formats = $this->getFormats($config['formats']);
91
        $patchFormats = $this->getFormats($config['patch_formats']);
92
        $errorFormats = $this->getFormats($config['error_formats']);
93
94
        // Backward Compatibility layer
95
        if (isset($formats['jsonapi']) && !isset($patchFormats['jsonapi'])) {
96
            $patchFormats['jsonapi'] = ['application/vnd.api+json'];
97
        }
98
99
        $this->registerCommonConfiguration($container, $config, $loader, $formats, $patchFormats, $errorFormats);
100
        $this->registerMetadataConfiguration($container, $config, $loader);
101
        $this->registerOAuthConfiguration($container, $config);
102
        $this->registerSwaggerConfiguration($container, $config, $loader);
103
        $this->registerJsonApiConfiguration($formats, $loader);
104
        $this->registerJsonLdHydraConfiguration($container, $formats, $loader, $config['enable_docs']);
105
        $this->registerJsonHalConfiguration($formats, $loader);
106
        $this->registerJsonProblemConfiguration($errorFormats, $loader);
107
        $this->registerGraphQlConfiguration($container, $config, $loader);
108
        $this->registerLegacyBundlesConfiguration($container, $config, $loader);
109
        $this->registerCacheConfiguration($container);
110
        $this->registerDoctrineOrmConfiguration($container, $config, $loader);
111
        $this->registerDoctrineMongoDbOdmConfiguration($container, $config, $loader);
112
        $this->registerHttpCacheConfiguration($container, $config, $loader);
113
        $this->registerValidatorConfiguration($container, $config, $loader);
114
        $this->registerDataCollectorConfiguration($container, $config, $loader);
115
        $this->registerMercureConfiguration($container, $config, $loader);
116
        $this->registerMessengerConfiguration($container, $config, $loader);
117
        $this->registerElasticsearchConfiguration($container, $config, $loader);
118
        $this->registerDataTransformerConfiguration($container);
119
        $this->registerSecurityConfiguration($container, $loader);
120
121
        $container->registerForAutoconfiguration(DataPersisterInterface::class)
122
            ->addTag('api_platform.data_persister');
123
        $container->registerForAutoconfiguration(ItemDataProviderInterface::class)
124
            ->addTag('api_platform.item_data_provider');
125
        $container->registerForAutoconfiguration(CollectionDataProviderInterface::class)
126
            ->addTag('api_platform.collection_data_provider');
127
        $container->registerForAutoconfiguration(SubresourceDataProviderInterface::class)
128
            ->addTag('api_platform.subresource_data_provider');
129
        $container->registerForAutoconfiguration(FilterInterface::class)
130
            ->addTag('api_platform.filter');
131
132
        if ($container->hasParameter('test.client.parameters')) {
133
            $loader->load('test.xml');
134
135
            if (!class_exists(AbstractBrowser::class) || !trait_exists(HttpClientTrait::class)) {
136
                $container->removeDefinition('test.api_platform.client');
137
            }
138
        }
139
    }
140
141
    private function registerCommonConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader, array $formats, array $patchFormats, array $errorFormats): void
142
    {
143
        $loader->load('api.xml');
144
        $loader->load('data_persister.xml');
145
        $loader->load('data_provider.xml');
146
        $loader->load('filter.xml');
147
148
        if (class_exists(Uuid::class)) {
149
            $loader->load('ramsey_uuid.xml');
150
        }
151
152
        $container->setParameter('api_platform.enable_entrypoint', $config['enable_entrypoint']);
153
        $container->setParameter('api_platform.enable_docs', $config['enable_docs']);
154
        $container->setParameter('api_platform.title', $config['title']);
155
        $container->setParameter('api_platform.description', $config['description']);
156
        $container->setParameter('api_platform.version', $config['version']);
157
        $container->setParameter('api_platform.show_webby', $config['show_webby']);
158
        $container->setParameter('api_platform.exception_to_status', $config['exception_to_status']);
159
        $container->setParameter('api_platform.formats', $formats);
160
        $container->setParameter('api_platform.patch_formats', $patchFormats);
161
        $container->setParameter('api_platform.error_formats', $errorFormats);
162
        $container->setParameter('api_platform.allow_plain_identifiers', $config['allow_plain_identifiers']);
163
        $container->setParameter('api_platform.eager_loading.enabled', $this->isConfigEnabled($container, $config['eager_loading']));
164
        $container->setParameter('api_platform.eager_loading.max_joins', $config['eager_loading']['max_joins']);
165
        $container->setParameter('api_platform.eager_loading.fetch_partial', $config['eager_loading']['fetch_partial']);
166
        $container->setParameter('api_platform.eager_loading.force_eager', $config['eager_loading']['force_eager']);
167
        $container->setParameter('api_platform.collection.exists_parameter_name', $config['collection']['exists_parameter_name']);
168
        $container->setParameter('api_platform.collection.order', $config['collection']['order']);
169
        $container->setParameter('api_platform.collection.order_parameter_name', $config['collection']['order_parameter_name']);
170
        $container->setParameter('api_platform.collection.pagination.enabled', $this->isConfigEnabled($container, $config['collection']['pagination']));
171
        $container->setParameter('api_platform.collection.pagination.partial', $config['collection']['pagination']['partial']);
172
        $container->setParameter('api_platform.collection.pagination.client_enabled', $config['collection']['pagination']['client_enabled']);
173
        $container->setParameter('api_platform.collection.pagination.client_items_per_page', $config['collection']['pagination']['client_items_per_page']);
174
        $container->setParameter('api_platform.collection.pagination.client_partial', $config['collection']['pagination']['client_partial']);
175
        $container->setParameter('api_platform.collection.pagination.items_per_page', $config['collection']['pagination']['items_per_page']);
176
        $container->setParameter('api_platform.collection.pagination.maximum_items_per_page', $config['collection']['pagination']['maximum_items_per_page']);
177
        $container->setParameter('api_platform.collection.pagination.page_parameter_name', $config['collection']['pagination']['page_parameter_name']);
178
        $container->setParameter('api_platform.collection.pagination.enabled_parameter_name', $config['collection']['pagination']['enabled_parameter_name']);
179
        $container->setParameter('api_platform.collection.pagination.items_per_page_parameter_name', $config['collection']['pagination']['items_per_page_parameter_name']);
180
        $container->setParameter('api_platform.collection.pagination.partial_parameter_name', $config['collection']['pagination']['partial_parameter_name']);
181
        $container->setParameter('api_platform.collection.pagination', $config['collection']['pagination']);
182
        $container->setParameter('api_platform.http_cache.etag', $config['http_cache']['etag']);
183
        $container->setParameter('api_platform.http_cache.max_age', $config['http_cache']['max_age']);
184
        $container->setParameter('api_platform.http_cache.shared_max_age', $config['http_cache']['shared_max_age']);
185
        $container->setParameter('api_platform.http_cache.vary', $config['http_cache']['vary']);
186
        $container->setParameter('api_platform.http_cache.public', $config['http_cache']['public']);
187
188
        $container->setAlias('api_platform.operation_path_resolver.default', $config['default_operation_path_resolver']);
189
        $container->setAlias('api_platform.path_segment_name_generator', $config['path_segment_name_generator']);
190
191
        if ($config['name_converter']) {
192
            $container->setAlias('api_platform.name_converter', $config['name_converter']);
193
        }
194
        $container->setParameter('api_platform.defaults', $this->normalizeDefaults($config['defaults'] ?? []));
195
    }
196
197
    private function normalizeDefaults(array $defaults): array
198
    {
199
        $normalizedDefaults = ['attributes' => []];
200
        $rootLevelOptions = [
201
            'description',
202
            'iri',
203
            'item_operations',
204
            'collection_operations',
205
            'graphql',
206
        ];
207
208
        foreach ($defaults as $option => $value) {
209
            if (\in_array($option, $rootLevelOptions, true)) {
210
                $normalizedDefaults[$option] = $value;
211
            } else {
212
                $normalizedDefaults['attributes'][$option] = $value;
213
            }
214
        }
215
216
        return $normalizedDefaults;
217
    }
218
219
    private function registerMetadataConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
220
    {
221
        $loader->load('metadata/metadata.xml');
222
        $loader->load('metadata/xml.xml');
223
224
        [$xmlResources, $yamlResources] = $this->getResourcesToWatch($container, $config);
225
226
        if (!empty($config['resource_class_directories'])) {
227
            $container->setParameter('api_platform.resource_class_directories', array_merge(
228
                $config['resource_class_directories'], $container->getParameter('api_platform.resource_class_directories')
229
            ));
230
        }
231
232
        $container->getDefinition('api_platform.metadata.extractor.xml')->replaceArgument(0, $xmlResources);
233
234
        if (class_exists(Annotation::class)) {
235
            $loader->load('metadata/annotation.xml');
236
        }
237
238
        if (class_exists(Yaml::class)) {
239
            $loader->load('metadata/yaml.xml');
240
            $container->getDefinition('api_platform.metadata.extractor.yaml')->replaceArgument(0, $yamlResources);
241
        }
242
243
        if (interface_exists(DocBlockFactoryInterface::class)) {
244
            $loader->load('metadata/php_doc.xml');
245
        }
246
    }
247
248
    private function getBundlesResourcesPaths(ContainerBuilder $container, array $config): array
249
    {
250
        $bundlesResourcesPaths = [];
251
252
        foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
253
            $paths = [];
254
            $dirname = $bundle['path'];
255
            foreach (['.yaml', '.yml', '.xml', ''] as $extension) {
256
                $paths[] = "$dirname/Resources/config/api_resources$extension";
257
            }
258
            if ($this->isConfigEnabled($container, $config['doctrine'])) {
259
                $paths[] = "$dirname/Entity";
260
            }
261
            if ($this->isConfigEnabled($container, $config['doctrine_mongodb_odm'])) {
262
                $paths[] = "$dirname/Document";
263
            }
264
265
            foreach ($paths as $path) {
266
                if ($container->fileExists($path, false)) {
267
                    $bundlesResourcesPaths[] = $path;
268
                }
269
            }
270
        }
271
272
        return $bundlesResourcesPaths;
273
    }
274
275
    private function getResourcesToWatch(ContainerBuilder $container, array $config): array
276
    {
277
        $paths = array_unique(array_merge($config['mapping']['paths'], $this->getBundlesResourcesPaths($container, $config)));
278
279
        // Flex structure (only if nothing specified)
280
        $projectDir = $container->getParameter('kernel.project_dir');
281
        if (!$paths && is_dir($dir = "$projectDir/config/api_platform")) {
0 ignored issues
show
Bug Best Practice introduced by Maxime Veber
The expression $paths of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
282
            $paths = [$dir];
283
        }
284
285
        $resources = ['yml' => [], 'xml' => [], 'dir' => []];
286
287
        foreach ($paths as $path) {
288
            if (is_dir($path)) {
289
                foreach (Finder::create()->followLinks()->files()->in($path)->name('/\.(xml|ya?ml)$/') as $file) {
290
                    $resources['yaml' === ($extension = $file->getExtension()) ? 'yml' : $extension][] = $file->getRealPath();
291
                }
292
293
                $resources['dir'][] = $path;
294
                $container->addResource(new DirectoryResource($path, '/\.(xml|ya?ml|php)$/'));
295
296
                continue;
297
            }
298
299
            if ($container->fileExists($path, false)) {
300
                if (!preg_match('/\.(xml|ya?ml)$/', $path, $matches)) {
301
                    throw new RuntimeException(sprintf('Unsupported mapping type in "%s", supported types are XML & YAML.', $path));
302
                }
303
304
                $resources['yaml' === $matches[1] ? 'yml' : $matches[1]][] = $path;
305
306
                continue;
307
            }
308
309
            throw new RuntimeException(sprintf('Could not open file or directory "%s".', $path));
310
        }
311
312
        $container->setParameter('api_platform.resource_class_directories', $resources['dir']);
313
314
        return [$resources['xml'], $resources['yml']];
315
    }
316
317
    private function registerOAuthConfiguration(ContainerBuilder $container, array $config): void
318
    {
319
        if (!$config['oauth']) {
320
            return;
321
        }
322
323
        $container->setParameter('api_platform.oauth.enabled', $this->isConfigEnabled($container, $config['oauth']));
324
        $container->setParameter('api_platform.oauth.clientId', $config['oauth']['clientId']);
325
        $container->setParameter('api_platform.oauth.clientSecret', $config['oauth']['clientSecret']);
326
        $container->setParameter('api_platform.oauth.type', $config['oauth']['type']);
327
        $container->setParameter('api_platform.oauth.flow', $config['oauth']['flow']);
328
        $container->setParameter('api_platform.oauth.tokenUrl', $config['oauth']['tokenUrl']);
329
        $container->setParameter('api_platform.oauth.authorizationUrl', $config['oauth']['authorizationUrl']);
330
        $container->setParameter('api_platform.oauth.scopes', $config['oauth']['scopes']);
331
    }
332
333
    /**
334
     * Registers the Swagger, ReDoc and Swagger UI configuration.
335
     */
336
    private function registerSwaggerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
337
    {
338
        if (empty($config['swagger']['versions'])) {
339
            return;
340
        }
341
342
        $loader->load('json_schema.xml');
343
        $loader->load('swagger.xml');
344
345
        if ($config['enable_swagger_ui'] || $config['enable_re_doc']) {
346
            $loader->load('swagger-ui.xml');
347
            $container->setParameter('api_platform.enable_swagger_ui', $config['enable_swagger_ui']);
348
            $container->setParameter('api_platform.enable_re_doc', $config['enable_re_doc']);
349
        }
350
351
        $container->setParameter('api_platform.swagger.versions', $config['swagger']['versions']);
352
        $container->setParameter('api_platform.swagger.api_keys', $config['swagger']['api_keys']);
353
    }
354
355
    private function registerJsonApiConfiguration(array $formats, XmlFileLoader $loader): void
356
    {
357
        if (!isset($formats['jsonapi'])) {
358
            return;
359
        }
360
361
        $loader->load('jsonapi.xml');
362
    }
363
364
    private function registerJsonLdHydraConfiguration(ContainerBuilder $container, array $formats, XmlFileLoader $loader, bool $docEnabled): void
365
    {
366
        if (!isset($formats['jsonld'])) {
367
            return;
368
        }
369
370
        $loader->load('jsonld.xml');
371
        $loader->load('hydra.xml');
372
373
        if (!$docEnabled) {
374
            $container->removeDefinition('api_platform.hydra.listener.response.add_link_header');
375
        }
376
    }
377
378
    private function registerJsonHalConfiguration(array $formats, XmlFileLoader $loader): void
379
    {
380
        if (!isset($formats['jsonhal'])) {
381
            return;
382
        }
383
384
        $loader->load('hal.xml');
385
    }
386
387
    private function registerJsonProblemConfiguration(array $errorFormats, XmlFileLoader $loader): void
388
    {
389
        if (!isset($errorFormats['jsonproblem'])) {
390
            return;
391
        }
392
393
        $loader->load('problem.xml');
394
    }
395
396
    private function registerGraphQlConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
397
    {
398
        $enabled = $this->isConfigEnabled($container, $config['graphql']);
399
400
        $container->setParameter('api_platform.graphql.enabled', $enabled);
401
        $container->setParameter('api_platform.graphql.graphiql.enabled', $enabled && $this->isConfigEnabled($container, $config['graphql']['graphiql']));
402
        $container->setParameter('api_platform.graphql.graphql_playground.enabled', $enabled && $this->isConfigEnabled($container, $config['graphql']['graphql_playground']));
403
        $container->setParameter('api_platform.graphql.collection.pagination', $config['graphql']['collection']['pagination']);
404
405
        if (!$enabled) {
406
            return;
407
        }
408
409
        $container->setParameter('api_platform.graphql.default_ide', $config['graphql']['default_ide']);
410
        $container->setParameter('api_platform.graphql.nesting_separator', $config['graphql']['nesting_separator']);
411
412
        $loader->load('graphql.xml');
413
414
        $container->registerForAutoconfiguration(QueryItemResolverInterface::class)
415
            ->addTag('api_platform.graphql.query_resolver');
416
        $container->registerForAutoconfiguration(QueryCollectionResolverInterface::class)
417
            ->addTag('api_platform.graphql.query_resolver');
418
        $container->registerForAutoconfiguration(MutationResolverInterface::class)
419
            ->addTag('api_platform.graphql.mutation_resolver');
420
        $container->registerForAutoconfiguration(GraphQlTypeInterface::class)
421
            ->addTag('api_platform.graphql.type');
422
    }
423
424
    private function registerLegacyBundlesConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
425
    {
426
        /** @var string[] $bundles */
427
        $bundles = $container->getParameter('kernel.bundles');
428
429
        if (isset($bundles['FOSUserBundle']) && $config['enable_fos_user']) {
430
            $loader->load('fos_user.xml');
431
        }
432
433
        if (isset($bundles['NelmioApiDocBundle']) && $config['enable_nelmio_api_doc']) {
434
            $loader->load('nelmio_api_doc.xml');
435
        }
436
    }
437
438
    private function registerCacheConfiguration(ContainerBuilder $container): void
439
    {
440
        if (!$container->hasParameter('kernel.debug') || !$container->getParameter('kernel.debug')) {
441
            $container->removeDefinition('api_platform.cache_warmer.cache_pool_clearer');
442
        }
443
444
        if (!$container->hasParameter('api_platform.metadata_cache')) {
445
            return;
446
        }
447
448
        @trigger_error('The "api_platform.metadata_cache" parameter is deprecated since version 2.4 and will have no effect in 3.0.', E_USER_DEPRECATED);
449
450
        // BC
451
        if (!$container->getParameter('api_platform.metadata_cache')) {
452
            $container->removeDefinition('api_platform.cache_warmer.cache_pool_clearer');
453
454
            $container->register('api_platform.cache.metadata.property', ArrayAdapter::class);
455
            $container->register('api_platform.cache.metadata.resource', ArrayAdapter::class);
456
            $container->register('api_platform.cache.route_name_resolver', ArrayAdapter::class);
457
            $container->register('api_platform.cache.identifiers_extractor', ArrayAdapter::class);
458
            $container->register('api_platform.cache.subresource_operation_factory', ArrayAdapter::class);
459
            $container->register('api_platform.elasticsearch.cache.metadata.document', ArrayAdapter::class);
460
        }
461
    }
462
463
    private function registerDoctrineOrmConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
464
    {
465
        if (!$this->isConfigEnabled($container, $config['doctrine'])) {
466
            return;
467
        }
468
469
        $container->registerForAutoconfiguration(QueryItemExtensionInterface::class)
470
            ->addTag('api_platform.doctrine.orm.query_extension.item');
471
        $container->registerForAutoconfiguration(DoctrineQueryCollectionExtensionInterface::class)
472
            ->addTag('api_platform.doctrine.orm.query_extension.collection');
473
        $container->registerForAutoconfiguration(DoctrineOrmAbstractContextAwareFilter::class)
474
            ->setBindings(['$requestStack' => null]);
475
476
        $loader->load('doctrine_orm.xml');
477
478
        if ($this->isConfigEnabled($container, $config['eager_loading'])) {
479
            return;
480
        }
481
482
        $container->removeAlias(EagerLoadingExtension::class);
483
        $container->removeDefinition('api_platform.doctrine.orm.query_extension.eager_loading');
484
        $container->removeAlias(FilterEagerLoadingExtension::class);
485
        $container->removeDefinition('api_platform.doctrine.orm.query_extension.filter_eager_loading');
486
    }
487
488
    private function registerDoctrineMongoDbOdmConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
489
    {
490
        if (!$this->isConfigEnabled($container, $config['doctrine_mongodb_odm'])) {
491
            return;
492
        }
493
494
        $container->registerForAutoconfiguration(AggregationItemExtensionInterface::class)
495
            ->addTag('api_platform.doctrine_mongodb.odm.aggregation_extension.item');
496
        $container->registerForAutoconfiguration(AggregationCollectionExtensionInterface::class)
497
            ->addTag('api_platform.doctrine_mongodb.odm.aggregation_extension.collection');
498
        $container->registerForAutoconfiguration(DoctrineMongoDbOdmAbstractFilter::class)
499
            ->setBindings(['$managerRegistry' => new Reference('doctrine_mongodb')]);
500
501
        $loader->load('doctrine_mongodb_odm.xml');
502
    }
503
504
    private function registerHttpCacheConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
505
    {
506
        $loader->load('http_cache.xml');
507
508
        if (!$this->isConfigEnabled($container, $config['http_cache']['invalidation'])) {
509
            return;
510
        }
511
512
        if ($this->isConfigEnabled($container, $config['doctrine'])) {
513
            $loader->load('doctrine_orm_http_cache_purger.xml');
514
        }
515
516
        $loader->load('http_cache_tags.xml');
517
518
        $definitions = [];
519
        foreach ($config['http_cache']['invalidation']['varnish_urls'] as $key => $url) {
520
            $definition = new ChildDefinition('api_platform.http_cache.purger.varnish_client');
521
            $definition->addArgument(['base_uri' => $url] + $config['http_cache']['invalidation']['request_options']);
522
523
            $definitions[] = $definition;
524
        }
525
526
        $container->getDefinition('api_platform.http_cache.purger.varnish')->addArgument($definitions);
527
        $container->setAlias('api_platform.http_cache.purger', 'api_platform.http_cache.purger.varnish');
528
    }
529
530
    /**
531
     * Normalizes the format from config to the one accepted by Symfony HttpFoundation.
532
     */
533
    private function getFormats(array $configFormats): array
534
    {
535
        $formats = [];
536
        foreach ($configFormats as $format => $value) {
537
            foreach ($value['mime_types'] as $mimeType) {
538
                $formats[$format][] = $mimeType;
539
            }
540
        }
541
542
        return $formats;
543
    }
544
545
    private function registerValidatorConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
546
    {
547
        if (interface_exists(ValidatorInterface::class)) {
548
            $loader->load('validator.xml');
549
        }
550
551
        if (!$config['validator']) {
552
            return;
553
        }
554
555
        $container->setParameter('api_platform.validator.serialize_payload_fields', $config['validator']['serialize_payload_fields']);
556
    }
557
558
    private function registerDataCollectorConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
559
    {
560
        if (!$config['enable_profiler']) {
561
            return;
562
        }
563
564
        $loader->load('data_collector.xml');
565
566
        if ($container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug')) {
567
            $loader->load('debug.xml');
568
        }
569
    }
570
571
    private function registerMercureConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
572
    {
573
        if (!$this->isConfigEnabled($container, $config['mercure'])) {
574
            return;
575
        }
576
577
        $loader->load('mercure.xml');
578
        $container->getDefinition('api_platform.mercure.listener.response.add_link_header')->addArgument($config['mercure']['hub_url'] ?? '%mercure.default_hub%');
579
580
        if ($this->isConfigEnabled($container, $config['doctrine'])) {
581
            $loader->load('doctrine_orm_mercure_publisher.xml');
582
        }
583
    }
584
585
    private function registerMessengerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
586
    {
587
        if (!$this->isConfigEnabled($container, $config['messenger'])) {
588
            return;
589
        }
590
591
        $loader->load('messenger.xml');
592
    }
593
594
    private function registerElasticsearchConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
595
    {
596
        $enabled = $this->isConfigEnabled($container, $config['elasticsearch']);
597
598
        $container->setParameter('api_platform.elasticsearch.enabled', $enabled);
599
600
        if (!$enabled) {
601
            return;
602
        }
603
604
        $loader->load('elasticsearch.xml');
605
606
        $container->registerForAutoconfiguration(RequestBodySearchCollectionExtensionInterface::class)
607
            ->addTag('api_platform.elasticsearch.request_body_search_extension.collection');
608
609
        $container->setParameter('api_platform.elasticsearch.hosts', $config['elasticsearch']['hosts']);
610
        $container->setParameter('api_platform.elasticsearch.mapping', $config['elasticsearch']['mapping']);
611
    }
612
613
    private function registerDataTransformerConfiguration(ContainerBuilder $container): void
614
    {
615
        $container->registerForAutoconfiguration(DataTransformerInterface::class)
616
            ->addTag('api_platform.data_transformer');
617
    }
618
619
    private function registerSecurityConfiguration(ContainerBuilder $container, XmlFileLoader $loader): void
620
    {
621
        /** @var string[] $bundles */
622
        $bundles = $container->getParameter('kernel.bundles');
623
624
        if (isset($bundles['SecurityBundle'])) {
625
            $loader->load('security.xml');
626
        }
627
    }
628
}
629