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

api-platform / core / 18060089452

27 Sep 2025 12:57PM UTC coverage: 0.0% (-21.8%) from 21.793%
18060089452

Pull #7397

github

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

0 of 15 new or added lines in 1 file covered. (0.0%)

11967 existing lines in 393 files now uncovered.

0 of 53914 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/Symfony/Bundle/DependencyInjection/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\AsOperationMutator;
35
use ApiPlatform\Metadata\AsResourceMutator;
36
use ApiPlatform\Metadata\FilterInterface;
37
use ApiPlatform\Metadata\OperationMutatorInterface;
38
use ApiPlatform\Metadata\ResourceMutatorInterface;
39
use ApiPlatform\Metadata\UriVariableTransformerInterface;
40
use ApiPlatform\Metadata\UrlGeneratorInterface;
41
use ApiPlatform\OpenApi\Model\Tag;
42
use ApiPlatform\RamseyUuid\Serializer\UuidDenormalizer;
43
use ApiPlatform\State\ApiResource\Error;
44
use ApiPlatform\State\ParameterProviderInterface;
45
use ApiPlatform\State\ProcessorInterface;
46
use ApiPlatform\State\ProviderInterface;
47
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRestrictionMetadataInterface;
48
use ApiPlatform\Symfony\Validator\ValidationGroupsGeneratorInterface;
49
use ApiPlatform\Validator\Exception\ValidationException;
50
use Doctrine\Persistence\ManagerRegistry;
51
use PHPStan\PhpDocParser\Parser\PhpDocParser;
52
use Ramsey\Uuid\Uuid;
53
use Symfony\Bundle\FrameworkBundle\Controller\ControllerHelper;
54
use Symfony\Component\Config\FileLocator;
55
use Symfony\Component\Config\Resource\DirectoryResource;
56
use Symfony\Component\DependencyInjection\ChildDefinition;
57
use Symfony\Component\DependencyInjection\ContainerBuilder;
58
use Symfony\Component\DependencyInjection\Definition;
59
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
60
use Symfony\Component\DependencyInjection\Extension\Extension;
61
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
62
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
63
use Symfony\Component\DependencyInjection\Reference;
64
use Symfony\Component\Finder\Finder;
65
use Symfony\Component\HttpClient\ScopingHttpClient;
66
use Symfony\Component\JsonStreamer\JsonStreamWriter;
67
use Symfony\Component\ObjectMapper\ObjectMapper;
68
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
69
use Symfony\Component\Uid\AbstractUid;
70
use Symfony\Component\Validator\Validator\ValidatorInterface;
71
use Symfony\Component\Yaml\Yaml;
72
use Twig\Environment;
73

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

107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function load(array $configs, ContainerBuilder $container): void
111
    {
UNCOV
112
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
×
113

UNCOV
114
        $configuration = new Configuration();
×
UNCOV
115
        $config = $this->processConfiguration($configuration, $configs);
×
UNCOV
116
        $container->setParameter('api_platform.use_symfony_listeners', $config['use_symfony_listeners']);
×
117

UNCOV
118
        $formats = $this->getFormats($config['formats']);
×
UNCOV
119
        $patchFormats = $this->getFormats($config['patch_formats']);
×
UNCOV
120
        $errorFormats = $this->getFormats($config['error_formats']);
×
UNCOV
121
        $docsFormats = $this->getFormats($config['docs_formats']);
×
122

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

UNCOV
133
        if (!$jsonSchemaFormats) {
×
UNCOV
134
            foreach (array_merge(array_keys($formats), array_keys($errorFormats)) as $f) {
×
135
                // Distinct JSON-based formats must have names that start with 'json'
UNCOV
136
                if (str_starts_with($f, 'json')) {
×
UNCOV
137
                    $jsonSchemaFormats[$f] = true;
×
138
                }
139
            }
140
        }
141

UNCOV
142
        if (!isset($errorFormats['json'])) {
×
UNCOV
143
            $errorFormats['json'] = ['application/problem+json', 'application/json'];
×
144
        }
145

UNCOV
146
        if (!isset($errorFormats['jsonproblem'])) {
×
147
            $errorFormats['jsonproblem'] = ['application/problem+json'];
×
148
        }
149

UNCOV
150
        if (isset($formats['jsonapi']) && !isset($patchFormats['jsonapi'])) {
×
UNCOV
151
            $patchFormats['jsonapi'] = ['application/vnd.api+json'];
×
152
        }
153

UNCOV
154
        $this->registerCommonConfiguration($container, $config, $loader, $formats, $patchFormats, $errorFormats, $docsFormats);
×
UNCOV
155
        $this->registerMetadataConfiguration($container, $config, $loader);
×
UNCOV
156
        $this->registerOAuthConfiguration($container, $config);
×
UNCOV
157
        $this->registerOpenApiConfiguration($container, $config, $loader);
×
UNCOV
158
        $this->registerSwaggerConfiguration($container, $config, $loader);
×
UNCOV
159
        $this->registerJsonApiConfiguration($formats, $loader, $config);
×
UNCOV
160
        $this->registerJsonLdHydraConfiguration($container, $formats, $loader, $config);
×
UNCOV
161
        $this->registerJsonHalConfiguration($formats, $loader);
×
UNCOV
162
        $this->registerJsonProblemConfiguration($errorFormats, $loader);
×
UNCOV
163
        $this->registerGraphQlConfiguration($container, $config, $loader);
×
UNCOV
164
        $this->registerCacheConfiguration($container);
×
UNCOV
165
        $this->registerDoctrineOrmConfiguration($container, $config, $loader);
×
UNCOV
166
        $this->registerDoctrineMongoDbOdmConfiguration($container, $config, $loader);
×
UNCOV
167
        $this->registerHttpCacheConfiguration($container, $config, $loader);
×
UNCOV
168
        $this->registerValidatorConfiguration($container, $config, $loader);
×
UNCOV
169
        $this->registerDataCollectorConfiguration($container, $config, $loader);
×
UNCOV
170
        $this->registerMercureConfiguration($container, $config, $loader);
×
UNCOV
171
        $this->registerMessengerConfiguration($container, $config, $loader);
×
UNCOV
172
        $this->registerElasticsearchConfiguration($container, $config, $loader);
×
UNCOV
173
        $this->registerSecurityConfiguration($container, $config, $loader);
×
UNCOV
174
        $this->registerMakerConfiguration($container, $config, $loader);
×
UNCOV
175
        $this->registerArgumentResolverConfiguration($loader);
×
UNCOV
176
        $this->registerLinkSecurityConfiguration($loader, $config);
×
UNCOV
177
        $this->registerJsonStreamerConfiguration($container, $loader, $formats, $config);
×
178

UNCOV
179
        if (class_exists(ObjectMapper::class)) {
×
UNCOV
180
            $loader->load('state/object_mapper.xml');
×
181
        }
UNCOV
182
        $container->registerForAutoconfiguration(FilterInterface::class)
×
UNCOV
183
            ->addTag('api_platform.filter');
×
UNCOV
184
        $container->registerForAutoconfiguration(ProviderInterface::class)
×
UNCOV
185
            ->addTag('api_platform.state_provider');
×
UNCOV
186
        $container->registerForAutoconfiguration(ProcessorInterface::class)
×
UNCOV
187
            ->addTag('api_platform.state_processor');
×
UNCOV
188
        $container->registerForAutoconfiguration(UriVariableTransformerInterface::class)
×
UNCOV
189
            ->addTag('api_platform.uri_variables.transformer');
×
UNCOV
190
        $container->registerForAutoconfiguration(ParameterProviderInterface::class)
×
UNCOV
191
            ->addTag('api_platform.parameter_provider');
×
UNCOV
192
        $container->registerAttributeForAutoconfiguration(ApiResource::class, static function (ChildDefinition $definition): void {
×
193
            $definition->setAbstract(true)
×
194
                ->addTag('api_platform.resource')
×
195
                ->addTag('container.excluded', ['source' => 'by #[ApiResource] attribute']);
×
UNCOV
196
        });
×
UNCOV
197
        $container->registerAttributeForAutoconfiguration(
×
UNCOV
198
            AsResourceMutator::class,
×
UNCOV
199
            static function (ChildDefinition $definition, AsResourceMutator $attribute, \ReflectionClass $reflector): void { // @phpstan-ignore-line
×
200
                if (!is_a($reflector->name, ResourceMutatorInterface::class, true)) {
×
201
                    throw new RuntimeException(\sprintf('Resource mutator "%s" should implement %s', $reflector->name, ResourceMutatorInterface::class));
×
202
                }
203

204
                $definition->addTag('api_platform.resource_mutator', [
×
205
                    'resourceClass' => $attribute->resourceClass,
×
206
                ]);
×
UNCOV
207
            },
×
UNCOV
208
        );
×
209

UNCOV
210
        $container->registerAttributeForAutoconfiguration(
×
UNCOV
211
            AsOperationMutator::class,
×
UNCOV
212
            static function (ChildDefinition $definition, AsOperationMutator $attribute, \ReflectionClass $reflector): void { // @phpstan-ignore-line
×
213
                if (!is_a($reflector->name, OperationMutatorInterface::class, true)) {
×
214
                    throw new RuntimeException(\sprintf('Operation mutator "%s" should implement %s', $reflector->name, OperationMutatorInterface::class));
×
215
                }
216

217
                $definition->addTag('api_platform.operation_mutator', [
×
218
                    'operationName' => $attribute->operationName,
×
219
                ]);
×
UNCOV
220
            },
×
UNCOV
221
        );
×
222

UNCOV
223
        if (!$container->has('api_platform.state.item_provider')) {
×
224
            $container->setAlias('api_platform.state.item_provider', 'api_platform.state_provider.object');
×
225
        }
226

UNCOV
227
        if ($container->getParameter('kernel.debug')) {
×
UNCOV
228
            $this->injectStopwatch($container);
×
229
        }
230
    }
