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

api-platform / core / 15040977736

15 May 2025 09:02AM UTC coverage: 21.754% (+13.3%) from 8.423%
15040977736

Pull #6960

github

web-flow
Merge 7a7a13526 into 1862d03b7
Pull Request #6960: feat(json-schema): mutualize json schema between formats

320 of 460 new or added lines in 24 files covered. (69.57%)

1863 existing lines in 109 files now uncovered.

11069 of 50882 relevant lines covered (21.75%)

29.49 hits per line

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

0.0
/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.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\Odm\Extension\AggregationCollectionExtensionInterface;
17
use ApiPlatform\Doctrine\Odm\Extension\AggregationItemExtensionInterface;
18
use ApiPlatform\Doctrine\Odm\Filter\AbstractFilter as DoctrineMongoDbOdmAbstractFilter;
19
use ApiPlatform\Doctrine\Odm\State\LinksHandlerInterface as OdmLinksHandlerInterface;
20
use ApiPlatform\Doctrine\Orm\Extension\EagerLoadingExtension;
21
use ApiPlatform\Doctrine\Orm\Extension\FilterEagerLoadingExtension;
22
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface as DoctrineQueryCollectionExtensionInterface;
23
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
24
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter as DoctrineOrmAbstractFilter;
25
use ApiPlatform\Doctrine\Orm\State\LinksHandlerInterface as OrmLinksHandlerInterface;
26
use ApiPlatform\Elasticsearch\Extension\RequestBodySearchCollectionExtensionInterface;
27
use ApiPlatform\GraphQl\Error\ErrorHandlerInterface;
28
use ApiPlatform\GraphQl\Executor;
29
use ApiPlatform\GraphQl\Resolver\MutationResolverInterface;
30
use ApiPlatform\GraphQl\Resolver\QueryCollectionResolverInterface;
31
use ApiPlatform\GraphQl\Resolver\QueryItemResolverInterface;
32
use ApiPlatform\GraphQl\Type\Definition\TypeInterface as GraphQlTypeInterface;
33
use ApiPlatform\Metadata\ApiResource;
34
use ApiPlatform\Metadata\FilterInterface;
35
use ApiPlatform\Metadata\UriVariableTransformerInterface;
36
use ApiPlatform\Metadata\UrlGeneratorInterface;
37
use ApiPlatform\OpenApi\Model\Tag;
38
use ApiPlatform\RamseyUuid\Serializer\UuidDenormalizer;
39
use ApiPlatform\State\ApiResource\Error;
40
use ApiPlatform\State\ParameterProviderInterface;
41
use ApiPlatform\State\ProcessorInterface;
42
use ApiPlatform\State\ProviderInterface;
43
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRestrictionMetadataInterface;
44
use ApiPlatform\Symfony\Validator\ValidationGroupsGeneratorInterface;
45
use ApiPlatform\Validator\Exception\ValidationException;
46
use Doctrine\Persistence\ManagerRegistry;
47
use phpDocumentor\Reflection\DocBlockFactoryInterface;
48
use PHPStan\PhpDocParser\Parser\PhpDocParser;
49
use Ramsey\Uuid\Uuid;
50
use Symfony\Component\Config\FileLocator;
51
use Symfony\Component\Config\Resource\DirectoryResource;
52
use Symfony\Component\DependencyInjection\ChildDefinition;
53
use Symfony\Component\DependencyInjection\ContainerBuilder;
54
use Symfony\Component\DependencyInjection\Definition;
55
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
56
use Symfony\Component\DependencyInjection\Extension\Extension;
57
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
58
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
59
use Symfony\Component\DependencyInjection\Reference;
60
use Symfony\Component\Finder\Finder;
61
use Symfony\Component\HttpClient\ScopingHttpClient;
62
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
63
use Symfony\Component\Uid\AbstractUid;
64
use Symfony\Component\Validator\Validator\ValidatorInterface;
65
use Symfony\Component\Yaml\Yaml;
66
use Twig\Environment;
67

68
/**
69
 * The extension of this bundle.
70
 *
71
 * @author Kévin Dunglas <dunglas@gmail.com>
72
 */
