serializer.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
  11. use Psr\Cache\CacheItemPoolInterface;
  12. use Symfony\Bundle\FrameworkBundle\CacheWarmer\SerializerCacheWarmer;
  13. use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
  14. use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
  15. use Symfony\Component\ErrorHandler\ErrorRenderer\SerializerErrorRenderer;
  16. use Symfony\Component\PropertyInfo\Extractor\SerializerExtractor;
  17. use Symfony\Component\Serializer\Encoder\CsvEncoder;
  18. use Symfony\Component\Serializer\Encoder\DecoderInterface;
  19. use Symfony\Component\Serializer\Encoder\EncoderInterface;
  20. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  21. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  22. use Symfony\Component\Serializer\Encoder\YamlEncoder;
  23. use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
  24. use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
  25. use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
  26. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
  27. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
  28. use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
  29. use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
  30. use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
  31. use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
  32. use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;
  33. use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
  34. use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
  35. use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
  36. use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
  37. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  38. use Symfony\Component\Serializer\Normalizer\FormErrorNormalizer;
  39. use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
  40. use Symfony\Component\Serializer\Normalizer\MimeMessageNormalizer;
  41. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  42. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  43. use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;
  44. use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
  45. use Symfony\Component\Serializer\Normalizer\UidNormalizer;
  46. use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
  47. use Symfony\Component\Serializer\Serializer;
  48. use Symfony\Component\Serializer\SerializerInterface;
  49. return static function (ContainerConfigurator $container) {
  50. $container->parameters()
  51. ->set('serializer.mapping.cache.file', '%kernel.cache_dir%/serialization.php')
  52. ;
  53. $container->services()
  54. ->set('serializer', Serializer::class)
  55. ->public()
  56. ->args([[], []])
  57. ->tag('container.private', ['package' => 'symfony/framework-bundle', 'version' => '5.2'])
  58. ->alias(SerializerInterface::class, 'serializer')
  59. ->alias(NormalizerInterface::class, 'serializer')
  60. ->alias(DenormalizerInterface::class, 'serializer')
  61. ->alias(EncoderInterface::class, 'serializer')
  62. ->alias(DecoderInterface::class, 'serializer')
  63. ->alias('serializer.property_accessor', 'property_accessor')
  64. // Discriminator Map
  65. ->set('serializer.mapping.class_discriminator_resolver', ClassDiscriminatorFromClassMetadata::class)
  66. ->args([service('serializer.mapping.class_metadata_factory')])
  67. ->alias(ClassDiscriminatorResolverInterface::class, 'serializer.mapping.class_discriminator_resolver')
  68. // Normalizer
  69. ->set('serializer.normalizer.constraint_violation_list', ConstraintViolationListNormalizer::class)
  70. ->args([[], service('serializer.name_converter.metadata_aware')])
  71. ->tag('serializer.normalizer', ['priority' => -915])
  72. ->set('serializer.normalizer.mime_message', MimeMessageNormalizer::class)
  73. ->args([service('serializer.normalizer.property')])
  74. ->tag('serializer.normalizer', ['priority' => -915])
  75. ->set('serializer.normalizer.datetimezone', DateTimeZoneNormalizer::class)
  76. ->tag('serializer.normalizer', ['priority' => -915])
  77. ->set('serializer.normalizer.dateinterval', DateIntervalNormalizer::class)
  78. ->tag('serializer.normalizer', ['priority' => -915])
  79. ->set('serializer.normalizer.data_uri', DataUriNormalizer::class)
  80. ->args([service('mime_types')->nullOnInvalid()])
  81. ->tag('serializer.normalizer', ['priority' => -920])
  82. ->set('serializer.normalizer.datetime', DateTimeNormalizer::class)
  83. ->tag('serializer.normalizer', ['priority' => -910])
  84. ->set('serializer.normalizer.json_serializable', JsonSerializableNormalizer::class)
  85. ->tag('serializer.normalizer', ['priority' => -900])
  86. ->set('serializer.normalizer.problem', ProblemNormalizer::class)
  87. ->args([param('kernel.debug')])
  88. ->tag('serializer.normalizer', ['priority' => -890])
  89. ->set('serializer.denormalizer.unwrapping', UnwrappingDenormalizer::class)
  90. ->args([service('serializer.property_accessor')])
  91. ->tag('serializer.normalizer', ['priority' => 1000])
  92. ->set('serializer.normalizer.uid', UidNormalizer::class)
  93. ->tag('serializer.normalizer', ['priority' => -890])
  94. ->set('serializer.normalizer.form_error', FormErrorNormalizer::class)
  95. ->tag('serializer.normalizer', ['priority' => -915])
  96. ->set('serializer.normalizer.object', ObjectNormalizer::class)
  97. ->args([
  98. service('serializer.mapping.class_metadata_factory'),
  99. service('serializer.name_converter.metadata_aware'),
  100. service('serializer.property_accessor'),
  101. service('property_info')->ignoreOnInvalid(),
  102. service('serializer.mapping.class_discriminator_resolver')->ignoreOnInvalid(),
  103. null,
  104. [],
  105. ])
  106. ->tag('serializer.normalizer', ['priority' => -1000])
  107. ->alias(ObjectNormalizer::class, 'serializer.normalizer.object')
  108. ->set('serializer.normalizer.property', PropertyNormalizer::class)
  109. ->args([
  110. service('serializer.mapping.class_metadata_factory'),
  111. service('serializer.name_converter.metadata_aware'),
  112. service('property_info')->ignoreOnInvalid(),
  113. service('serializer.mapping.class_discriminator_resolver')->ignoreOnInvalid(),
  114. null,
  115. [],
  116. ])
  117. ->alias(PropertyNormalizer::class, 'serializer.normalizer.property')
  118. ->set('serializer.denormalizer.array', ArrayDenormalizer::class)
  119. ->tag('serializer.normalizer', ['priority' => -990])
  120. // Loader
  121. ->set('serializer.mapping.chain_loader', LoaderChain::class)
  122. ->args([[]])
  123. // Class Metadata Factory
  124. ->set('serializer.mapping.class_metadata_factory', ClassMetadataFactory::class)
  125. ->args([service('serializer.mapping.chain_loader')])
  126. ->alias(ClassMetadataFactoryInterface::class, 'serializer.mapping.class_metadata_factory')
  127. // Cache
  128. ->set('serializer.mapping.cache_warmer', SerializerCacheWarmer::class)
  129. ->args([abstract_arg('The serializer metadata loaders'), param('serializer.mapping.cache.file')])
  130. ->tag('kernel.cache_warmer')
  131. ->set('serializer.mapping.cache.symfony', CacheItemPoolInterface::class)
  132. ->factory([PhpArrayAdapter::class, 'create'])
  133. ->args([param('serializer.mapping.cache.file'), service('cache.serializer')])
  134. ->set('serializer.mapping.cache_class_metadata_factory', CacheClassMetadataFactory::class)
  135. ->decorate('serializer.mapping.class_metadata_factory')
  136. ->args([
  137. service('serializer.mapping.cache_class_metadata_factory.inner'),
  138. service('serializer.mapping.cache.symfony'),
  139. ])
  140. // Encoders
  141. ->set('serializer.encoder.xml', XmlEncoder::class)
  142. ->tag('serializer.encoder')
  143. ->set('serializer.encoder.json', JsonEncoder::class)
  144. ->tag('serializer.encoder')
  145. ->set('serializer.encoder.yaml', YamlEncoder::class)
  146. ->tag('serializer.encoder')
  147. ->set('serializer.encoder.csv', CsvEncoder::class)
  148. ->tag('serializer.encoder')
  149. // Name converter
  150. ->set('serializer.name_converter.camel_case_to_snake_case', CamelCaseToSnakeCaseNameConverter::class)
  151. ->set('serializer.name_converter.metadata_aware', MetadataAwareNameConverter::class)
  152. ->args([service('serializer.mapping.class_metadata_factory')])
  153. // PropertyInfo extractor
  154. ->set('property_info.serializer_extractor', SerializerExtractor::class)
  155. ->args([service('serializer.mapping.class_metadata_factory')])
  156. ->tag('property_info.list_extractor', ['priority' => -999])
  157. // ErrorRenderer integration
  158. ->alias('error_renderer', 'error_renderer.serializer')
  159. ->alias('error_renderer.serializer', 'error_handler.error_renderer.serializer')
  160. ->set('error_handler.error_renderer.serializer', SerializerErrorRenderer::class)
  161. ->args([
  162. service('serializer'),
  163. inline_service()
  164. ->factory([SerializerErrorRenderer::class, 'getPreferredFormat'])
  165. ->args([service('request_stack')]),
  166. service('error_renderer.html'),
  167. inline_service()
  168. ->factory([HtmlErrorRenderer::class, 'isDebug'])
  169. ->args([service('request_stack'), param('kernel.debug')]),
  170. ])
  171. ;
  172. };