231

232
    private function injectStopwatch(ContainerBuilder $container): void
233
    {
UNCOV
234
        $services = [
×
UNCOV
235
            'api_platform.state_processor.add_link_header',
×
UNCOV
236
            'api_platform.state_processor.respond',
×
UNCOV
237
            'api_platform.state_processor.serialize',
×
UNCOV
238
            'api_platform.state_processor.write',
×
UNCOV
239
            'api_platform.state_provider.content_negotiation',
×
UNCOV
240
            'api_platform.state_provider.deserialize',
×
UNCOV
241
            'api_platform.state_provider.parameter',
×
UNCOV
242
            'api_platform.state_provider.read',
×
UNCOV
243
        ];
×
244

UNCOV
245
        foreach ($services as $id) {
×
UNCOV
246
            if (!$container->hasDefinition($id)) {
×
247
                continue;
×
248
            }
249

UNCOV
250
            $definition = $container->getDefinition($id);
×
UNCOV
251
            $definition->addMethodCall('setStopwatch', [new Reference('debug.stopwatch', ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)]);
×
252
        }
253
    }
254

255
    private function registerCommonConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader, array $formats, array $patchFormats, array $errorFormats, array $docsFormats): void
256
    {
UNCOV
257
        $loader->load('state/state.xml');
×
UNCOV
258
        $loader->load('symfony/symfony.xml');
×
UNCOV
259
        $loader->load('api.xml');
×
UNCOV
260
        $loader->load('filter.xml');
×
261

UNCOV
262
        if (class_exists(UuidDenormalizer::class) && class_exists(Uuid::class)) {
×
UNCOV
263
            $loader->load('ramsey_uuid.xml');
×
264
        }
265

UNCOV
266
        if (class_exists(AbstractUid::class)) {
×
UNCOV
267
            $loader->load('symfony/uid.xml');
×
268
        }
269

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

UNCOV
272
        $container->setParameter('api_platform.serializer.default_context', $defaultContext);
×
UNCOV
273
        if (!$container->hasParameter('serializer.default_context')) {
×
UNCOV
274
            $container->setParameter('serializer.default_context', $container->getParameter('api_platform.serializer.default_context'));
×
275
        }
UNCOV
276
        if ($config['use_symfony_listeners']) {
×
277
            $loader->load('symfony/events.xml');
×
278
        } else {
UNCOV
279
            $loader->load('symfony/controller.xml');
×
UNCOV
280
            $loader->load('state/provider.xml');
×
UNCOV
281
            $loader->load('state/processor.xml');
×
282
        }
UNCOV
283
        $loader->load('state/parameter_provider.xml');
×
284

UNCOV
285
        $container->setParameter('api_platform.enable_entrypoint', $config['enable_entrypoint']);
×
UNCOV
286
        $container->setParameter('api_platform.enable_docs', $config['enable_docs']);
×
UNCOV
287
        $container->setParameter('api_platform.title', $config['title']);
×
UNCOV
288
        $container->setParameter('api_platform.description', $config['description']);
×
UNCOV
289
        $container->setParameter('api_platform.version', $config['version']);
×
UNCOV
290
        $container->setParameter('api_platform.show_webby', $config['show_webby']);
×
UNCOV
291
        $container->setParameter('api_platform.url_generation_strategy', $config['defaults']['url_generation_strategy'] ?? UrlGeneratorInterface::ABS_PATH);
×
UNCOV
292
        $container->setParameter('api_platform.exception_to_status', $config['exception_to_status']);
×
UNCOV
293
        $container->setParameter('api_platform.formats', $formats);
×
UNCOV
294
        $container->setParameter('api_platform.patch_formats', $patchFormats);
×
UNCOV
295
        $container->setParameter('api_platform.error_formats', $errorFormats);
×
UNCOV
296
        $container->setParameter('api_platform.docs_formats', $docsFormats);
×
UNCOV
297
        $container->setParameter('api_platform.jsonschema_formats', []);
×
UNCOV
298
        $container->setParameter('api_platform.eager_loading.enabled', $this->isConfigEnabled($container, $config['eager_loading']));
×
UNCOV
299
        $container->setParameter('api_platform.eager_loading.max_joins', $config['eager_loading']['max_joins']);
×
UNCOV
300
        $container->setParameter('api_platform.eager_loading.fetch_partial', $config['eager_loading']['fetch_partial']);
×
UNCOV
301
        $container->setParameter('api_platform.eager_loading.force_eager', $config['eager_loading']['force_eager']);
×
UNCOV
302
        $container->setParameter('api_platform.collection.exists_parameter_name', $config['collection']['exists_parameter_name']);
×
UNCOV
303
        $container->setParameter('api_platform.collection.order', $config['collection']['order']);
×
UNCOV
304
        $container->setParameter('api_platform.collection.order_parameter_name', $config['collection']['order_parameter_name']);
×
UNCOV
305
        $container->setParameter('api_platform.collection.order_nulls_comparison', $config['collection']['order_nulls_comparison']);
×
UNCOV
306
        $container->setParameter('api_platform.collection.pagination.enabled', $config['defaults']['pagination_enabled'] ?? true);
×
UNCOV
307
        $container->setParameter('api_platform.collection.pagination.partial', $config['defaults']['pagination_partial'] ?? false);
×
UNCOV
308
        $container->setParameter('api_platform.collection.pagination.client_enabled', $config['defaults']['pagination_client_enabled'] ?? false);
×
UNCOV
309
        $container->setParameter('api_platform.collection.pagination.client_items_per_page', $config['defaults']['pagination_client_items_per_page'] ?? false);
×
UNCOV
310
        $container->setParameter('api_platform.collection.pagination.client_partial', $config['defaults']['pagination_client_partial'] ?? false);
×
UNCOV
311
        $container->setParameter('api_platform.collection.pagination.items_per_page', $config['defaults']['pagination_items_per_page'] ?? 30);
×
UNCOV
312
        $container->setParameter('api_platform.collection.pagination.maximum_items_per_page', $config['defaults']['pagination_maximum_items_per_page'] ?? 30);
×
UNCOV
313
        $container->setParameter('api_platform.collection.pagination.page_parameter_name', $config['defaults']['pagination_page_parameter_name'] ?? $config['collection']['pagination']['page_parameter_name']);
×
UNCOV
314
        $container->setParameter('api_platform.collection.pagination.enabled_parameter_name', $config['defaults']['pagination_enabled_parameter_name'] ?? $config['collection']['pagination']['enabled_parameter_name']);
×
UNCOV
315
        $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
316
        $container->setParameter('api_platform.collection.pagination.partial_parameter_name', $config['defaults']['pagination_partial_parameter_name'] ?? $config['collection']['pagination']['partial_parameter_name']);
×
UNCOV
317
        $container->setParameter('api_platform.collection.pagination', $this->getPaginationDefaults($config['defaults'] ?? [], $config['collection']['pagination']));
×
UNCOV
318
        $container->setParameter('api_platform.handle_symfony_errors', $config['handle_symfony_errors'] ?? false);
×
UNCOV
319
        $container->setParameter('api_platform.http_cache.etag', $config['defaults']['cache_headers']['etag'] ?? true);
×
UNCOV
320
        $container->setParameter('api_platform.http_cache.max_age', $config['defaults']['cache_headers']['max_age'] ?? null);
×
UNCOV
321
        $container->setParameter('api_platform.http_cache.shared_max_age', $config['defaults']['cache_headers']['shared_max_age'] ?? null);
×
UNCOV
322
        $container->setParameter('api_platform.http_cache.vary', $config['defaults']['cache_headers']['vary'] ?? ['Accept']);
×
UNCOV
323
        $container->setParameter('api_platform.http_cache.public', $config['defaults']['cache_headers']['public'] ?? $config['http_cache']['public']);
×
UNCOV
324
        $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
325
        $container->setParameter('api_platform.http_cache.invalidation.xkey.glue', $config['defaults']['cache_headers']['invalidation']['xkey']['glue'] ?? $config['http_cache']['invalidation']['xkey']['glue']);
×
326

UNCOV
327
        $container->setAlias('api_platform.path_segment_name_generator', $config['path_segment_name_generator']);
×
UNCOV
328
        $container->setAlias('api_platform.inflector', $config['inflector']);
×
329

UNCOV
330
        if ($config['name_converter']) {
×
UNCOV
331
            $container->setAlias('api_platform.name_converter', $config['name_converter']);
×
332
        }
UNCOV
333
        $container->setParameter('api_platform.asset_package', $config['asset_package']);
×
UNCOV
334
        $container->setParameter('api_platform.defaults', $this->normalizeDefaults($config['defaults'] ?? []));
×
335

UNCOV
336
        if ($container->getParameter('kernel.debug')) {
×
UNCOV
337
            $container->removeDefinition('api_platform.serializer.mapping.cache_class_metadata_factory');
×
338
        }
339
    }
