1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\FilterInterface; |
17
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\EagerLoadingExtension; |
18
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\FilterEagerLoadingExtension; |
19
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; |
20
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface; |
21
|
|
|
use ApiPlatform\Core\DataPersister\DataPersisterInterface; |
22
|
|
|
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; |
23
|
|
|
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; |
24
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
25
|
|
|
use Doctrine\Common\Annotations\Annotation; |
26
|
|
|
use Doctrine\ORM\Version; |
27
|
|
|
use phpDocumentor\Reflection\DocBlockFactoryInterface; |
28
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
29
|
|
|
use Symfony\Component\Config\FileLocator; |
30
|
|
|
use Symfony\Component\Config\Resource\DirectoryResource; |
31
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
32
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
33
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
34
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
35
|
|
|
use Symfony\Component\Finder\Finder; |
36
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
37
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
38
|
|
|
use Symfony\Component\Yaml\Yaml; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The extension of this bundle. |
42
|
|
|
* |
43
|
|
|
* @author Kévin Dunglas <[email protected]> |
44
|
|
|
*/ |
45
|
|
|
final class ApiPlatformExtension extends Extension implements PrependExtensionInterface |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function prepend(ContainerBuilder $container) |
51
|
|
|
{ |
52
|
|
|
if (!$frameworkConfiguration = $container->getExtensionConfig('framework')) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($frameworkConfiguration as $frameworkParameters) { |
57
|
|
|
if (isset($frameworkParameters['serializer'])) { |
58
|
|
|
$serializerConfig = $serializerConfig ?? $frameworkParameters['serializer']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (isset($frameworkParameters['property_info'])) { |
62
|
|
|
$propertyInfoConfig = $propertyInfoConfig ?? $frameworkParameters['property_info']; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!isset($serializerConfig['enabled'])) { |
67
|
|
|
$container->prependExtensionConfig('framework', ['serializer' => ['enabled' => true]]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!isset($propertyInfoConfig['enabled'])) { |
71
|
|
|
$container->prependExtensionConfig('framework', ['property_info' => ['enabled' => true]]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (isset($serializerConfig['name_converter'])) { |
75
|
|
|
$container->prependExtensionConfig('api_platform', ['name_converter' => $serializerConfig['name_converter']]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function load(array $configs, ContainerBuilder $container) |
83
|
|
|
{ |
84
|
|
|
$configuration = new Configuration(); |
85
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
86
|
|
|
$formats = $this->getFormats($config['formats']); |
87
|
|
|
$errorFormats = $this->getFormats($config['error_formats']); |
88
|
|
|
$this->handleConfig($container, $config, $formats, $errorFormats); |
89
|
|
|
|
90
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
91
|
|
|
$loader->load('api.xml'); |
92
|
|
|
$loader->load('data_persister.xml'); |
93
|
|
|
$loader->load('data_provider.xml'); |
94
|
|
|
$loader->load('filter.xml'); |
95
|
|
|
|
96
|
|
|
$container->registerForAutoconfiguration(DataPersisterInterface::class) |
97
|
|
|
->addTag('api_platform.data_persister'); |
98
|
|
|
$container->registerForAutoconfiguration(ItemDataProviderInterface::class) |
99
|
|
|
->addTag('api_platform.item_data_provider'); |
100
|
|
|
$container->registerForAutoconfiguration(CollectionDataProviderInterface::class) |
101
|
|
|
->addTag('api_platform.collection_data_provider'); |
102
|
|
|
$container->registerForAutoconfiguration(QueryItemExtensionInterface::class) |
103
|
|
|
->addTag('api_platform.doctrine.orm.query_extension.item'); |
104
|
|
|
$container->registerForAutoconfiguration(QueryCollectionExtensionInterface::class) |
105
|
|
|
->addTag('api_platform.doctrine.orm.query_extension.collection'); |
106
|
|
|
$container->registerForAutoconfiguration(FilterInterface::class) |
107
|
|
|
->addTag('api_platform.filter'); |
108
|
|
|
|
109
|
|
|
if (interface_exists(ValidatorInterface::class)) { |
110
|
|
|
$loader->load('validator.xml'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$bundles = $container->getParameter('kernel.bundles'); |
114
|
|
|
if (isset($bundles['SecurityBundle'])) { |
115
|
|
|
$loader->load('security.xml'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$useDoctrine = isset($bundles['DoctrineBundle']) && class_exists(Version::class); |
119
|
|
|
|
120
|
|
|
$this->registerMetadataConfiguration($container, $config, $loader); |
121
|
|
|
$this->registerOAuthConfiguration($container, $config, $loader); |
122
|
|
|
$this->registerApiKeysConfiguration($container, $config, $loader); |
123
|
|
|
$this->registerSwaggerConfiguration($container, $config, $loader); |
124
|
|
|
$this->registerJsonApiConfiguration($formats, $loader); |
125
|
|
|
$this->registerJsonLdConfiguration($formats, $loader); |
126
|
|
|
$this->registerJsonHalConfiguration($formats, $loader); |
127
|
|
|
$this->registerJsonProblemConfiguration($errorFormats, $loader); |
128
|
|
|
$this->registerGraphqlConfiguration($container, $config, $loader); |
129
|
|
|
$this->registerBundlesConfiguration($bundles, $config, $loader, $useDoctrine); |
130
|
|
|
$this->registerCacheConfiguration($container); |
131
|
|
|
$this->registerDoctrineExtensionConfiguration($container, $config, $useDoctrine); |
132
|
|
|
$this->registerHttpCache($container, $config, $loader, $useDoctrine); |
133
|
|
|
$this->registerValidatorConfiguration($container, $config, $loader); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Handles configuration. |
138
|
|
|
* |
139
|
|
|
* @param ContainerBuilder $container |
140
|
|
|
* @param array $config |
141
|
|
|
* @param array $formats |
142
|
|
|
* @param array $errorFormats |
143
|
|
|
*/ |
144
|
|
|
private function handleConfig(ContainerBuilder $container, array $config, array $formats, array $errorFormats) |
145
|
|
|
{ |
146
|
|
|
$container->setParameter('api_platform.enable_entrypoint', $config['enable_entrypoint']); |
147
|
|
|
$container->setParameter('api_platform.enable_docs', $config['enable_docs']); |
148
|
|
|
$container->setParameter('api_platform.title', $config['title']); |
149
|
|
|
$container->setParameter('api_platform.description', $config['description']); |
150
|
|
|
$container->setParameter('api_platform.version', $config['version']); |
151
|
|
|
$container->setParameter('api_platform.exception_to_status', $config['exception_to_status']); |
152
|
|
|
$container->setParameter('api_platform.formats', $formats); |
153
|
|
|
$container->setParameter('api_platform.error_formats', $errorFormats); |
154
|
|
|
$container->setParameter('api_platform.allow_plain_identifiers', $config['allow_plain_identifiers']); |
155
|
|
|
$container->setParameter('api_platform.eager_loading.enabled', $config['eager_loading']['enabled']); |
156
|
|
|
$container->setParameter('api_platform.eager_loading.max_joins', $config['eager_loading']['max_joins']); |
157
|
|
|
$container->setParameter('api_platform.eager_loading.fetch_partial', $config['eager_loading']['fetch_partial']); |
158
|
|
|
$container->setParameter('api_platform.eager_loading.force_eager', $config['eager_loading']['force_eager']); |
159
|
|
|
$container->setParameter('api_platform.collection.order', $config['collection']['order']); |
160
|
|
|
$container->setParameter('api_platform.collection.order_parameter_name', $config['collection']['order_parameter_name']); |
161
|
|
|
$container->setParameter('api_platform.collection.pagination.enabled', $config['collection']['pagination']['enabled']); |
162
|
|
|
$container->setParameter('api_platform.collection.pagination.partial', $config['collection']['pagination']['partial']); |
163
|
|
|
$container->setParameter('api_platform.collection.pagination.client_enabled', $config['collection']['pagination']['client_enabled']); |
164
|
|
|
$container->setParameter('api_platform.collection.pagination.client_items_per_page', $config['collection']['pagination']['client_items_per_page']); |
165
|
|
|
$container->setParameter('api_platform.collection.pagination.client_partial', $config['collection']['pagination']['client_partial']); |
166
|
|
|
$container->setParameter('api_platform.collection.pagination.items_per_page', $config['collection']['pagination']['items_per_page']); |
167
|
|
|
$container->setParameter('api_platform.collection.pagination.maximum_items_per_page', $config['collection']['pagination']['maximum_items_per_page']); |
168
|
|
|
$container->setParameter('api_platform.collection.pagination.page_parameter_name', $config['collection']['pagination']['page_parameter_name']); |
169
|
|
|
$container->setParameter('api_platform.collection.pagination.enabled_parameter_name', $config['collection']['pagination']['enabled_parameter_name']); |
170
|
|
|
$container->setParameter('api_platform.collection.pagination.items_per_page_parameter_name', $config['collection']['pagination']['items_per_page_parameter_name']); |
171
|
|
|
$container->setParameter('api_platform.collection.pagination.partial_parameter_name', $config['collection']['pagination']['partial_parameter_name']); |
172
|
|
|
$container->setParameter('api_platform.http_cache.etag', $config['http_cache']['etag']); |
173
|
|
|
$container->setParameter('api_platform.http_cache.max_age', $config['http_cache']['max_age']); |
174
|
|
|
$container->setParameter('api_platform.http_cache.shared_max_age', $config['http_cache']['shared_max_age']); |
175
|
|
|
$container->setParameter('api_platform.http_cache.vary', $config['http_cache']['vary']); |
176
|
|
|
$container->setParameter('api_platform.http_cache.public', $config['http_cache']['public']); |
177
|
|
|
|
178
|
|
|
$container->setAlias('api_platform.operation_path_resolver.default', $config['default_operation_path_resolver']); |
179
|
|
|
$container->setAlias('api_platform.path_segment_name_generator', $config['path_segment_name_generator']); |
180
|
|
|
|
181
|
|
|
if ($config['name_converter']) { |
182
|
|
|
$container->setAlias('api_platform.name_converter', $config['name_converter']); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Registers metadata configuration. |
188
|
|
|
* |
189
|
|
|
* @param ContainerBuilder $container |
190
|
|
|
* @param array $config |
191
|
|
|
* @param XmlFileLoader $loader |
192
|
|
|
*/ |
193
|
|
|
private function registerMetadataConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader) |
194
|
|
|
{ |
195
|
|
|
$loader->load('metadata/metadata.xml'); |
196
|
|
|
$loader->load('metadata/xml.xml'); |
197
|
|
|
|
198
|
|
|
list($xmlResources, $yamlResources) = $this->getResourcesToWatch($container, $config['mapping']['paths']); |
199
|
|
|
|
200
|
|
|
if (isset($config['resource_class_directories']) && $config['resource_class_directories']) { |
201
|
|
|
$container->setParameter('api_platform.resource_class_directories', array_merge( |
202
|
|
|
$config['resource_class_directories'], $container->getParameter('api_platform.resource_class_directories') |
203
|
|
|
)); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$container->getDefinition('api_platform.metadata.extractor.xml')->addArgument($xmlResources); |
207
|
|
|
|
208
|
|
|
if (class_exists(Annotation::class)) { |
209
|
|
|
$loader->load('metadata/annotation.xml'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (class_exists(Yaml::class)) { |
213
|
|
|
$loader->load('metadata/yaml.xml'); |
214
|
|
|
$container->getDefinition('api_platform.metadata.extractor.yaml')->addArgument($yamlResources); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if (interface_exists(DocBlockFactoryInterface::class)) { |
218
|
|
|
$loader->load('metadata/php_doc.xml'); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
private function getBundlesResourcesPaths(ContainerBuilder $container): array |
223
|
|
|
{ |
224
|
|
|
$bundlesResourcesPaths = []; |
225
|
|
|
|
226
|
|
|
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) { |
227
|
|
|
$paths = []; |
228
|
|
|
$dirname = $bundle['path']; |
229
|
|
|
foreach (['.yaml', '.yml', '.xml', ''] as $extension) { |
230
|
|
|
$paths[] = "$dirname/Resources/config/api_resources$extension"; |
231
|
|
|
} |
232
|
|
|
$paths[] = "$dirname/Entity"; |
233
|
|
|
|
234
|
|
|
foreach ($paths as $path) { |
235
|
|
|
if ($container->fileExists($path, false)) { |
236
|
|
|
$bundlesResourcesPaths[] = $path; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $bundlesResourcesPaths; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
private function getResourcesToWatch(ContainerBuilder $container, array $resourcesPaths): array |
245
|
|
|
{ |
246
|
|
|
// Flex structure |
247
|
|
|
$projectDir = $container->getParameter('kernel.project_dir'); |
248
|
|
|
if (is_dir($dir = "$projectDir/config/api_platform")) { |
249
|
|
|
$resourcesPaths[] = $dir; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$paths = array_unique(array_merge($resourcesPaths, $this->getBundlesResourcesPaths($container))); |
253
|
|
|
$resources = ['yml' => [], 'xml' => [], 'dir' => []]; |
254
|
|
|
|
255
|
|
|
foreach ($paths as $path) { |
256
|
|
|
if (is_dir($path)) { |
257
|
|
|
foreach (Finder::create()->followLinks()->files()->in($path)->name('/\.(xml|ya?ml)$/') as $file) { |
258
|
|
|
$resources['yaml' === ($extension = $file->getExtension()) ? 'yml' : $extension][] = $file->getRealPath(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$resources['dir'][] = $path; |
262
|
|
|
$container->addResource(new DirectoryResource($path, '/\.(xml|ya?ml|php)$/')); |
263
|
|
|
|
264
|
|
|
continue; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
if ($container->fileExists($path, false)) { |
268
|
|
|
if (!preg_match('/\.(xml|ya?ml)$/', $path, $matches)) { |
269
|
|
|
throw new RuntimeException(sprintf('Unsupported mapping type in "%s", supported types are XML & Yaml.', $path)); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$resources['yaml' === $matches[1] ? 'yml' : $matches[1]][] = $path; |
273
|
|
|
|
274
|
|
|
continue; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
throw new RuntimeException(sprintf('Could not open file or directory "%s".', $path)); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$container->setParameter('api_platform.resource_class_directories', $resources['dir']); |
281
|
|
|
|
282
|
|
|
return [$resources['xml'], $resources['yml']]; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Registers the OAuth configuration. |
287
|
|
|
* |
288
|
|
|
* @param ContainerBuilder $container |
289
|
|
|
* @param array $config |
290
|
|
|
* @param XmlFileLoader $loader |
291
|
|
|
*/ |
292
|
|
|
private function registerOAuthConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader) |
|
|
|
|
293
|
|
|
{ |
294
|
|
|
if (!$config['oauth']) { |
295
|
|
|
return; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$container->setParameter('api_platform.oauth.enabled', $config['oauth']['enabled']); |
299
|
|
|
$container->setParameter('api_platform.oauth.clientId', $config['oauth']['clientId']); |
300
|
|
|
$container->setParameter('api_platform.oauth.clientSecret', $config['oauth']['clientSecret']); |
301
|
|
|
$container->setParameter('api_platform.oauth.type', $config['oauth']['type']); |
302
|
|
|
$container->setParameter('api_platform.oauth.flow', $config['oauth']['flow']); |
303
|
|
|
$container->setParameter('api_platform.oauth.tokenUrl', $config['oauth']['tokenUrl']); |
304
|
|
|
$container->setParameter('api_platform.oauth.authorizationUrl', $config['oauth']['authorizationUrl']); |
305
|
|
|
$container->setParameter('api_platform.oauth.scopes', $config['oauth']['scopes']); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Registers the api keys configuration. |
310
|
|
|
* |
311
|
|
|
* @param ContainerBuilder $container |
312
|
|
|
* @param array $config |
313
|
|
|
* @param XmlFileLoader $loader |
314
|
|
|
*/ |
315
|
|
|
private function registerApiKeysConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader) |
|
|
|
|
316
|
|
|
{ |
317
|
|
|
$container->setParameter('api_platform.swagger.api_keys', $config['swagger']['api_keys']); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Registers the Swagger and Swagger UI configuration. |
322
|
|
|
* |
323
|
|
|
* @param ContainerBuilder $container |
324
|
|
|
* @param array $config |
325
|
|
|
* @param XmlFileLoader $loader |
326
|
|
|
*/ |
327
|
|
|
private function registerSwaggerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader) |
328
|
|
|
{ |
329
|
|
|
if (!$config['enable_swagger']) { |
330
|
|
|
return; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$loader->load('swagger.xml'); |
334
|
|
|
|
335
|
|
|
if ($config['enable_swagger_ui']) { |
336
|
|
|
$loader->load('swagger-ui.xml'); |
337
|
|
|
$container->setParameter('api_platform.enable_swagger_ui', $config['enable_swagger_ui']); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
$container->setParameter('api_platform.enable_swagger', $config['enable_swagger']); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Registers the JsonApi configuration. |
345
|
|
|
* |
346
|
|
|
* @param array $formats |
347
|
|
|
* @param XmlFileLoader $loader |
348
|
|
|
*/ |
349
|
|
|
private function registerJsonApiConfiguration(array $formats, XmlFileLoader $loader) |
350
|
|
|
{ |
351
|
|
|
if (!isset($formats['jsonapi'])) { |
352
|
|
|
return; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
$loader->load('jsonapi.xml'); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Registers the JSON-LD and Hydra configuration. |
360
|
|
|
* |
361
|
|
|
* @param array $formats |
362
|
|
|
* @param XmlFileLoader $loader |
363
|
|
|
*/ |
364
|
|
|
private function registerJsonLdConfiguration(array $formats, XmlFileLoader $loader) |
365
|
|
|
{ |
366
|
|
|
if (!isset($formats['jsonld'])) { |
367
|
|
|
return; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$loader->load('jsonld.xml'); |
371
|
|
|
$loader->load('hydra.xml'); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Registers the HAL configuration. |
376
|
|
|
* |
377
|
|
|
* @param array $formats |
378
|
|
|
* @param XmlFileLoader $loader |
379
|
|
|
*/ |
380
|
|
|
private function registerJsonHalConfiguration(array $formats, XmlFileLoader $loader) |
381
|
|
|
{ |
382
|
|
|
if (!isset($formats['jsonhal'])) { |
383
|
|
|
return; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
$loader->load('hal.xml'); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Registers the JSON Problem configuration. |
391
|
|
|
* |
392
|
|
|
* @param array $errorFormats |
393
|
|
|
* @param XmlFileLoader $loader |
394
|
|
|
*/ |
395
|
|
|
private function registerJsonProblemConfiguration(array $errorFormats, XmlFileLoader $loader) |
396
|
|
|
{ |
397
|
|
|
if (!isset($errorFormats['jsonproblem'])) { |
398
|
|
|
return; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
$loader->load('problem.xml'); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Registers the GraphQL configuration. |
406
|
|
|
* |
407
|
|
|
* @param ContainerBuilder $container |
408
|
|
|
* @param array $config |
409
|
|
|
* @param XmlFileLoader $loader |
410
|
|
|
*/ |
411
|
|
|
private function registerGraphqlConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader) |
412
|
|
|
{ |
413
|
|
|
if (!$config['graphql']) { |
414
|
|
|
return; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
$container->setParameter('api_platform.graphql.enabled', $config['graphql']['enabled']); |
418
|
|
|
$container->setParameter('api_platform.graphql.graphiql.enabled', $config['graphql']['graphiql']['enabled']); |
419
|
|
|
|
420
|
|
|
$loader->load('graphql.xml'); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Registers configuration for integration with third-party bundles. |
425
|
|
|
* |
426
|
|
|
* @param string[] $bundles |
427
|
|
|
* @param array $config |
428
|
|
|
* @param XmlFileLoader $loader |
429
|
|
|
* @param bool $useDoctrine |
430
|
|
|
*/ |
431
|
|
|
private function registerBundlesConfiguration(array $bundles, array $config, XmlFileLoader $loader, bool $useDoctrine) |
432
|
|
|
{ |
433
|
|
|
// Doctrine ORM support |
434
|
|
|
if ($useDoctrine) { |
435
|
|
|
$loader->load('doctrine_orm.xml'); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
// FOSUser support |
439
|
|
|
if (isset($bundles['FOSUserBundle']) && $config['enable_fos_user']) { |
440
|
|
|
$loader->load('fos_user.xml'); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
// NelmioApiDoc support |
444
|
|
|
if (isset($bundles['NelmioApiDocBundle']) && $config['enable_nelmio_api_doc']) { |
445
|
|
|
$loader->load('nelmio_api_doc.xml'); |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Registers the cache configuration. |
451
|
|
|
* |
452
|
|
|
* @param ContainerBuilder $container |
453
|
|
|
*/ |
454
|
|
|
private function registerCacheConfiguration(ContainerBuilder $container) |
455
|
|
|
{ |
456
|
|
|
// Don't use system cache pool in dev |
457
|
|
|
if (!$container->getParameter('kernel.debug')) { |
458
|
|
|
return; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
$container->register('api_platform.cache.metadata.property', ArrayAdapter::class); |
462
|
|
|
$container->register('api_platform.cache.metadata.resource', ArrayAdapter::class); |
463
|
|
|
$container->register('api_platform.cache.route_name_resolver', ArrayAdapter::class); |
464
|
|
|
$container->register('api_platform.cache.identifiers_extractor', ArrayAdapter::class); |
465
|
|
|
$container->register('api_platform.cache.subresource_operation_factory', ArrayAdapter::class); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Manipulate doctrine extension services according to the configuration. |
470
|
|
|
* |
471
|
|
|
* @param ContainerBuilder $container |
472
|
|
|
* @param array $config |
473
|
|
|
* @param bool $useDoctrine |
474
|
|
|
*/ |
475
|
|
|
private function registerDoctrineExtensionConfiguration(ContainerBuilder $container, array $config, bool $useDoctrine) |
476
|
|
|
{ |
477
|
|
|
if (!$useDoctrine || $config['eager_loading']['enabled']) { |
478
|
|
|
return; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
$container->removeAlias(EagerLoadingExtension::class); |
482
|
|
|
$container->removeDefinition('api_platform.doctrine.orm.query_extension.eager_loading'); |
483
|
|
|
$container->removeAlias(FilterEagerLoadingExtension::class); |
484
|
|
|
$container->removeDefinition('api_platform.doctrine.orm.query_extension.filter_eager_loading'); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
private function registerHttpCache(ContainerBuilder $container, array $config, XmlFileLoader $loader, bool $useDoctrine) |
488
|
|
|
{ |
489
|
|
|
$loader->load('http_cache.xml'); |
490
|
|
|
|
491
|
|
|
if (!$config['http_cache']['invalidation']['enabled']) { |
492
|
|
|
return; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
if ($useDoctrine) { |
496
|
|
|
$loader->load('doctrine_orm_http_cache_purger.xml'); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
$loader->load('http_cache_tags.xml'); |
500
|
|
|
|
501
|
|
|
$definitions = []; |
502
|
|
|
foreach ($config['http_cache']['invalidation']['varnish_urls'] as $key => $url) { |
503
|
|
|
$definition = new ChildDefinition('api_platform.http_cache.purger.varnish_client'); |
504
|
|
|
$definition->addArgument(['base_uri' => $url]); |
505
|
|
|
|
506
|
|
|
$definitions[] = $definition; |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
$container->getDefinition('api_platform.http_cache.purger.varnish')->addArgument($definitions); |
510
|
|
|
$container->setAlias('api_platform.http_cache.purger', 'api_platform.http_cache.purger.varnish'); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* Normalizes the format from config to the one accepted by Symfony HttpFoundation. |
515
|
|
|
* |
516
|
|
|
* @param array $configFormats |
517
|
|
|
* |
518
|
|
|
* @return array |
519
|
|
|
*/ |
520
|
|
|
private function getFormats(array $configFormats): array |
521
|
|
|
{ |
522
|
|
|
$formats = []; |
523
|
|
|
foreach ($configFormats as $format => $value) { |
524
|
|
|
foreach ($value['mime_types'] as $mimeType) { |
525
|
|
|
$formats[$format][] = $mimeType; |
526
|
|
|
} |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
return $formats; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Registers the Validator configuration. |
534
|
|
|
* |
535
|
|
|
* @param ContainerBuilder $container |
536
|
|
|
* @param array $config |
537
|
|
|
* @param XmlFileLoader $loader |
538
|
|
|
*/ |
539
|
|
|
private function registerValidatorConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader) |
|
|
|
|
540
|
|
|
{ |
541
|
|
|
if (!$config['validator']) { |
542
|
|
|
return; |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
$container->setParameter('api_platform.validator.serialize_payload_fields', $config['validator']['serialize_payload_fields']); |
546
|
|
|
} |
547
|
|
|
} |
548
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.