73
final class ApiPlatformExtension extends Extension implements PrependExtensionInterface
74
{
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function prepend(ContainerBuilder $container): void
79
    {
UNCOV
80
        if (isset($container->getExtensions()['framework'])) {
×
UNCOV
81
            $container->prependExtensionConfig('framework', [
×
UNCOV
82
                'serializer' => [
×
UNCOV
83
                    'enabled' => true,
×
UNCOV
84
                ],
×
UNCOV
85
            ]);
×
UNCOV
86
            $container->prependExtensionConfig('framework', [
×
UNCOV
87
                'property_info' => [
×
UNCOV
88
                    'enabled' => true,
×
UNCOV
89
                ],
×
UNCOV
90
            ]);
×
91
        }
UNCOV
92
        if (isset($container->getExtensions()['lexik_jwt_authentication'])) {
×
93
            $container->prependExtensionConfig('lexik_jwt_authentication', [
×
94
                'api_platform' => [
×
95
                    'enabled' => true,
×
96
                ],
×
97
            ]);
×
98
        }
99
    }
100

101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function load(array $configs, ContainerBuilder $container): void
105
    {
UNCOV
106
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
×
107

UNCOV
108
        $configuration = new Configuration();
×
UNCOV
109
        $config = $this->processConfiguration($configuration, $configs);
×
UNCOV
110
        $container->setParameter('api_platform.use_symfony_listeners', $config['use_symfony_listeners']);
×
111

UNCOV
112
        $formats = $this->getFormats($config['formats']);
×
UNCOV
113
        $patchFormats = $this->getFormats($config['patch_formats']);
×
UNCOV
114
        $errorFormats = $this->getFormats($config['error_formats']);
×
UNCOV
115
        $docsFormats = $this->getFormats($config['docs_formats']);
×
116

UNCOV
117
        if (!$config['enable_docs']) {
×
118
            // JSON-LD documentation format is mandatory, even if documentation is disabled.
119
            $docsFormats = isset($formats['jsonld']) ? ['jsonld' => ['application/ld+json']] : [];
×
120
            // If documentation is disabled, the Hydra documentation for all the resources is hidden by default.
121
            if (!isset($config['defaults']['hideHydraOperation']) && !isset($config['defaults']['hide_hydra_operation'])) {
×
122
                $config['defaults']['hideHydraOperation'] = true;
×
123
            }
124
        }
UNCOV
125
        $jsonSchemaFormats = $config['jsonschema_formats'];
×
126

UNCOV
127
        if (!$jsonSchemaFormats) {
×
UNCOV
128
            foreach (array_merge(array_keys($formats), array_keys($errorFormats)) as $f) {
×
129
                // Distinct JSON-based formats must have names that start with 'json'
UNCOV
130
                if (str_starts_with($f, 'json')) {
×
UNCOV
131
                    $jsonSchemaFormats[$f] = true;
×
132
                }
133
            }
134
        }
135

UNCOV
136
        if (!isset($errorFormats['json'])) {
×
UNCOV
137
            $errorFormats['json'] = ['application/problem+json', 'application/json'];
×
138
        }
139

UNCOV
140
        if (!isset($errorFormats['jsonproblem'])) {
×
141
            $errorFormats['jsonproblem'] = ['application/problem+json'];
×
142
        }
143

UNCOV
144
        if (isset($formats['jsonapi']) && !isset($patchFormats['jsonapi'])) {
×
UNCOV
145
            $patchFormats['jsonapi'] = ['application/vnd.api+json'];
×
146
        }
147

NEW
148
        $this->registerCommonConfiguration($container, $config, $loader, $formats, $patchFormats, $errorFormats, $docsFormats);
×
UNCOV
149
        $this->registerMetadataConfiguration($container, $config, $loader);
×
UNCOV
150
        $this->registerOAuthConfiguration($container, $config);
×
UNCOV
151
        $this->registerOpenApiConfiguration($container, $config, $loader);
×
UNCOV
152
        $this->registerSwaggerConfiguration($container, $config, $loader);
×
UNCOV
153
        $this->registerJsonApiConfiguration($formats, $loader, $config);
×
UNCOV
154
        $this->registerJsonLdHydraConfiguration($container, $formats, $loader, $config);
×
UNCOV
155
        $this->registerJsonHalConfiguration($formats, $loader);
×
UNCOV
156
        $this->registerJsonProblemConfiguration($errorFormats, $loader);
×
UNCOV
157
        $this->registerGraphQlConfiguration($container, $config, $loader);
×
UNCOV
158
        $this->registerCacheConfiguration($container);
×
UNCOV
159
        $this->registerDoctrineOrmConfiguration($container, $config, $loader);
×
UNCOV
160
        $this->registerDoctrineMongoDbOdmConfiguration($container, $config, $loader);
×
UNCOV
161
        $this->registerHttpCacheConfiguration($container, $config, $loader);
×
UNCOV
162
        $this->registerValidatorConfiguration($container, $config, $loader);
×
UNCOV
163
        $this->registerDataCollectorConfiguration($container, $config, $loader);
×
UNCOV
164
        $this->registerMercureConfiguration($container, $config, $loader);
×
UNCOV
165
        $this->registerMessengerConfiguration($container, $config, $loader);
×
UNCOV
166
        $this->registerElasticsearchConfiguration($container, $config, $loader);
×
UNCOV
167
        $this->registerSecurityConfiguration($container, $config, $loader);
×
UNCOV
168
        $this->registerMakerConfiguration($container, $config, $loader);
×
UNCOV
169
        $this->registerArgumentResolverConfiguration($loader);
×
UNCOV
170
        $this->registerLinkSecurityConfiguration($loader, $config);
×
171

UNCOV
172
        $container->registerForAutoconfiguration(FilterInterface::class)
×
UNCOV
173
            ->addTag('api_platform.filter');
×
UNCOV
174
        $container->registerForAutoconfiguration(ProviderInterface::class)
×
UNCOV
175
            ->addTag('api_platform.state_provider');
×
UNCOV
176
        $container->registerForAutoconfiguration(ProcessorInterface::class)
×
UNCOV
177
            ->addTag('api_platform.state_processor');
×
UNCOV
178
        $container->registerForAutoconfiguration(UriVariableTransformerInterface::class)
×
UNCOV
179
            ->addTag('api_platform.uri_variables.transformer');
×
UNCOV
180
        $container->registerForAutoconfiguration(ParameterProviderInterface::class)
×
UNCOV
181
            ->addTag('api_platform.parameter_provider');
×
UNCOV
182
        $container->registerAttributeForAutoconfiguration(ApiResource::class, static function (ChildDefinition $definition): void {
×
183
            $definition->setAbstract(true)
×
184
                ->addTag('api_platform.resource')
×
185
                ->addTag('container.excluded', ['source' => 'by #[ApiResource] attribute']);
×
UNCOV
186
        });
×
187

UNCOV
188
        if (!$container->has('api_platform.state.item_provider')) {
×
189
            $container->setAlias('api_platform.state.item_provider', 'api_platform.state_provider.object');
×
190
        }
191
    }
192

193
    private function registerCommonConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader, array $formats, array $patchFormats, array $errorFormats, array $docsFormats): void