340

341
    /**
342
     * This method will be removed in 3.0 when "defaults" will be the regular configuration path for the pagination.
343
     */
344
    private function getPaginationDefaults(array $defaults, array $collectionPaginationConfiguration): array
345
    {
UNCOV
346
        $paginationOptions = [];
×
347

UNCOV
348
        foreach ($defaults as $key => $value) {
×
UNCOV
349
            if (!str_starts_with($key, 'pagination_')) {
×
UNCOV
350
                continue;
×
351
            }
352

UNCOV
353
            $paginationOptions[str_replace('pagination_', '', $key)] = $value;
×
354
        }
355

UNCOV
356
        return array_merge($collectionPaginationConfiguration, $paginationOptions);
×
357
    }
358

359
    private function normalizeDefaults(array $defaults): array
360
    {
UNCOV
361
        $normalizedDefaults = ['extra_properties' => $defaults['extra_properties'] ?? []];
×
UNCOV
362
        unset($defaults['extra_properties']);
×
363

UNCOV
364
        $rc = new \ReflectionClass(ApiResource::class);
×
UNCOV
365
        $publicProperties = [];
×
UNCOV
366
        foreach ($rc->getConstructor()->getParameters() as $param) {
×
UNCOV
367
            $publicProperties[$param->getName()] = true;
×
368
        }
369

UNCOV
370
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();
×
UNCOV
371
        foreach ($defaults as $option => $value) {
×
UNCOV
372
            if (isset($publicProperties[$nameConverter->denormalize($option)])) {
×
UNCOV
373
                $normalizedDefaults[$option] = $value;
×
374

UNCOV
375
                continue;
×
376
            }
377

378
            $normalizedDefaults['extra_properties'][$option] = $value;
×
379
        }
380

UNCOV
381
        return $normalizedDefaults;
×
382
    }
