Skip to content

Commit 74ec895

Browse files
committed
feature #13107 [FrameworkBundle] Serializer groups support (dunglas)
This PR was squashed before being merged into the 2.7 branch (closes #13107). Discussion ---------- [FrameworkBundle] Serializer groups support | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT | Doc PR | not yet This PR enables serializer groups in the full stack framework (including XML, YAML, annotations and caching). Commits ------- 83b56f6 [FrameworkBundle] Serializer groups support
2 parents 59ca5b3 + 83b56f6 commit 74ec895

File tree

9 files changed

+132
-4
lines changed

9 files changed

+132
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

+4
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,10 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode)
660660
->arrayNode('serializer')
661661
->info('serializer configuration')
662662
->canBeEnabled()
663+
->children()
664+
->booleanNode('enable_annotations')->defaultFalse()->end()
665+
->scalarNode('cache')->end()
666+
->end()
663667
->end()
664668
->end()
665669
;

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+84-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\ContainerInterface;
16+
use Symfony\Component\DependencyInjection\Definition;
1617
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1718
use Symfony\Component\DependencyInjection\Exception\LogicException;
1819
use Symfony\Component\DependencyInjection\Reference;
@@ -29,6 +30,7 @@
2930
*
3031
* @author Fabien Potencier <fabien@symfony.com>
3132
* @author Jeremy Mikola <jmikola@gmail.com>
33+
* @author Kévin Dunglas <dunglas@gmail.com>
3234
*/
3335
class FrameworkExtension extends Extension
3436
{
@@ -122,11 +124,10 @@ public function load(array $configs, ContainerBuilder $container)
122124
}
123125

124126
$this->registerAnnotationsConfiguration($config['annotations'], $container, $loader);
125-
126127
$this->registerPropertyAccessConfiguration($config['property_access'], $container);
127128

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);
130131
}
131132

132133
$loader->load('debug_prod.xml');
@@ -874,6 +875,86 @@ private function registerSecurityCsrfConfiguration(array $config, ContainerBuild
874875
$loader->load('security_csrf.xml');
875876
}
876877

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+
877958
/**
878959
* Returns the base path for the XSD files.
879960
*

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<xsd:element name="validation" type="validation" minOccurs="0" maxOccurs="1" />
3333
<xsd:element name="annotations" type="annotations" minOccurs="0" maxOccurs="1" />
3434
<xsd:element name="property-access" type="property_access" minOccurs="0" maxOccurs="1" />
35+
<xsd:element name="serializer" type="serializer" minOccurs="0" maxOccurs="1" />
3536
</xsd:all>
3637

3738
<xsd:attribute name="http-method-override" type="xsd:boolean" />
@@ -210,4 +211,10 @@
210211
<xsd:attribute name="magic-call" type="xsd:boolean" />
211212
<xsd:attribute name="throw-exception-on-invalid-index" type="xsd:boolean" />
212213
</xsd:complexType>
214+
215+
<xsd:complexType name="serializer">
216+
<xsd:attribute name="enabled" type="xsd:boolean" />
217+
<xsd:attribute name="cache" type="xsd:string" />
218+
<xsd:attribute name="enable-annotations" type="xsd:boolean" />
219+
</xsd:complexType>
213220
</xsd:schema>

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

+21-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,31 @@
1717
</service>
1818

1919
<!-- Normalizer -->
20-
<service id="serializer.normalizer.get_set_method" class="Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer" public="false">
20+
<service id="serializer.normalizer.object" class="Symfony\Component\Serializer\Normalizer\ObjectNormalizer" public="false">
21+
<argument type="service" id="serializer.mapping.class_metadata_factory" />
22+
2123
<!-- Run after all custom serializers -->
2224
<tag name="serializer.normalizer" priority="-1000" />
2325
</service>
2426

27+
<!-- Loader -->
28+
<service id="serializer.mapping.chain_loader" class="Symfony\Component\Serializer\Mapping\Loader\LoaderChain" public="false">
29+
<argument type="collection" />
30+
</service>
31+
32+
<!-- Class Metadata Factory -->
33+
<service id="serializer.mapping.class_metadata_factory" class="Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory" public="false">
34+
<argument type="service" id="serializer.mapping.chain_loader" />
35+
<argument>null</argument>
36+
</service>
37+
38+
<!-- Cache -->
39+
<service id="serializer.mapping.cache.apc" class="Doctrine\Common\Cache\ApcCache" public="false">
40+
<call method="setNamespace">
41+
<argument>%serializer.mapping.cache.prefix%</argument>
42+
</call>
43+
</service>
44+
2545
<!-- Encoders -->
2646
<service id="serializer.encoder.xml" class="%serializer.encoder.xml.class%" public="false" >
2747
<tag name="serializer.encoder" />

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ protected static function getBundleDefaultConfig()
162162
),
163163
'serializer' => array(
164164
'enabled' => false,
165+
'enable_annotations' => false,
165166
),
166167
'property_access' => array(
167168
'magic_call' => false,

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
'debug' => true,
6161
'file_cache_dir' => '%kernel.cache_dir%/annotations',
6262
),
63+
'serializer' => array('enabled' => true),
6364
'ide' => 'file%%link%%format',
6465
'request' => array(
6566
'formats' => array(

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml

+1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@
3737
<framework:translator enabled="true" fallback="fr" logging="true" />
3838
<framework:validation enabled="true" cache="apc" />
3939
<framework:annotations cache="file" debug="true" file-cache-dir="%kernel.cache_dir%/annotations" />
40+
<framework:serializer enabled="true" />
4041
</framework:config>
4142
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ framework:
4646
cache: file
4747
debug: true
4848
file_cache_dir: %kernel.cache_dir%/annotations
49+
serializer: { enabled: true }
4950
ide: file%%link%%format
5051
request:
5152
formats:

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,18 @@ public function testStopwatchEnabledWithDebugModeDisabled()
503503
$this->assertTrue($container->has('debug.stopwatch'));
504504
}
505505

506+
public function testSerializerDisabled()
507+
{
508+
$container = $this->createContainerFromFile('default_config');
509+
$this->assertFalse($container->has('serializer'));
510+
}
511+
512+
public function testSerializerEnabled()
513+
{
514+
$container = $this->createContainerFromFile('full');
515+
$this->assertTrue($container->has('serializer'));
516+
}
517+
506518
protected function createContainer(array $data = array())
507519
{
508520
return new ContainerBuilder(new ParameterBag(array_merge(array(

0 commit comments

Comments
 (0)