|
13 | 13 |
|
14 | 14 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
15 | 15 | use Symfony\Component\DependencyInjection\ContainerInterface;
|
| 16 | +use Symfony\Component\DependencyInjection\Definition; |
16 | 17 | use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
17 | 18 | use Symfony\Component\DependencyInjection\Exception\LogicException;
|
18 | 19 | use Symfony\Component\DependencyInjection\Reference;
|
|
29 | 30 | *
|
30 | 31 | * @author Fabien Potencier <fabien@symfony.com>
|
31 | 32 | * @author Jeremy Mikola <jmikola@gmail.com>
|
| 33 | + * @author Kévin Dunglas <dunglas@gmail.com> |
32 | 34 | */
|
33 | 35 | class FrameworkExtension extends Extension
|
34 | 36 | {
|
@@ -122,11 +124,10 @@ public function load(array $configs, ContainerBuilder $container)
|
122 | 124 | }
|
123 | 125 |
|
124 | 126 | $this->registerAnnotationsConfiguration($config['annotations'], $container, $loader);
|
125 |
| - |
126 | 127 | $this->registerPropertyAccessConfiguration($config['property_access'], $container);
|
127 | 128 |
|
128 |
| - if (isset($config['serializer']) && $config['serializer']['enabled']) { |
129 |
| - $loader->load('serializer.xml'); |
| 129 | + if (isset($config['serializer'])) { |
| 130 | + $this->registerSerializerConfiguration($config['serializer'], $container, $loader); |
130 | 131 | }
|
131 | 132 |
|
132 | 133 | $loader->load('debug_prod.xml');
|
@@ -874,6 +875,86 @@ private function registerSecurityCsrfConfiguration(array $config, ContainerBuild
|
874 | 875 | $loader->load('security_csrf.xml');
|
875 | 876 | }
|
876 | 877 |
|
| 878 | + /** |
| 879 | + * Loads the serializer configuration. |
| 880 | + * |
| 881 | + * @param array $config A serializer configuration array |
| 882 | + * @param ContainerBuilder $container A ContainerBuilder instance |
| 883 | + * @param XmlFileLoader $loader An XmlFileLoader instance |
| 884 | + */ |
| 885 | + private function registerSerializerConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) |
| 886 | + { |
| 887 | + if (!$config['enabled']) { |
| 888 | + return; |
| 889 | + } |
| 890 | + |
| 891 | + $loader->load('serializer.xml'); |
| 892 | + $chainLoader = $container->getDefinition('serializer.mapping.chain_loader'); |
| 893 | + |
| 894 | + $serializerLoaders = array(); |
| 895 | + if (isset($config['enable_annotations']) && $config['enable_annotations']) { |
| 896 | + $annotationLoader = new Definition( |
| 897 | + 'Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader', |
| 898 | + array(new Reference('annotation_reader')) |
| 899 | + ); |
| 900 | + $annotationLoader->setPublic(false); |
| 901 | + |
| 902 | + $serializerLoaders[] = $annotationLoader; |
| 903 | + } |
| 904 | + |
| 905 | + $bundles = $container->getParameter('kernel.bundles'); |
| 906 | + foreach ($bundles as $bundle) { |
| 907 | + $reflection = new \ReflectionClass($bundle); |
| 908 | + $dirname = dirname($reflection->getFilename()); |
| 909 | + |
| 910 | + if (is_file($file = $dirname.'/Resources/config/serialization.xml')) { |
| 911 | + $definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader', array(realpath($file))); |
| 912 | + $definition->setPublic(false); |
| 913 | + |
| 914 | + $serializerLoaders[] = $definition; |
| 915 | + $container->addResource(new FileResource($file)); |
| 916 | + } |
| 917 | + |
| 918 | + if (is_file($file = $dirname.'/Resources/config/serialization.yml')) { |
| 919 | + $definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader', array(realpath($file))); |
| 920 | + $definition->setPublic(false); |
| 921 | + |
| 922 | + $serializerLoaders[] = $definition; |
| 923 | + $container->addResource(new FileResource($file)); |
| 924 | + } |
| 925 | + |
| 926 | + if (is_dir($dir = $dirname.'/Resources/config/serialization')) { |
| 927 | + foreach (Finder::create()->files()->in($dir)->name('*.xml') as $file) { |
| 928 | + $definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader', array($file->getRealpath())); |
| 929 | + $definition->setPublic(false); |
| 930 | + |
| 931 | + $serializerLoaders[] = $definition; |
| 932 | + } |
| 933 | + foreach (Finder::create()->files()->in($dir)->name('*.yml') as $file) { |
| 934 | + $definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader', array($file->getRealpath())); |
| 935 | + $definition->setPublic(false); |
| 936 | + |
| 937 | + $serializerLoaders[] = $definition; |
| 938 | + } |
| 939 | + |
| 940 | + $container->addResource(new DirectoryResource($dir)); |
| 941 | + } |
| 942 | + } |
| 943 | + |
| 944 | + $chainLoader->replaceArgument(0, $serializerLoaders); |
| 945 | + |
| 946 | + if (isset($config['cache']) && $config['cache']) { |
| 947 | + $container->setParameter( |
| 948 | + 'serializer.mapping.cache.prefix', |
| 949 | + 'serializer_'.hash('sha256', $container->getParameter('kernel.root_dir')) |
| 950 | + ); |
| 951 | + |
| 952 | + $container->getDefinition('serializer.mapping.class_metadata_factory')->replaceArgument( |
| 953 | + 1, new Reference($config['cache']) |
| 954 | + ); |
| 955 | + } |
| 956 | + } |
| 957 | + |
877 | 958 | /**
|
878 | 959 | * Returns the base path for the XSD files.
|
879 | 960 | *
|
|
0 commit comments