383

384
    private function registerMetadataConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
385
    {
UNCOV
386
        [$xmlResources, $yamlResources, $phpResources] = $this->getResourcesToWatch($container, $config);
×
387

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

UNCOV
390
        $loader->load('metadata/resource_name.xml');
×
UNCOV
391
        $loader->load('metadata/property_name.xml');
×
392

UNCOV
393
        if (!empty($config['resource_class_directories'])) {
×
394
            $container->setParameter('api_platform.resource_class_directories', array_merge(
×
395
                $config['resource_class_directories'],
×
396
                $container->getParameter('api_platform.resource_class_directories')
×
397
            ));
×
398
        }
399

400
        // V3 metadata
UNCOV
401
        $loader->load('metadata/php.xml');
×
UNCOV
402
        $loader->load('metadata/xml.xml');
×
UNCOV
403
        $loader->load('metadata/links.xml');
×
UNCOV
404
        $loader->load('metadata/property.xml');
×
UNCOV
405
        $loader->load('metadata/resource.xml');
×
UNCOV
406
        $loader->load('metadata/operation.xml');
×
UNCOV
407
        $loader->load('metadata/mutator.xml');
×
408

UNCOV
409
        $container->getDefinition('api_platform.metadata.resource_extractor.xml')->replaceArgument(0, $xmlResources);
×
UNCOV
410
        $container->getDefinition('api_platform.metadata.property_extractor.xml')->replaceArgument(0, $xmlResources);
×
411

UNCOV
412
        if (class_exists(PhpDocParser::class)) {
×
UNCOV
413
            $loader->load('metadata/php_doc.xml');
×
414
        }
415

UNCOV
416
        if (class_exists(Yaml::class)) {
×
UNCOV
417
            $loader->load('metadata/yaml.xml');
×
UNCOV
418
            $container->getDefinition('api_platform.metadata.resource_extractor.yaml')->replaceArgument(0, $yamlResources);
×
UNCOV
419
            $container->getDefinition('api_platform.metadata.property_extractor.yaml')->replaceArgument(0, $yamlResources);
×
420
        }
421

UNCOV
422
        $container->getDefinition('api_platform.metadata.resource_extractor.php_file')->replaceArgument(0, $phpResources);
×
423
    }
424

425
    private function getClassNameResources(): array
426
    {
UNCOV
427
        return [
×
UNCOV
428
            Error::class,
×
UNCOV
429
            ValidationException::class,
×
UNCOV
430
        ];
×
431
    }
432

433
    private function getBundlesResourcesPaths(ContainerBuilder $container, array $config): array
434
    {
UNCOV
435
        $bundlesResourcesPaths = [];
×
436

UNCOV
437
        foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
×
UNCOV
438
            $dirname = $bundle['path'];
×
UNCOV
439
            $paths = [
×
UNCOV
440
                "$dirname/ApiResource",
×
UNCOV
441
                "$dirname/src/ApiResource",
×
UNCOV
442
            ];
×
UNCOV
443
            foreach (['.yaml', '.yml', '.xml', ''] as $extension) {
×
UNCOV
444
                $paths[] = "$dirname/Resources/config/api_resources$extension";
×
UNCOV
445
                $paths[] = "$dirname/config/api_resources$extension";
×
446
            }
UNCOV
447
            if ($this->isConfigEnabled($container, $config['doctrine'])) {
×
UNCOV
448
                $paths[] = "$dirname/Entity";
×
UNCOV
449
                $paths[] = "$dirname/src/Entity";
×
450
            }
UNCOV
451
            if ($this->isConfigEnabled($container, $config['doctrine_mongodb_odm'])) {
×
452
                $paths[] = "$dirname/Document";
×
453
                $paths[] = "$dirname/src/Document";
×
454
            }
455

UNCOV
456
            foreach ($paths as $path) {
×
UNCOV
457
                if ($container->fileExists($path, false)) {
×
UNCOV
458
                    $bundlesResourcesPaths[] = $path;
×
459
                }
460
            }
461
        }
462

UNCOV
463
        return $bundlesResourcesPaths;
×
464
    }
465

466
    private function getResourcesToWatch(ContainerBuilder $container, array $config): array