194
    {
UNCOV
195
        $loader->load('state/state.xml');
×
UNCOV
196
        $loader->load('symfony/symfony.xml');
×
UNCOV
197
        $loader->load('api.xml');
×
UNCOV
198
        $loader->load('filter.xml');
×
199

UNCOV
200
        if (class_exists(UuidDenormalizer::class) && class_exists(Uuid::class)) {
×
UNCOV
201
            $loader->load('ramsey_uuid.xml');
×
202
        }
203

UNCOV
204
        if (class_exists(AbstractUid::class)) {
×
UNCOV
205
            $loader->load('symfony/uid.xml');
×
206
        }
207

UNCOV
208
        $defaultContext = ['hydra_prefix' => $config['serializer']['hydra_prefix']] + ($container->hasParameter('serializer.default_context') ? $container->getParameter('serializer.default_context') : []);
×
209

UNCOV
210
        $container->setParameter('api_platform.serializer.default_context', $defaultContext);
×
UNCOV
211
        if (!$container->hasParameter('serializer.default_context')) {
×
UNCOV
212
            $container->setParameter('serializer.default_context', $container->getParameter('api_platform.serializer.default_context'));
×
213
        }
UNCOV
214
        if ($config['use_symfony_listeners']) {
×
215
            $loader->load('symfony/events.xml');
×
216
        } else {
UNCOV
217
            $loader->load('symfony/controller.xml');
×
UNCOV
218
            $loader->load('state/provider.xml');
×
UNCOV
219
            $loader->load('state/processor.xml');
×
220
        }
221

UNCOV
222
        $container->setParameter('api_platform.enable_entrypoint', $config['enable_entrypoint']);
×
UNCOV
223
        $container->setParameter('api_platform.enable_docs', $config['enable_docs']);
×
UNCOV
224
        $container->setParameter('api_platform.title', $config['title']);
×
UNCOV
225
        $container->setParameter('api_platform.description', $config['description']);
×
UNCOV
226
        $container->setParameter('api_platform.version', $config['version']);
×
UNCOV
227
        $container->setParameter('api_platform.show_webby', $config['show_webby']);
×
UNCOV
228
        $container->setParameter('api_platform.url_generation_strategy', $config['defaults']['url_generation_strategy'] ?? UrlGeneratorInterface::ABS_PATH);
×
UNCOV
229
        $container->setParameter('api_platform.exception_to_status', $config['exception_to_status']);
×
UNCOV
230
        $container->setParameter('api_platform.formats', $formats);
×
UNCOV
231
        $container->setParameter('api_platform.patch_formats', $patchFormats);
×
UNCOV
232
        $container->setParameter('api_platform.error_formats', $errorFormats);
×
UNCOV
233
        $container->setParameter('api_platform.docs_formats', $docsFormats);
×
NEW
234
        $container->setParameter('api_platform.jsonschema_formats', []);
×
UNCOV
235
        $container->setParameter('api_platform.eager_loading.enabled', $this->isConfigEnabled($container, $config['eager_loading']));
×
UNCOV
236
        $container->setParameter('api_platform.eager_loading.max_joins', $config['eager_loading']['max_joins']);
×
UNCOV
237
        $container->setParameter('api_platform.eager_loading.fetch_partial', $config['eager_loading']['fetch_partial']);
×
UNCOV
238
        $container->setParameter('api_platform.eager_loading.force_eager', $config['eager_loading']['force_eager']);
×
UNCOV
239
        $container->setParameter('api_platform.collection.exists_parameter_name', $config['collection']['exists_parameter_name']);
×
UNCOV
240
        $container->setParameter('api_platform.collection.order', $config['collection']['order']);
×
UNCOV
241
        $container->setParameter('api_platform.collection.order_parameter_name', $config['collection']['order_parameter_name']);
×
UNCOV
242
        $container->setParameter('api_platform.collection.order_nulls_comparison', $config['collection']['order_nulls_comparison']);
×
UNCOV
243
        $container->setParameter('api_platform.collection.pagination.enabled', $config['defaults']['pagination_enabled'] ?? true);
×
UNCOV
244
        $container->setParameter('api_platform.collection.pagination.partial', $config['defaults']['pagination_partial'] ?? false);
×
UNCOV
245
        $container->setParameter('api_platform.collection.pagination.client_enabled', $config['defaults']['pagination_client_enabled'] ?? false);
×
UNCOV
246
        $container->setParameter('api_platform.collection.pagination.client_items_per_page', $config['defaults']['pagination_client_items_per_page'] ?? false);
×
UNCOV
247
        $container->setParameter('api_platform.collection.pagination.client_partial', $config['defaults']['pagination_client_partial'] ?? false);
×
UNCOV
248
        $container->setParameter('api_platform.collection.pagination.items_per_page', $config['defaults']['pagination_items_per_page'] ?? 30);
×
UNCOV
249
        $container->setParameter('api_platform.collection.pagination.maximum_items_per_page', $config['defaults']['pagination_maximum_items_per_page'] ?? null);
×
UNCOV
250
        $container->setParameter('api_platform.collection.pagination.page_parameter_name', $config['defaults']['pagination_page_parameter_name'] ?? $config['collection']['pagination']['page_parameter_name']);
×
UNCOV
251
        $container->setParameter('api_platform.collection.pagination.enabled_parameter_name', $config['defaults']['pagination_enabled_parameter_name'] ?? $config['collection']['pagination']['enabled_parameter_name']);
×
UNCOV
252
        $container->setParameter('api_platform.collection.pagination.items_per_page_parameter_name', $config['defaults']['pagination_items_per_page_parameter_name'] ?? $config['collection']['pagination']['items_per_page_parameter_name']);
×
UNCOV
253
        $container->setParameter('api_platform.collection.pagination.partial_parameter_name', $config['defaults']['pagination_partial_parameter_name'] ?? $config['collection']['pagination']['partial_parameter_name']);
×
UNCOV
254
        $container->setParameter('api_platform.collection.pagination', $this->getPaginationDefaults($config['defaults'] ?? [], $config['collection']['pagination']));
×
UNCOV
255
        $container->setParameter('api_platform.handle_symfony_errors', $config['handle_symfony_errors'] ?? false);
×
UNCOV
256
        $container->setParameter('api_platform.http_cache.etag', $config['defaults']['cache_headers']['etag'] ?? true);
×
UNCOV
257
        $container->setParameter('api_platform.http_cache.max_age', $config['defaults']['cache_headers']['max_age'] ?? null);
×
UNCOV
258
        $container->setParameter('api_platform.http_cache.shared_max_age', $config['defaults']['cache_headers']['shared_max_age'] ?? null);
×
UNCOV
259
        $container->setParameter('api_platform.http_cache.vary', $config['defaults']['cache_headers']['vary'] ?? ['Accept']);
×
UNCOV
260
        $container->setParameter('api_platform.http_cache.public', $config['defaults']['cache_headers']['public'] ?? $config['http_cache']['public']);
×
UNCOV
261
        $container->setParameter('api_platform.http_cache.invalidation.max_header_length', $config['defaults']['cache_headers']['invalidation']['max_header_length'] ?? $config['http_cache']['invalidation']['max_header_length']);
×
UNCOV
262
        $container->setParameter('api_platform.http_cache.invalidation.xkey.glue', $config['defaults']['cache_headers']['invalidation']['xkey']['glue'] ?? $config['http_cache']['invalidation']['xkey']['glue']);
×
263

UNCOV
264
        $container->setAlias('api_platform.path_segment_name_generator', $config['path_segment_name_generator']);
×
UNCOV
265
        $container->setAlias('api_platform.inflector', $config['inflector']);
×
266

UNCOV
267
        if ($config['name_converter']) {
×
UNCOV
268
            $container->setAlias('api_platform.name_converter', $config['name_converter']);
×
269
        }
UNCOV
270
        $container->setParameter('api_platform.asset_package', $config['asset_package']);
×
UNCOV
271
        $container->setParameter('api_platform.defaults', $this->normalizeDefaults($config['defaults'] ?? []));
×
272

UNCOV
273
        if ($container->getParameter('kernel.debug')) {
×
UNCOV
274
            $container->removeDefinition('api_platform.serializer.mapping.cache_class_metadata_factory');
×
275
        }
276
    }
277

278
    /**
279
     * This method will be removed in 3.0 when "defaults" will be the regular configuration path for the pagination.
280
     */
