|
13 | 13 |
|
14 | 14 | namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection;
|
15 | 15 |
|
| 16 | +use ApiPlatform\Core\Annotation\ApiResource; |
16 | 17 | use ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Document\DocumentMetadata;
|
17 | 18 | use ApiPlatform\Core\Exception\FilterValidationException;
|
18 | 19 | use ApiPlatform\Core\Exception\InvalidArgumentException;
|
|
33 | 34 | use Symfony\Component\HttpFoundation\Response;
|
34 | 35 | use Symfony\Component\Messenger\MessageBusInterface;
|
35 | 36 | use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface;
|
| 37 | +use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
36 | 38 |
|
37 | 39 | /**
|
38 | 40 | * The configuration of the bundle.
|
@@ -128,13 +130,41 @@ public function getConfigTreeBuilder()
|
128 | 130 | ->canBeDisabled()
|
129 | 131 | ->addDefaultsIfNotSet()
|
130 | 132 | ->children()
|
131 |
| - ->booleanNode('enabled')->defaultTrue()->info('To enable or disable pagination for all resource collections by default.')->end() |
132 |
| - ->booleanNode('partial')->defaultFalse()->info('To enable or disable partial pagination for all resource collections by default when pagination is enabled.')->end() |
133 |
| - ->booleanNode('client_enabled')->defaultFalse()->info('To allow the client to enable or disable the pagination.')->end() |
134 |
| - ->booleanNode('client_items_per_page')->defaultFalse()->info('To allow the client to set the number of items per page.')->end() |
135 |
| - ->booleanNode('client_partial')->defaultFalse()->info('To allow the client to enable or disable partial pagination.')->end() |
136 |
| - ->integerNode('items_per_page')->defaultValue(30)->info('The default number of items per page.')->end() |
137 |
| - ->integerNode('maximum_items_per_page')->defaultNull()->info('The maximum number of items per page.')->end() |
| 133 | + ->booleanNode('enabled') |
| 134 | + ->setDeprecated('The use of the `collection.pagination.enabled` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.pagination_enabled` instead.') |
| 135 | + ->defaultTrue() |
| 136 | + ->info('To enable or disable pagination for all resource collections by default.') |
| 137 | + ->end() |
| 138 | + ->booleanNode('partial') |
| 139 | + ->setDeprecated('The use of the `collection.pagination.partial` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.pagination_partial` instead.') |
| 140 | + ->defaultFalse() |
| 141 | + ->info('To enable or disable partial pagination for all resource collections by default when pagination is enabled.') |
| 142 | + ->end() |
| 143 | + ->booleanNode('client_enabled') |
| 144 | + ->setDeprecated('The use of the `collection.pagination.client_enabled` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.pagination_client_enabled` instead.') |
| 145 | + ->defaultFalse() |
| 146 | + ->info('To allow the client to enable or disable the pagination.') |
| 147 | + ->end() |
| 148 | + ->booleanNode('client_items_per_page') |
| 149 | + ->setDeprecated('The use of the `collection.pagination.client_items_per_page` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.pagination_client_items_per_page` instead.') |
| 150 | + ->defaultFalse() |
| 151 | + ->info('To allow the client to set the number of items per page.') |
| 152 | + ->end() |
| 153 | + ->booleanNode('client_partial') |
| 154 | + ->setDeprecated('The use of the `collection.pagination.client_partial` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.pagination_client_partial` instead.') |
| 155 | + ->defaultFalse() |
| 156 | + ->info('To allow the client to enable or disable partial pagination.') |
| 157 | + ->end() |
| 158 | + ->integerNode('items_per_page') |
| 159 | + ->setDeprecated('The use of the `collection.pagination.items_per_page` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.pagination_items_per_page` instead.') |
| 160 | + ->defaultValue(30) |
| 161 | + ->info('The default number of items per page.') |
| 162 | + ->end() |
| 163 | + ->integerNode('maximum_items_per_page') |
| 164 | + ->setDeprecated('The use of the `collection.pagination.maximum_items_per_page` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.pagination_maximum_items_per_page` instead.') |
| 165 | + ->defaultNull() |
| 166 | + ->info('The maximum number of items per page.') |
| 167 | + ->end() |
138 | 168 | ->scalarNode('page_parameter_name')->defaultValue('page')->cannotBeEmpty()->info('The default name of the parameter handling the page number.')->end()
|
139 | 169 | ->scalarNode('enabled_parameter_name')->defaultValue('pagination')->cannotBeEmpty()->info('The name of the query parameter to enable or disable pagination.')->end()
|
140 | 170 | ->scalarNode('items_per_page_parameter_name')->defaultValue('itemsPerPage')->cannotBeEmpty()->info('The name of the query parameter to set the number of items per page.')->end()
|
@@ -179,6 +209,8 @@ public function getConfigTreeBuilder()
|
179 | 209 | 'jsonld' => ['mime_types' => ['application/ld+json']],
|
180 | 210 | ]);
|
181 | 211 |
|
| 212 | + $this->addDefaultsSection($rootNode); |
| 213 | + |
182 | 214 | return $treeBuilder;
|
183 | 215 | }
|
184 | 216 |
|
@@ -311,16 +343,30 @@ private function addHttpCacheSection(ArrayNodeDefinition $rootNode): void
|
311 | 343 | ->arrayNode('http_cache')
|
312 | 344 | ->addDefaultsIfNotSet()
|
313 | 345 | ->children()
|
314 |
| - ->booleanNode('etag')->defaultTrue()->info('Automatically generate etags for API responses.')->end() |
315 |
| - ->integerNode('max_age')->defaultNull()->info('Default value for the response max age.')->end() |
316 |
| - ->integerNode('shared_max_age')->defaultNull()->info('Default value for the response shared (proxy) max age.')->end() |
| 346 | + ->booleanNode('etag') |
| 347 | + ->setDeprecated('The use of the `http_cache.etag` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.cache_headers.etag` instead.') |
| 348 | + ->defaultTrue() |
| 349 | + ->info('Automatically generate etags for API responses.') |
| 350 | + ->end() |
| 351 | + ->integerNode('max_age') |
| 352 | + ->setDeprecated('The use of the `http_cache.max_age` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.cache_headers.max_age` instead.') |
| 353 | + ->defaultNull() |
| 354 | + ->info('Default value for the response max age.') |
| 355 | + ->end() |
| 356 | + ->integerNode('shared_max_age') |
| 357 | + ->setDeprecated('The use of the `http_cache.shared_max_age` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.cache_headers.shared_max_age` instead.') |
| 358 | + ->defaultNull() |
| 359 | + ->info('Default value for the response shared (proxy) max age.') |
| 360 | + ->end() |
317 | 361 | ->arrayNode('vary')
|
| 362 | + ->setDeprecated('The use of the `http_cache.vary` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.cache_headers.vary` instead.') |
318 | 363 | ->defaultValue(['Accept'])
|
319 | 364 | ->prototype('scalar')->end()
|
320 | 365 | ->info('Default values of the "Vary" HTTP header.')
|
321 | 366 | ->end()
|
322 | 367 | ->booleanNode('public')->defaultNull()->info('To make all responses public by default.')->end()
|
323 | 368 | ->arrayNode('invalidation')
|
| 369 | + ->setDeprecated('The use of the `http_cache.invalidation` has been deprecated in 2.6 and will be removed in 3.0. Use `defaults.cache_headers.invalidation` instead.') |
324 | 370 | ->info('Enable the tags-based cache invalidation system.')
|
325 | 371 | ->canBeEnabled()
|
326 | 372 | ->children()
|
@@ -494,4 +540,28 @@ private function addFormatSection(ArrayNodeDefinition $rootNode, string $key, ar
|
494 | 540 | ->end()
|
495 | 541 | ->end();
|
496 | 542 | }
|
| 543 | + |
| 544 | + private function addDefaultsSection(ArrayNodeDefinition $rootNode): void |
| 545 | + { |
| 546 | + $nameConverter = new CamelCaseToSnakeCaseNameConverter(); |
| 547 | + $defaultsNode = $rootNode->children()->arrayNode('defaults'); |
| 548 | + |
| 549 | + $defaultsNode |
| 550 | + ->ignoreExtraKeys() |
| 551 | + ->beforeNormalization() |
| 552 | + ->always(function (array $defaults) use ($nameConverter) { |
| 553 | + $normalizedDefaults = []; |
| 554 | + foreach ($defaults as $option => $value) { |
| 555 | + $option = $nameConverter->normalize($option); |
| 556 | + $normalizedDefaults[$option] = $value; |
| 557 | + } |
| 558 | + |
| 559 | + return $normalizedDefaults; |
| 560 | + }); |
| 561 | + |
| 562 | + foreach (ApiResource::CONFIGURABLE_DEFAULTS as $attribute) { |
| 563 | + $snakeCased = $nameConverter->normalize($attribute); |
| 564 | + $defaultsNode->children()->variableNode($snakeCased); |
| 565 | + } |
| 566 | + } |
497 | 567 | }
|
0 commit comments