467
    {
UNCOV
468
        $paths = array_unique(array_merge($this->getBundlesResourcesPaths($container, $config), $config['mapping']['paths']));
×
469

UNCOV
470
        if (!$config['mapping']['paths']) {
×
471
            $projectDir = $container->getParameter('kernel.project_dir');
×
472
            foreach (["$projectDir/config/api_platform", "$projectDir/src/ApiResource"] as $dir) {
×
473
                if (is_dir($dir)) {
×
474
                    $paths[] = $dir;
×
475
                }
476
            }
477

478
            if ($this->isConfigEnabled($container, $config['doctrine']) && is_dir($doctrinePath = "$projectDir/src/Entity")) {
×
479
                $paths[] = $doctrinePath;
×
480
            }
481

482
            if ($this->isConfigEnabled($container, $config['doctrine_mongodb_odm']) && is_dir($documentPath = "$projectDir/src/Document")) {
×
483
                $paths[] = $documentPath;
×
484
            }
485
        }
486

UNCOV
487
        $resources = ['yml' => [], 'xml' => [], 'php' => [], 'dir' => []];
×
488

UNCOV
489
        foreach ($config['mapping']['imports'] ?? [] as $path) {
×
490
            if (is_dir($path)) {
×
491
                foreach (Finder::create()->followLinks()->files()->in($path)->name('/\.php$/')->sortByName() as $file) {
×
492
                    $resources[$file->getExtension()][] = $file->getRealPath();
×
493
                }
494

495
                $resources['dir'][] = $path;
×
496
                $container->addResource(new DirectoryResource($path, '/\.php$/'));
×
497

498
                continue;
×
499
            }
500

501
            if ($container->fileExists($path, false)) {
×
502
                if (!str_ends_with($path, '.php')) {
×
503
                    throw new RuntimeException(\sprintf('Unsupported mapping type in "%s", supported type is PHP.', $path));
×
504
                }
505

506
                $resources['php'][] = $path;
×
507

508
                continue;
×
509
            }
510

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

UNCOV
514
        foreach ($paths as $path) {
×
UNCOV
515
            if (is_dir($path)) {
×
UNCOV
516
                foreach (Finder::create()->followLinks()->files()->in($path)->name('/\.(xml|ya?ml)$/')->sortByName() as $file) {
×
UNCOV
517
                    $resources['yaml' === ($extension = $file->getExtension()) ? 'yml' : $extension][] = $file->getRealPath();
×
518
                }
519

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

UNCOV
523
                continue;
×
524
            }
525

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

531
                $resources['yaml' === $matches[1] ? 'yml' : $matches[1]][] = $path;
×
532

533
                continue;
×
534
            }
535

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

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

UNCOV
541
        return [$resources['xml'], $resources['yml'], $resources['php']];
×
542
    }
543

544
    private function registerOAuthConfiguration(ContainerBuilder $container, array $config): void
545
    {
UNCOV
546
        if (!$config['oauth']) {
×
547
            return;
×
548
        }
549

UNCOV
550
        $container->setParameter('api_platform.oauth.enabled', $this->isConfigEnabled($container, $config['oauth']));
×
UNCOV
551
        $container->setParameter('api_platform.oauth.clientId', $config['oauth']['clientId']);
×
UNCOV
552
        $container->setParameter('api_platform.oauth.clientSecret', $config['oauth']['clientSecret']);
×
UNCOV
553
        $container->setParameter('api_platform.oauth.type', $config['oauth']['type']);
×
UNCOV
554
        $container->setParameter('api_platform.oauth.flow', $config['oauth']['flow']);
×
UNCOV
555
        $container->setParameter('api_platform.oauth.tokenUrl', $config['oauth']['tokenUrl']);
×
UNCOV
556
        $container->setParameter('api_platform.oauth.authorizationUrl', $config['oauth']['authorizationUrl']);
×
UNCOV
557
        $container->setParameter('api_platform.oauth.refreshUrl', $config['oauth']['refreshUrl']);
×
UNCOV
558
        $container->setParameter('api_platform.oauth.scopes', $config['oauth']['scopes']);
×
UNCOV
559
        $container->setParameter('api_platform.oauth.pkce', $config['oauth']['pkce']);
×
560
    }
561

562
    /**
563
     * Registers the Swagger, ReDoc and Swagger UI configuration.
564
     */
565
    private function registerSwaggerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
566
    {
UNCOV
567
        foreach (array_keys($config['swagger']['api_keys']) as $keyName) {
×
UNCOV
568
            if (!preg_match('/^[a-zA-Z0-9._-]+$/', $keyName)) {
×
569
                throw new RuntimeException(\sprintf('The swagger api_keys key "%s" is not valid, it should match "^[a-zA-Z0-9._-]+$"', $keyName));
×
570
            }
571
        }
572

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

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

UNCOV
579
        if (!$config['enable_swagger']) {
×
580
            return;
×
581
        }
582

UNCOV
583
        $loader->load('openapi.xml');
×
584

UNCOV
585
        if (class_exists(Yaml::class)) {
×
UNCOV
586
            $loader->load('openapi/yaml.xml');
×
587
        }
588

UNCOV
589
        $loader->load('swagger_ui.xml');
×
590

UNCOV
591
        if ($config['use_symfony_listeners']) {
×
592
            $loader->load('symfony/swagger_ui.xml');
×
593
        }
594

UNCOV
595
        if ($config['enable_swagger_ui']) {
×
UNCOV
596
            $loader->load('state/swagger_ui.xml');
×
597
        }
598

UNCOV
599
        if (!$config['enable_swagger_ui'] && !$config['enable_re_doc']) {
×
600
            // Remove the listener but keep the controller to allow customizing the path of the UI
601
            $container->removeDefinition('api_platform.swagger.listener.ui');
×
602
        }
603

UNCOV
604
        $container->setParameter('api_platform.enable_swagger_ui', $config['enable_swagger_ui']);
×
UNCOV
605
        $container->setParameter('api_platform.enable_re_doc', $config['enable_re_doc']);
×
UNCOV
606
        $container->setParameter('api_platform.swagger.api_keys', $config['swagger']['api_keys']);
×
UNCOV
607
        $container->setParameter('api_platform.swagger.persist_authorization', $config['swagger']['persist_authorization']);
×
UNCOV
608
        $container->setParameter('api_platform.swagger.http_auth', $config['swagger']['http_auth']);
×
UNCOV
609
        if ($config['openapi']['swagger_ui_extra_configuration'] && $config['swagger']['swagger_ui_extra_configuration']) {
×
610
            throw new RuntimeException('You can not set "swagger_ui_extra_configuration" twice - in "openapi" and "swagger" section.');
×
611
        }
UNCOV
612
        $container->setParameter('api_platform.swagger_ui.extra_configuration', $config['openapi']['swagger_ui_extra_configuration'] ?: $config['swagger']['swagger_ui_extra_configuration']);
×
613
    }
614

615
    private function registerJsonApiConfiguration(array $formats, XmlFileLoader $loader, array $config): void
616
    {
UNCOV
617
        if (!isset($formats['jsonapi'])) {
×
618
            return;
×
619
        }
620

UNCOV
621
        $loader->load('jsonapi.xml');
×
UNCOV
622
        $loader->load('state/jsonapi.xml');
×
623
    }
624

625
    private function registerJsonLdHydraConfiguration(ContainerBuilder $container, array $formats, XmlFileLoader $loader, array $config): void
626
    {
UNCOV
627
        if (!isset($formats['jsonld'])) {
×
628
            return;
×
629
        }
630

UNCOV
631
        if ($config['use_symfony_listeners']) {
×
632
            $loader->load('symfony/jsonld.xml');
×
633
        } else {
UNCOV
634
            $loader->load('state/jsonld.xml');
×
635
        }
636

UNCOV
637
        $loader->load('state/hydra.xml');
×
UNCOV
638
        $loader->load('jsonld.xml');
×
UNCOV
639
        $loader->load('hydra.xml');
×
640

UNCOV
641
        if (!$container->has('api_platform.json_schema.schema_factory')) {
×
642
            $container->removeDefinition('api_platform.hydra.json_schema.schema_factory');
×
643
        }
644
    }
645

646
    private function registerJsonHalConfiguration(array $formats, XmlFileLoader $loader): void
647
    {
UNCOV
648
        if (!isset($formats['jsonhal'])) {
×
649
            return;
×
650
        }
651

UNCOV
652
        $loader->load('hal.xml');
×
653
    }
654

655
    private function registerJsonProblemConfiguration(array $errorFormats, XmlFileLoader $loader): void
656
    {
UNCOV
657
        if (!isset($errorFormats['jsonproblem'])) {
×
658
            return;
×
659
        }
660

UNCOV
661
        $loader->load('problem.xml');
×
662
    }
663

664
    private function registerGraphQlConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
665
    {
UNCOV
666
        $enabled = $this->isConfigEnabled($container, $config['graphql']);
×
UNCOV
667
        $graphqlIntrospectionEnabled = $enabled && $this->isConfigEnabled($container, $config['graphql']['introspection']);
×
UNCOV
668
        $graphiqlEnabled = $enabled && $this->isConfigEnabled($container, $config['graphql']['graphiql']);
×
UNCOV
669
        $maxQueryDepth = (int) $config['graphql']['max_query_depth'];
×
UNCOV
670
        $maxQueryComplexity = (int) $config['graphql']['max_query_complexity'];
×
671

UNCOV
672
        $container->setParameter('api_platform.graphql.enabled', $enabled);
×
UNCOV
673
        $container->setParameter('api_platform.graphql.max_query_depth', $maxQueryDepth);
×
UNCOV
674
        $container->setParameter('api_platform.graphql.max_query_complexity', $maxQueryComplexity);
×
UNCOV
675
        $container->setParameter('api_platform.graphql.introspection.enabled', $graphqlIntrospectionEnabled);
×
UNCOV
676
        $container->setParameter('api_platform.graphql.graphiql.enabled', $graphiqlEnabled);
×
UNCOV
677
        $container->setParameter('api_platform.graphql.collection.pagination', $config['graphql']['collection']['pagination']);
×
678

UNCOV
679
        if (!$enabled) {
×
680
            return;
×
681
        }
682

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

UNCOV
687
        $container->setParameter('api_platform.graphql.default_ide', $config['graphql']['default_ide']);
×
UNCOV
688
        $container->setParameter('api_platform.graphql.nesting_separator', $config['graphql']['nesting_separator']);
×
689

UNCOV
690
        $loader->load('graphql.xml');
×
691

UNCOV
692
        if (!class_exists(Environment::class) || !isset($container->getParameter('kernel.bundles')['TwigBundle'])) {
×
693
            if ($graphiqlEnabled) {
×
694
                throw new RuntimeException(\sprintf('GraphiQL interfaces depend on Twig. Please activate TwigBundle for the %s environnement or disable GraphiQL.', $container->getParameter('kernel.environment')));
×
695
            }
696
            $container->removeDefinition('api_platform.graphql.action.graphiql');
×
697
        }
698

UNCOV
699
        $container->registerForAutoconfiguration(QueryItemResolverInterface::class)
×
UNCOV
700
            ->addTag('api_platform.graphql.resolver');
×
UNCOV
701
        $container->registerForAutoconfiguration(QueryCollectionResolverInterface::class)
×
UNCOV
702
            ->addTag('api_platform.graphql.resolver');
×
UNCOV
703
        $container->registerForAutoconfiguration(MutationResolverInterface::class)
×
UNCOV
704
            ->addTag('api_platform.graphql.resolver');
×
UNCOV
705
        $container->registerForAutoconfiguration(GraphQlTypeInterface::class)
×
UNCOV
706
            ->addTag('api_platform.graphql.type');
×
UNCOV
707
        $container->registerForAutoconfiguration(ErrorHandlerInterface::class)
×
UNCOV
708
            ->addTag('api_platform.graphql.error_handler');
×
709
    }
710

711
    private function registerCacheConfiguration(ContainerBuilder $container): void
712
    {
UNCOV
713
        if (!$container->hasParameter('kernel.debug') || !$container->getParameter('kernel.debug')) {
×
714
            $container->removeDefinition('api_platform.cache_warmer.cache_pool_clearer');
×
715
        }
716
    }
717

718
    private function registerDoctrineOrmConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
719
    {
UNCOV
720
        if (!$this->isConfigEnabled($container, $config['doctrine'])) {
×
721
            return;
×
722
        }
723

724
        // For older versions of doctrine bridge this allows autoconfiguration for filters
UNCOV
725
        if (!$container->has(ManagerRegistry::class)) {
×
UNCOV
726
            $container->setAlias(ManagerRegistry::class, 'doctrine');
×
727
        }
728

UNCOV
729
        $container->registerForAutoconfiguration(QueryItemExtensionInterface::class)
×
UNCOV
730
            ->addTag('api_platform.doctrine.orm.query_extension.item');
×
UNCOV
731
        $container->registerForAutoconfiguration(DoctrineQueryCollectionExtensionInterface::class)
×
UNCOV
732
            ->addTag('api_platform.doctrine.orm.query_extension.collection');
×
UNCOV
733
        $container->registerForAutoconfiguration(DoctrineOrmAbstractFilter::class);
×
734

UNCOV
735
        $container->registerForAutoconfiguration(OrmLinksHandlerInterface::class)
×
UNCOV
736
            ->addTag('api_platform.doctrine.orm.links_handler');
×
737

UNCOV
738
        $loader->load('doctrine_orm.xml');
×
739

UNCOV
740
        if ($this->isConfigEnabled($container, $config['eager_loading'])) {
×
UNCOV
741
            return;
×
742
        }
743

744
        $container->removeAlias(EagerLoadingExtension::class);
×
745
        $container->removeDefinition('api_platform.doctrine.orm.query_extension.eager_loading');
×
746
        $container->removeAlias(FilterEagerLoadingExtension::class);
×
747
        $container->removeDefinition('api_platform.doctrine.orm.query_extension.filter_eager_loading');
×
748
    }
749

750
    private function registerDoctrineMongoDbOdmConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
751
    {
UNCOV
752
        if (!$this->isConfigEnabled($container, $config['doctrine_mongodb_odm'])) {
×
UNCOV
753
            return;
×
754
        }
755

756
        $container->registerForAutoconfiguration(AggregationItemExtensionInterface::class)
×
757
            ->addTag('api_platform.doctrine_mongodb.odm.aggregation_extension.item');
×
758
        $container->registerForAutoconfiguration(AggregationCollectionExtensionInterface::class)
×
759
            ->addTag('api_platform.doctrine_mongodb.odm.aggregation_extension.collection');
×
760
        $container->registerForAutoconfiguration(DoctrineMongoDbOdmAbstractFilter::class)
×
761
            ->setBindings(['$managerRegistry' => new Reference('doctrine_mongodb')]);
×
762
        $container->registerForAutoconfiguration(OdmLinksHandlerInterface::class)
×
763
            ->addTag('api_platform.doctrine.odm.links_handler');
×
764

765
        $loader->load('doctrine_mongodb_odm.xml');
×
766
    }
767

768
    private function registerHttpCacheConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
769
    {
UNCOV
770
        $loader->load('http_cache.xml');
×
771

UNCOV
772
        if (!$this->isConfigEnabled($container, $config['http_cache']['invalidation'])) {
×
773
            return;
×
774
        }
775

UNCOV
776
        if ($this->isConfigEnabled($container, $config['doctrine'])) {
×
UNCOV
777
            $loader->load('doctrine_orm_http_cache_purger.xml');
×
778
        }
779

UNCOV
780
        $loader->load('state/http_cache_purger.xml');
×
UNCOV
781
        $loader->load('http_cache_purger.xml');
×
782

UNCOV
783
        foreach ($config['http_cache']['invalidation']['scoped_clients'] as $client) {
×
784
            $definition = $container->getDefinition($client);
×
785
            $definition->addTag('api_platform.http_cache.http_client');
×
786
        }
787

UNCOV
788
        if (!($urls = $config['http_cache']['invalidation']['urls'])) {
×
UNCOV
789
            $urls = $config['http_cache']['invalidation']['varnish_urls'];
×
790
        }
791

UNCOV
792
        foreach ($urls as $key => $url) {
×
793
            $definition = new Definition(ScopingHttpClient::class, [new Reference('http_client'), $url, ['base_uri' => $url] + $config['http_cache']['invalidation']['request_options']]);
×
794
            $definition->setFactory([ScopingHttpClient::class, 'forBaseUri']);
×
795
            $definition->addTag('api_platform.http_cache.http_client');
×
796
            $container->setDefinition('api_platform.invalidation_http_client.'.$key, $definition);
×
797
        }
798

UNCOV
799
        $serviceName = $config['http_cache']['invalidation']['purger'];
×
800

UNCOV
801
        if (!$container->hasDefinition('api_platform.http_cache.purger')) {
×
UNCOV
802
            $container->setAlias('api_platform.http_cache.purger', $serviceName);
×
803
        }
804
    }
805

806
    /**
807
     * Normalizes the format from config to the one accepted by Symfony HttpFoundation.
808
     */
809
    private function getFormats(array $configFormats): array
810
    {
UNCOV
811
        $formats = [];
×
UNCOV
812
        foreach ($configFormats as $format => $value) {
×
UNCOV
813
            foreach ($value['mime_types'] as $mimeType) {
×
UNCOV
814
                $formats[$format][] = $mimeType;
×
815
            }
816
        }
817

UNCOV
818
        return $formats;
×
819
    }
820

821
    private function registerValidatorConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
822
    {
UNCOV
823
        if (interface_exists(ValidatorInterface::class)) {
×
UNCOV
824
            $loader->load('metadata/validator.xml');
×
UNCOV
825
            $loader->load('validator/validator.xml');
×
826

UNCOV
827
            if ($this->isConfigEnabled($container, $config['graphql'])) {
×
UNCOV
828
                $loader->load('graphql/validator.xml');
×
829
            }
830

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

UNCOV
833
            $container->registerForAutoconfiguration(ValidationGroupsGeneratorInterface::class)
×
UNCOV
834
                ->addTag('api_platform.validation_groups_generator');
×
UNCOV
835
            $container->registerForAutoconfiguration(PropertySchemaRestrictionMetadataInterface::class)
×
UNCOV
836
                ->addTag('api_platform.metadata.property_schema_restriction');
×
837
        }
838

UNCOV
839
        if (!$config['validator']) {
×
840
            return;
×
841
        }
842

UNCOV
843
        $container->setParameter('api_platform.validator.serialize_payload_fields', $config['validator']['serialize_payload_fields']);
×
844
    }
845

846
    private function registerDataCollectorConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
847
    {
UNCOV
848
        if (!$config['enable_profiler']) {
×
849
            return;
×
850
        }
851

UNCOV
852
        $loader->load('data_collector.xml');
×
853

UNCOV
854
        if ($container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug')) {
×
UNCOV
855
            $loader->load('debug.xml');
×
856
        }
857
    }
858

859
    private function registerMercureConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
860
    {
UNCOV
861
        if (!$this->isConfigEnabled($container, $config['mercure'])) {
×
862
            return;
×
863
        }
864

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

UNCOV
868
        if ($this->isConfigEnabled($container, $config['doctrine'])) {
×
UNCOV
869
            $loader->load('doctrine_orm_mercure_publisher.xml');
×
870
        }
UNCOV
871
        if ($this->isConfigEnabled($container, $config['doctrine_mongodb_odm'])) {
×
872
            $loader->load('doctrine_odm_mercure_publisher.xml');
×
873
        }
874

UNCOV
875
        if ($this->isConfigEnabled($container, $config['graphql'])) {
×
UNCOV
876
            $loader->load('graphql_mercure.xml');
×
877
        }
878
    }
879

880
    private function registerMessengerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
881
    {
UNCOV
882
        if (!$this->isConfigEnabled($container, $config['messenger'])) {
×
883
            return;
×
884
        }
885

UNCOV
886
        $loader->load('messenger.xml');
×
887
    }
888

889
    private function registerElasticsearchConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
890
    {
UNCOV
891
        $enabled = $this->isConfigEnabled($container, $config['elasticsearch']);
×
892

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

UNCOV
895
        if (!$enabled) {
×
UNCOV
896
            return;
×
897
        }
898

899
        $clientClass = !class_exists(\Elasticsearch\Client::class)
×
900
            // ES v7
×
901
            ? \Elastic\Elasticsearch\Client::class
×
902
            // ES v8 and up
×
903
            : \Elasticsearch\Client::class;
×
904

905
        $clientDefinition = new Definition($clientClass);
×
906
        $container->setDefinition('api_platform.elasticsearch.client', $clientDefinition);
×
907
        $container->registerForAutoconfiguration(RequestBodySearchCollectionExtensionInterface::class)
×
908
            ->addTag('api_platform.elasticsearch.request_body_search_extension.collection');
×
909
        $container->setParameter('api_platform.elasticsearch.hosts', $config['elasticsearch']['hosts']);
×
910
        $loader->load('elasticsearch.xml');
×
911
    }
912

913
    private function registerSecurityConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
914
    {
915
        /** @var string[] $bundles */
UNCOV
916
        $bundles = $container->getParameter('kernel.bundles');
×
917

UNCOV
918
        if (!isset($bundles['SecurityBundle'])) {
×
919
            return;
×
920
        }
921

UNCOV
922
        $loader->load('security.xml');
×
923

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

UNCOV
926
        if (interface_exists(ValidatorInterface::class)) {
×
UNCOV
927
            $loader->load('state/security_validator.xml');
×
928
        }
929

UNCOV
930
        if ($this->isConfigEnabled($container, $config['graphql'])) {
×
UNCOV
931
            $loader->load('graphql/security.xml');
×
932
        }
933
    }
934

935
    private function registerOpenApiConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
936
    {
UNCOV
937
        $container->setParameter('api_platform.openapi.termsOfService', $config['openapi']['termsOfService']);
×
UNCOV
938
        $container->setParameter('api_platform.openapi.contact.name', $config['openapi']['contact']['name']);
×
UNCOV
939
        $container->setParameter('api_platform.openapi.contact.url', $config['openapi']['contact']['url']);
×
UNCOV
940
        $container->setParameter('api_platform.openapi.contact.email', $config['openapi']['contact']['email']);
×
UNCOV
941
        $container->setParameter('api_platform.openapi.license.name', $config['openapi']['license']['name']);
×
UNCOV
942
        $container->setParameter('api_platform.openapi.license.url', $config['openapi']['license']['url']);
×
UNCOV
943
        $container->setParameter('api_platform.openapi.license.identifier', $config['openapi']['license']['identifier']);
×
UNCOV
944
        $container->setParameter('api_platform.openapi.overrideResponses', $config['openapi']['overrideResponses']);
×
945

UNCOV
946
        $tags = [];
×
UNCOV
947
        foreach ($config['openapi']['tags'] as $tag) {
×
948
            $tags[] = new Tag($tag['name'], $tag['description'] ?? null);
×
949
        }
950

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

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

UNCOV
956
        $loader->load('json_schema.xml');
×
957
    }
958

959
    private function registerMakerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
960
    {
UNCOV
961
        if (!$this->isConfigEnabled($container, $config['maker'])) {
×
962
            return;
×
963
        }
964

UNCOV
965
        $loader->load('maker.xml');
×
966
    }
967

968
    private function registerArgumentResolverConfiguration(XmlFileLoader $loader): void
969
    {
UNCOV
970
        $loader->load('argument_resolver.xml');
×
971
    }
972

973
    private function registerLinkSecurityConfiguration(XmlFileLoader $loader, array $config): void
974
    {
UNCOV
975
        if ($config['enable_link_security']) {
×
UNCOV
976
            $loader->load('link_security.xml');
×
977
        }
978
    }
979

980
    private function registerJsonStreamerConfiguration(ContainerBuilder $container, XmlFileLoader $loader, array $formats, array $config): void
981
    {
UNCOV
982
        if (!$config['enable_json_streamer']) {
×
UNCOV
983
            return;
×
984
        }
985

986
        if (!class_exists(JsonStreamWriter::class)) {
×
987
            throw new RuntimeException('symfony/json-streamer is not installed.');
×
988
        }
989

990
        // @TODO symfony/json-streamer:>=7.4.1 add composer conflict
991
        if (!class_exists(ControllerHelper::class)) {
×
992
            throw new RuntimeException('Symfony symfony/json-stream:^7.4 is needed.');
×
993
        }
994

995
        if (isset($formats['jsonld'])) {
×
996
            $container->setParameter('.json_streamer.stream_writers_dir.jsonld', '%kernel.cache_dir%/json_streamer/stream_writer/jsonld');
×
997
            $container->setParameter('.json_streamer.stream_readers_dir.jsonld', '%kernel.cache_dir%/json_streamer/stream_reader/jsonld');
×
998
            $container->setParameter('.json_streamer.lazy_ghosts_dir.jsonld', '%kernel.cache_dir%/json_streamer/lazy_ghost/jsonld');
×
999
        }
1000

1001
        $loader->load('json_streamer/common.xml');
×
1002

1003
        if ($config['use_symfony_listeners']) {
×
1004
            $loader->load('json_streamer/events.xml');
×
1005
        } else {
1006
            if (isset($formats['jsonld'])) {
×
1007
                $loader->load('json_streamer/hydra.xml');
×
1008
            }
1009

1010
            if (isset($formats['json'])) {
×
1011
                $loader->load('json_streamer/json.xml');
×
1012
            }
1013
        }
1014
    }
1015
}
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

© 2026 Coveralls, Inc