281
    private function getPaginationDefaults(array $defaults, array $collectionPaginationConfiguration): array
282
    {
UNCOV
283
        $paginationOptions = [];
×
284

UNCOV
285
        foreach ($defaults as $key => $value) {
×
UNCOV
286
            if (!str_starts_with($key, 'pagination_')) {
×
UNCOV
287
                continue;
×
288
            }
289

UNCOV
290
            $paginationOptions[str_replace('pagination_', '', $key)] = $value;
×
291
        }
292

UNCOV
293
        return array_merge($collectionPaginationConfiguration, $paginationOptions);
×
294
    }
295

296
    private function normalizeDefaults(array $defaults): array
297
    {
UNCOV
298
        $normalizedDefaults = ['extra_properties' => $defaults['extra_properties'] ?? []];
×
UNCOV
299
        unset($defaults['extra_properties']);
×
300

UNCOV
301
        $rc = new \ReflectionClass(ApiResource::class);
×
UNCOV
302
        $publicProperties = [];
×
UNCOV
303
        foreach ($rc->getConstructor()->getParameters() as $param) {
×
UNCOV
304
            $publicProperties[$param->getName()] = true;
×
305
        }
306

UNCOV
307
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();
×
UNCOV
308
        foreach ($defaults as $option => $value) {
×
UNCOV
309
            if (isset($publicProperties[$nameConverter->denormalize($option)])) {
×
UNCOV
310
                $normalizedDefaults[$option] = $value;
×
311

UNCOV
312
                continue;
×
313
            }
314

315
            $normalizedDefaults['extra_properties'][$option] = $value;
×
316
        }
317

UNCOV
318
        return $normalizedDefaults;
×
319
    }
320

321
    private function registerMetadataConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
322
    {
UNCOV
323
        [$xmlResources, $yamlResources] = $this->getResourcesToWatch($container, $config);
×
324

UNCOV
325
        $container->setParameter('api_platform.class_name_resources', $this->getClassNameResources());
×
326

UNCOV
327
        $loader->load('metadata/resource_name.xml');
×
UNCOV
328
        $loader->load('metadata/property_name.xml');
×
329

UNCOV
330
        if (!empty($config['resource_class_directories'])) {
×
331
            $container->setParameter('api_platform.resource_class_directories', array_merge(
×
332
                $config['resource_class_directories'],
×
333
                $container->getParameter('api_platform.resource_class_directories')
×
334
            ));
×
335
        }
336

337
        // V3 metadata
UNCOV
338
        $loader->load('metadata/xml.xml');
×
UNCOV
339
        $loader->load('metadata/links.xml');
×
UNCOV
340
        $loader->load('metadata/property.xml');
×
UNCOV
341
        $loader->load('metadata/resource.xml');
×
UNCOV
342
        $loader->load('metadata/operation.xml');
×
343

UNCOV
344
        $container->getDefinition('api_platform.metadata.resource_extractor.xml')->replaceArgument(0, $xmlResources);
×
UNCOV
345
        $container->getDefinition('api_platform.metadata.property_extractor.xml')->replaceArgument(0, $xmlResources);
×
346

UNCOV
347
        if (class_exists(PhpDocParser::class) || interface_exists(DocBlockFactoryInterface::class)) {
×
UNCOV
348
            $loader->load('metadata/php_doc.xml');
×
349
        }
350

UNCOV
351
        if (class_exists(Yaml::class)) {
×
UNCOV
352
            $loader->load('metadata/yaml.xml');
×
UNCOV
353
            $container->getDefinition('api_platform.metadata.resource_extractor.yaml')->replaceArgument(0, $yamlResources);
×
UNCOV
354
            $container->getDefinition('api_platform.metadata.property_extractor.yaml')->replaceArgument(0, $yamlResources);
×
355
        }
356
    }
357

358
    private function getClassNameResources(): array
359
    {
UNCOV
360
        return [
×
UNCOV
361
            Error::class,
×
UNCOV
362
            ValidationException::class,
×
UNCOV
363
        ];
×
364
    }
365

366
    private function getBundlesResourcesPaths(ContainerBuilder $container, array $config): array
367
    {
UNCOV
368
        $bundlesResourcesPaths = [];
×
369

UNCOV
370
        foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
×
UNCOV
371
            $dirname = $bundle['path'];
×
UNCOV
372
            $paths = [
×
UNCOV
373
                "$dirname/ApiResource",
×
UNCOV
374
                "$dirname/src/ApiResource",
×
UNCOV
375
            ];
×
UNCOV
376
            foreach (['.yaml', '.yml', '.xml', ''] as $extension) {
×
UNCOV
377
                $paths[] = "$dirname/Resources/config/api_resources$extension";
×
UNCOV
378
                $paths[] = "$dirname/config/api_resources$extension";
×
379
            }
UNCOV
380
            if ($this->isConfigEnabled($container, $config['doctrine'])) {
×
UNCOV
381
                $paths[] = "$dirname/Entity";
×
UNCOV
382
                $paths[] = "$dirname/src/Entity";
×
383
            }
UNCOV
384
            if ($this->isConfigEnabled($container, $config['doctrine_mongodb_odm'])) {
×
385
                $paths[] = "$dirname/Document";
×
386
                $paths[] = "$dirname/src/Document";
×
387
            }
388

UNCOV
389
            foreach ($paths as $path) {
×
UNCOV
390
                if ($container->fileExists($path, false)) {
×
UNCOV
391
                    $bundlesResourcesPaths[] = $path;
×
392
                }
393
            }
394
        }
395

UNCOV
396
        return $bundlesResourcesPaths;
×
397
    }
398

399
    private function getResourcesToWatch(ContainerBuilder $container, array $config): array
400
    {
UNCOV
401
        $paths = array_unique(array_merge($this->getBundlesResourcesPaths($container, $config), $config['mapping']['paths']));
×
402

UNCOV
403
        if (!$config['mapping']['paths']) {
×
404
            $projectDir = $container->getParameter('kernel.project_dir');
×
405
            foreach (["$projectDir/config/api_platform", "$projectDir/src/ApiResource"] as $dir) {
×
406
                if (is_dir($dir)) {
×
407
                    $paths[] = $dir;
×
408
                }
409
            }
410

411
            if ($this->isConfigEnabled($container, $config['doctrine']) && is_dir($doctrinePath = "$projectDir/src/Entity")) {
×
412
                $paths[] = $doctrinePath;
×
413
            }
414

415
            if ($this->isConfigEnabled($container, $config['doctrine_mongodb_odm']) && is_dir($documentPath = "$projectDir/src/Document")) {
×
416
                $paths[] = $documentPath;
×
417
            }
418
        }
419

UNCOV
420
        $resources = ['yml' => [], 'xml' => [], 'dir' => []];
×
421

UNCOV
422
        foreach ($paths as $path) {
×
UNCOV
423
            if (is_dir($path)) {
×
UNCOV
424
                foreach (Finder::create()->followLinks()->files()->in($path)->name('/\.(xml|ya?ml)$/')->sortByName() as $file) {
×
UNCOV
425
                    $resources['yaml' === ($extension = $file->getExtension()) ? 'yml' : $extension][] = $file->getRealPath();
×
426
                }
427

UNCOV
428
                $resources['dir'][] = $path;
×
UNCOV
429
                $container->addResource(new DirectoryResource($path, '/\.(xml|ya?ml|php)$/'));
×
430

UNCOV
431
                continue;
×
432
            }
433

434
            if ($container->fileExists($path, false)) {
×
435
                if (!preg_match('/\.(xml|ya?ml)$/', (string) $path, $matches)) {
×
436
                    throw new RuntimeException(\sprintf('Unsupported mapping type in "%s", supported types are XML & YAML.', $path));
×
437
                }
438

439
                $resources['yaml' === $matches[1] ? 'yml' : $matches[1]][] = $path;
×
440

441
                continue;
×
442
            }
443

444
            throw new RuntimeException(\sprintf('Could not open file or directory "%s".', $path));
×
445
        }
446

UNCOV
447
        $container->setParameter('api_platform.resource_class_directories', $resources['dir']);
×
448

UNCOV
449
        return [$resources['xml'], $resources['yml']];
×
450
    }
451

452
    private function registerOAuthConfiguration(ContainerBuilder $container, array $config): void
453
    {
UNCOV
454
        if (!$config['oauth']) {
×
455
            return;
×
456
        }
457

UNCOV
458
        $container->setParameter('api_platform.oauth.enabled', $this->isConfigEnabled($container, $config['oauth']));
×
UNCOV
459
        $container->setParameter('api_platform.oauth.clientId', $config['oauth']['clientId']);
×
UNCOV
460
        $container->setParameter('api_platform.oauth.clientSecret', $config['oauth']['clientSecret']);
×
UNCOV
461
        $container->setParameter('api_platform.oauth.type', $config['oauth']['type']);
×
UNCOV
462
        $container->setParameter('api_platform.oauth.flow', $config['oauth']['flow']);
×
UNCOV
463
        $container->setParameter('api_platform.oauth.tokenUrl', $config['oauth']['tokenUrl']);
×
UNCOV
464
        $container->setParameter('api_platform.oauth.authorizationUrl', $config['oauth']['authorizationUrl']);
×
UNCOV
465
        $container->setParameter('api_platform.oauth.refreshUrl', $config['oauth']['refreshUrl']);
×
UNCOV
466
        $container->setParameter('api_platform.oauth.scopes', $config['oauth']['scopes']);
×
UNCOV
467
        $container->setParameter('api_platform.oauth.pkce', $config['oauth']['pkce']);
×
468

UNCOV
469
        if ($container->hasDefinition('api_platform.swagger_ui.action')) {
×
470
            $container->getDefinition('api_platform.swagger_ui.action')->setArgument(10, $config['oauth']['pkce']);
×
471
        }
472
    }
473

474
    /**
475
     * Registers the Swagger, ReDoc and Swagger UI configuration.
476
     */
477
    private function registerSwaggerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
478
    {
UNCOV
479
        foreach (array_keys($config['swagger']['api_keys']) as $keyName) {
×
UNCOV
480
            if (!preg_match('/^[a-zA-Z0-9._-]+$/', $keyName)) {
×
481
                trigger_deprecation('api-platform/core', '3.1', \sprintf('The swagger api_keys key "%s" is not valid with OpenAPI 3.1 it should match "^[a-zA-Z0-9._-]+$"', $keyName));
×
482
            }
483
        }
484

UNCOV
485
        $container->setParameter('api_platform.swagger.versions', $config['swagger']['versions']);
×
486

UNCOV
487
        if (!$config['enable_swagger'] && $config['enable_swagger_ui']) {
×
488
            throw new RuntimeException('You can not enable the Swagger UI without enabling Swagger, fix this by enabling swagger via the configuration "enable_swagger: true".');
×
489
        }
490

UNCOV
491
        if (!$config['enable_swagger']) {
×
492
            return;
×
493
        }
494

UNCOV
495
        $loader->load('openapi.xml');
×
496

UNCOV
497
        if (class_exists(Yaml::class)) {
×
UNCOV
498
            $loader->load('openapi/yaml.xml');
×
499
        }
500

UNCOV
501
        $loader->load('swagger_ui.xml');
×
502

UNCOV
503
        if ($config['use_symfony_listeners']) {
×
504
            $loader->load('symfony/swagger_ui.xml');
×
505
        }
506

UNCOV
507
        if ($config['enable_swagger_ui']) {
×
UNCOV
508
            $loader->load('state/swagger_ui.xml');
×
509
        }
510

UNCOV
511
        if (!$config['enable_swagger_ui'] && !$config['enable_re_doc']) {
×
512
            // Remove the listener but keep the controller to allow customizing the path of the UI
513
            $container->removeDefinition('api_platform.swagger.listener.ui');
×
514
        }
515

UNCOV
516
        $container->setParameter('api_platform.enable_swagger_ui', $config['enable_swagger_ui']);
×
UNCOV
517
        $container->setParameter('api_platform.enable_re_doc', $config['enable_re_doc']);
×
UNCOV
518
        $container->setParameter('api_platform.swagger.api_keys', $config['swagger']['api_keys']);
×
UNCOV
519
        $container->setParameter('api_platform.swagger.persist_authorization', $config['swagger']['persist_authorization']);
×
UNCOV
520
        $container->setParameter('api_platform.swagger.http_auth', $config['swagger']['http_auth']);
×
UNCOV
521
        if ($config['openapi']['swagger_ui_extra_configuration'] && $config['swagger']['swagger_ui_extra_configuration']) {
×
522
            throw new RuntimeException('You can not set "swagger_ui_extra_configuration" twice - in "openapi" and "swagger" section.');
×
523
        }
UNCOV
524
        $container->setParameter('api_platform.swagger_ui.extra_configuration', $config['openapi']['swagger_ui_extra_configuration'] ?: $config['swagger']['swagger_ui_extra_configuration']);
×
525
    }
526

527
    private function registerJsonApiConfiguration(array $formats, XmlFileLoader $loader, array $config): void
528
    {
UNCOV
529
        if (!isset($formats['jsonapi'])) {
×
530
            return;
×
531
        }
532

UNCOV
533
        $loader->load('jsonapi.xml');
×
UNCOV
534
        $loader->load('state/jsonapi.xml');
×
535
    }
536

537
    private function registerJsonLdHydraConfiguration(ContainerBuilder $container, array $formats, XmlFileLoader $loader, array $config): void
538
    {
UNCOV
539
        if (!isset($formats['jsonld'])) {
×
540
            return;
×
541
        }
542

UNCOV
543
        if ($config['use_symfony_listeners']) {
×
544
            $loader->load('symfony/jsonld.xml');
×
545
        } else {
UNCOV
546
            $loader->load('state/jsonld.xml');
×
547
        }
548

UNCOV
549
        $loader->load('state/hydra.xml');
×
UNCOV
550
        $loader->load('jsonld.xml');
×
UNCOV
551
        $loader->load('hydra.xml');
×
552

UNCOV
553
        if (!$container->has('api_platform.json_schema.schema_factory')) {
×
554
            $container->removeDefinition('api_platform.hydra.json_schema.schema_factory');
×
555
        }
556
    }
557

558
    private function registerJsonHalConfiguration(array $formats, XmlFileLoader $loader): void
559
    {
UNCOV
560
        if (!isset($formats['jsonhal'])) {
×
561
            return;
×
562
        }
563

UNCOV
564
        $loader->load('hal.xml');
×
565
    }
566

567
    private function registerJsonProblemConfiguration(array $errorFormats, XmlFileLoader $loader): void
568
    {
UNCOV
569
        if (!isset($errorFormats['jsonproblem'])) {
×
570
            return;
×
571
        }
572

UNCOV
573
        $loader->load('problem.xml');
×
574
    }
575

576
    private function registerGraphQlConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
577
    {
UNCOV
578
        $enabled = $this->isConfigEnabled($container, $config['graphql']);
×
UNCOV
579
        $graphqlIntrospectionEnabled = $enabled && $this->isConfigEnabled($container, $config['graphql']['introspection']);
×
UNCOV
580
        $graphiqlEnabled = $enabled && $this->isConfigEnabled($container, $config['graphql']['graphiql']);
×
UNCOV
581
        $graphqlPlayGroundEnabled = $enabled && $this->isConfigEnabled($container, $config['graphql']['graphql_playground']);
×
UNCOV
582
        $maxQueryDepth = (int) $config['graphql']['max_query_depth'];
×
UNCOV
583
        $maxQueryComplexity = (int) $config['graphql']['max_query_complexity'];
×
UNCOV
584
        if ($graphqlPlayGroundEnabled) {
×
585
            trigger_deprecation('api-platform/core', '3.1', 'GraphQL Playground is deprecated and will be removed in API Platform 4.0. Only GraphiQL will be available in the future. Set api_platform.graphql.graphql_playground to false in the configuration to remove this deprecation.');
×
586
        }
587

UNCOV
588
        $container->setParameter('api_platform.graphql.enabled', $enabled);
×
UNCOV
589
        $container->setParameter('api_platform.graphql.max_query_depth', $maxQueryDepth);
×
UNCOV
590
        $container->setParameter('api_platform.graphql.max_query_complexity', $maxQueryComplexity);
×
UNCOV
591
        $container->setParameter('api_platform.graphql.introspection.enabled', $graphqlIntrospectionEnabled);
×
UNCOV
592
        $container->setParameter('api_platform.graphql.graphiql.enabled', $graphiqlEnabled);
×
UNCOV
593
        $container->setParameter('api_platform.graphql.graphql_playground.enabled', $graphqlPlayGroundEnabled);
×
UNCOV
594
        $container->setParameter('api_platform.graphql.collection.pagination', $config['graphql']['collection']['pagination']);
×
595

UNCOV
596
        if (!$enabled) {
×
597
            return;
×
598
        }
599

UNCOV
600
        if (!class_exists(Executor::class)) {
×
601
            throw new \RuntimeException('Graphql is enabled but not installed, run: composer require "api-platform/graphql".');
×
602
        }
603

UNCOV
604
        $container->setParameter('api_platform.graphql.default_ide', $config['graphql']['default_ide']);
×
UNCOV
605
        $container->setParameter('api_platform.graphql.nesting_separator', $config['graphql']['nesting_separator']);
×
606

UNCOV
607
        $loader->load('graphql.xml');
×
608

609
        // @phpstan-ignore-next-line because PHPStan uses the container of the test env cache and in test the parameter kernel.bundles always contains the key TwigBundle
UNCOV
610
        if (!class_exists(Environment::class) || !isset($container->getParameter('kernel.bundles')['TwigBundle'])) {
×
611
            if ($graphiqlEnabled || $graphqlPlayGroundEnabled) {
×
612
                throw new RuntimeException(\sprintf('GraphiQL and GraphQL Playground interfaces depend on Twig. Please activate TwigBundle for the %s environnement or disable GraphiQL and GraphQL Playground.', $container->getParameter('kernel.environment')));
×
613
            }
614
            $container->removeDefinition('api_platform.graphql.action.graphiql');
×
615
            $container->removeDefinition('api_platform.graphql.action.graphql_playground');
×
616
        }
617

UNCOV
618
        $container->registerForAutoconfiguration(QueryItemResolverInterface::class)
×
UNCOV
619
            ->addTag('api_platform.graphql.resolver');
×
UNCOV
620
        $container->registerForAutoconfiguration(QueryCollectionResolverInterface::class)
×
UNCOV
621
            ->addTag('api_platform.graphql.resolver');
×
UNCOV
622
        $container->registerForAutoconfiguration(MutationResolverInterface::class)
×
UNCOV
623
            ->addTag('api_platform.graphql.resolver');
×
UNCOV
624
        $container->registerForAutoconfiguration(GraphQlTypeInterface::class)
×
UNCOV
625
            ->addTag('api_platform.graphql.type');
×
UNCOV
626
        $container->registerForAutoconfiguration(ErrorHandlerInterface::class)
×
UNCOV
627
            ->addTag('api_platform.graphql.error_handler');
×
628
    }
629

630
    private function registerCacheConfiguration(ContainerBuilder $container): void
631
    {
UNCOV
632
        if (!$container->hasParameter('kernel.debug') || !$container->getParameter('kernel.debug')) {
×
633
            $container->removeDefinition('api_platform.cache_warmer.cache_pool_clearer');
×
634
        }
635
    }
636

637
    private function registerDoctrineOrmConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
638
    {
UNCOV
639
        if (!$this->isConfigEnabled($container, $config['doctrine'])) {
×
640
            return;
×
641
        }
642

643
        // For older versions of doctrine bridge this allows autoconfiguration for filters
UNCOV
644
        if (!$container->has(ManagerRegistry::class)) {
×
UNCOV
645
            $container->setAlias(ManagerRegistry::class, 'doctrine');
×
646
        }
647

UNCOV
648
        $container->registerForAutoconfiguration(QueryItemExtensionInterface::class)
×
UNCOV
649
            ->addTag('api_platform.doctrine.orm.query_extension.item');
×
UNCOV
650
        $container->registerForAutoconfiguration(DoctrineQueryCollectionExtensionInterface::class)
×
UNCOV
651
            ->addTag('api_platform.doctrine.orm.query_extension.collection');
×
UNCOV
652
        $container->registerForAutoconfiguration(DoctrineOrmAbstractFilter::class);
×
653

UNCOV
654
        $container->registerForAutoconfiguration(OrmLinksHandlerInterface::class)
×
UNCOV
655
            ->addTag('api_platform.doctrine.orm.links_handler');
×
656

UNCOV
657
        $loader->load('doctrine_orm.xml');
×
658

UNCOV
659
        if ($this->isConfigEnabled($container, $config['eager_loading'])) {
×
UNCOV
660
            return;
×
661
        }
662

663
        $container->removeAlias(EagerLoadingExtension::class);
×
664
        $container->removeDefinition('api_platform.doctrine.orm.query_extension.eager_loading');
×
665
        $container->removeAlias(FilterEagerLoadingExtension::class);
×
666
        $container->removeDefinition('api_platform.doctrine.orm.query_extension.filter_eager_loading');
×
667
    }
668

669
    private function registerDoctrineMongoDbOdmConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
670
    {
UNCOV
671
        if (!$this->isConfigEnabled($container, $config['doctrine_mongodb_odm'])) {
×
UNCOV
672
            return;
×
673
        }
674

675
        $container->registerForAutoconfiguration(AggregationItemExtensionInterface::class)
×
676
            ->addTag('api_platform.doctrine_mongodb.odm.aggregation_extension.item');
×
677
        $container->registerForAutoconfiguration(AggregationCollectionExtensionInterface::class)
×
678
            ->addTag('api_platform.doctrine_mongodb.odm.aggregation_extension.collection');
×
679
        $container->registerForAutoconfiguration(DoctrineMongoDbOdmAbstractFilter::class)
×
680
            ->setBindings(['$managerRegistry' => new Reference('doctrine_mongodb')]);
×
681
        $container->registerForAutoconfiguration(OdmLinksHandlerInterface::class)
×
682
            ->addTag('api_platform.doctrine.odm.links_handler');
×
683

684
        $loader->load('doctrine_mongodb_odm.xml');
×
685
    }
686

687
    private function registerHttpCacheConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
688
    {
UNCOV
689
        $loader->load('http_cache.xml');
×
690

UNCOV
691
        if (!$this->isConfigEnabled($container, $config['http_cache']['invalidation'])) {
×
692
            return;
×
693
        }
694

UNCOV
695
        if ($this->isConfigEnabled($container, $config['doctrine'])) {
×
UNCOV
696
            $loader->load('doctrine_orm_http_cache_purger.xml');
×
697
        }
698

UNCOV
699
        $loader->load('state/http_cache_purger.xml');
×
UNCOV
700
        $loader->load('http_cache_purger.xml');
×
701

UNCOV
702
        foreach ($config['http_cache']['invalidation']['scoped_clients'] as $client) {
×
703
            $definition = $container->getDefinition($client);
×
704
            $definition->addTag('api_platform.http_cache.http_client');
×
705
        }
706

UNCOV
707
        if (!($urls = $config['http_cache']['invalidation']['urls'])) {
×
UNCOV
708
            $urls = $config['http_cache']['invalidation']['varnish_urls'];
×
709
        }
710

UNCOV
711
        foreach ($urls as $key => $url) {
×
712
            $definition = new Definition(ScopingHttpClient::class, [new Reference('http_client'), $url, ['base_uri' => $url] + $config['http_cache']['invalidation']['request_options']]);
×
713
            $definition->setFactory([ScopingHttpClient::class, 'forBaseUri']);
×
714
            $definition->addTag('api_platform.http_cache.http_client');
×
715
            $container->setDefinition('api_platform.invalidation_http_client.'.$key, $definition);
×
716
        }
717

UNCOV
718
        $serviceName = $config['http_cache']['invalidation']['purger'];
×
719

UNCOV
720
        if (!$container->hasDefinition('api_platform.http_cache.purger')) {
×
UNCOV
721
            $container->setAlias('api_platform.http_cache.purger', $serviceName);
×
722
        }
723
    }
724

725
    /**
726
     * Normalizes the format from config to the one accepted by Symfony HttpFoundation.
727
     */
728
    private function getFormats(array $configFormats): array
729
    {
UNCOV
730
        $formats = [];
×
UNCOV
731
        foreach ($configFormats as $format => $value) {
×
UNCOV
732
            foreach ($value['mime_types'] as $mimeType) {
×
UNCOV
733
                $formats[$format][] = $mimeType;
×
734
            }
735
        }
736

UNCOV
737
        return $formats;
×
738
    }
739

740
    private function registerValidatorConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
741
    {
UNCOV
742
        if (interface_exists(ValidatorInterface::class)) {
×
UNCOV
743
            $loader->load('metadata/validator.xml');
×
UNCOV
744
            $loader->load('validator/validator.xml');
×
745

UNCOV
746
            if ($this->isConfigEnabled($container, $config['graphql'])) {
×
UNCOV
747
                $loader->load('graphql/validator.xml');
×
748
            }
749

UNCOV
750
            $loader->load($config['use_symfony_listeners'] ? 'validator/events.xml' : 'validator/state.xml');
×
751

UNCOV
752
            $container->registerForAutoconfiguration(ValidationGroupsGeneratorInterface::class)
×
UNCOV
753
                ->addTag('api_platform.validation_groups_generator');
×
UNCOV
754
            $container->registerForAutoconfiguration(PropertySchemaRestrictionMetadataInterface::class)
×
UNCOV
755
                ->addTag('api_platform.metadata.property_schema_restriction');
×
756
        }
757

UNCOV
758
        if (!$config['validator']) {
×
759
            return;
×
760
        }
761

UNCOV
762
        $container->setParameter('api_platform.validator.serialize_payload_fields', $config['validator']['serialize_payload_fields']);
×
UNCOV
763
        $container->setParameter('api_platform.validator.query_parameter_validation', $config['validator']['query_parameter_validation']);
×
764

UNCOV
765
        if (!$config['validator']['query_parameter_validation']) {
×
766
            $container->removeDefinition('api_platform.listener.view.validate_query_parameters');
×
767
            $container->removeDefinition('api_platform.validator.query_parameter_validator');
×
768
            $container->removeDefinition('api_platform.symfony.parameter_validator');
×
769
        }
770
    }
771

772
    private function registerDataCollectorConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
773
    {
UNCOV
774
        if (!$config['enable_profiler']) {
×
775
            return;
×
776
        }
777

UNCOV
778
        $loader->load('data_collector.xml');
×
779

UNCOV
780
        if ($container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug')) {
×
UNCOV
781
            $loader->load('debug.xml');
×
782
        }
783
    }
784

785
    private function registerMercureConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
786
    {
UNCOV
787
        if (!$this->isConfigEnabled($container, $config['mercure'])) {
×
788
            return;
×
789
        }
790

UNCOV
791
        $container->setParameter('api_platform.mercure.include_type', $config['mercure']['include_type']);
×
UNCOV
792
        $loader->load('state/mercure.xml');
×
793

UNCOV
794
        if ($this->isConfigEnabled($container, $config['doctrine'])) {
×
UNCOV
795
            $loader->load('doctrine_orm_mercure_publisher.xml');
×
796
        }
UNCOV
797
        if ($this->isConfigEnabled($container, $config['doctrine_mongodb_odm'])) {
×
798
            $loader->load('doctrine_odm_mercure_publisher.xml');
×
799
        }
800

UNCOV
801
        if ($this->isConfigEnabled($container, $config['graphql'])) {
×
UNCOV
802
            $loader->load('graphql_mercure.xml');
×
803
        }
804
    }
805

806
    private function registerMessengerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
807
    {
UNCOV
808
        if (!$this->isConfigEnabled($container, $config['messenger'])) {
×
809
            return;
×
810
        }
811

UNCOV
812
        $loader->load('messenger.xml');
×
813
    }
814

815
    private function registerElasticsearchConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
816
    {
UNCOV
817
        $enabled = $this->isConfigEnabled($container, $config['elasticsearch']);
×
818

UNCOV
819
        $container->setParameter('api_platform.elasticsearch.enabled', $enabled);
×
820

UNCOV
821
        if (!$enabled) {
×
UNCOV
822
            return;
×
823
        }
824

825
        $clientClass = !class_exists(\Elasticsearch\Client::class)
×
826
            // ES v7
×
827
            ? \Elastic\Elasticsearch\Client::class
×
828
            // ES v8 and up
×
829
            : \Elasticsearch\Client::class;
×
830

831
        $clientDefinition = new Definition($clientClass);
×
832
        $container->setDefinition('api_platform.elasticsearch.client', $clientDefinition);
×
833
        $container->registerForAutoconfiguration(RequestBodySearchCollectionExtensionInterface::class)
×
834
            ->addTag('api_platform.elasticsearch.request_body_search_extension.collection');
×
835
        $container->setParameter('api_platform.elasticsearch.hosts', $config['elasticsearch']['hosts']);
×
836
        $loader->load('elasticsearch.xml');
×
837
    }
838

839
    private function registerSecurityConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
840
    {
841
        /** @var string[] $bundles */
UNCOV
842
        $bundles = $container->getParameter('kernel.bundles');
×
843

UNCOV
844
        if (!isset($bundles['SecurityBundle'])) {
×
845
            return;
×
846
        }
847

UNCOV
848
        $loader->load('security.xml');
×
849

UNCOV
850
        $loader->load('state/security.xml');
×
851

UNCOV
852
        if (interface_exists(ValidatorInterface::class)) {
×
UNCOV
853
            $loader->load('state/security_validator.xml');
×
854
        }
855

UNCOV
856
        if ($this->isConfigEnabled($container, $config['graphql'])) {
×
UNCOV
857
            $loader->load('graphql/security.xml');
×
858
        }
859
    }
860

861
    private function registerOpenApiConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
862
    {
UNCOV
863
        $container->setParameter('api_platform.openapi.termsOfService', $config['openapi']['termsOfService']);
×
UNCOV
864
        $container->setParameter('api_platform.openapi.contact.name', $config['openapi']['contact']['name']);
×
UNCOV
865
        $container->setParameter('api_platform.openapi.contact.url', $config['openapi']['contact']['url']);
×
UNCOV
866
        $container->setParameter('api_platform.openapi.contact.email', $config['openapi']['contact']['email']);
×
UNCOV
867
        $container->setParameter('api_platform.openapi.license.name', $config['openapi']['license']['name']);
×
UNCOV
868
        $container->setParameter('api_platform.openapi.license.url', $config['openapi']['license']['url']);
×
UNCOV
869
        $container->setParameter('api_platform.openapi.license.identifier', $config['openapi']['license']['identifier']);
×
UNCOV
870
        $container->setParameter('api_platform.openapi.overrideResponses', $config['openapi']['overrideResponses']);
×
871

UNCOV
872
        $tags = [];
×
UNCOV
873
        foreach ($config['openapi']['tags'] as $tag) {
×
874
            $tags[] = new Tag($tag['name'], $tag['description'] ?? null);
×
875
        }
876

UNCOV
877
        $container->setParameter('api_platform.openapi.tags', $tags);
×
878

UNCOV
879
        $container->setParameter('api_platform.openapi.errorResourceClass', $config['openapi']['error_resource_class'] ?? null);
×
UNCOV
880
        $container->setParameter('api_platform.openapi.validationErrorResourceClass', $config['openapi']['validation_error_resource_class'] ?? null);
×
881

UNCOV
882
        $loader->load('json_schema.xml');
×
883
    }
884

885
    private function registerMakerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
886
    {
UNCOV
887
        if (!$this->isConfigEnabled($container, $config['maker'])) {
×
888
            return;
×
889
        }
890

UNCOV
891
        $loader->load('maker.xml');
×
892
    }
893

894
    private function registerArgumentResolverConfiguration(XmlFileLoader $loader): void
895
    {
UNCOV
896
        $loader->load('argument_resolver.xml');
×
897
    }
898

899
    private function registerLinkSecurityConfiguration(XmlFileLoader $loader, array $config): void
900
    {
UNCOV
901
        if ($config['enable_link_security']) {
×
UNCOV
902
            $loader->load('link_security.xml');
×
903
        }
904
    }
905
}
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