Skip to content

Commit 93f206e

Browse files
committed
bug #24833 [FrameworkBundle] Add default mapping path for serializer component in bundle-less app (yceruto)
This PR was merged into the 3.4 branch. Discussion ---------- [FrameworkBundle] Add default mapping path for serializer component in bundle-less app | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | ToDo > http://symfony.com/doc/current/serializer.html#using-serialization-groups-annotations: > In addition to the @groups annotation, the Serializer component also supports Yaml or XML files. These files are automatically loaded when being stored in one of the following locations: >* The `serialization.yml` or `serialization.xml` file in the `Resources/config/` directory of a bundle; >* All `*.yml` and `*.xml` files in the `Resources/config/serialization/` directory of a bundle. Inspired by the second convention, this proposal adds one more but for bundle-less structure. Theoretically this is what it does for you: ```yaml framework: serializer: mapping: paths: - '%kernel.project_dir%/config/serializer/' ``` Commits ------- 43895b8 Add default mapping path for serializer component
2 parents aaa9f13 + 43895b8 commit 93f206e

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,11 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
14711471
}
14721472
}
14731473

1474+
$projectDir = $container->getParameter('kernel.project_dir');
1475+
if ($container->fileExists($dir = $projectDir.'/config/serializer', '/^$/')) {
1476+
$this->registerMappingFilesFromDir($dir, $fileRecorder);
1477+
}
1478+
14741479
$this->registerMappingFilesFromConfig($container, $config, $fileRecorder);
14751480

14761481
$chainLoader->replaceArgument(0, $serializerLoaders);

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ public function testSerializerEnabled()
749749

750750
$argument = $container->getDefinition('serializer.mapping.chain_loader')->getArgument(0);
751751

752-
$this->assertCount(1, $argument);
752+
$this->assertCount(2, $argument);
753753
$this->assertEquals('Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader', $argument[0]->getClass());
754754
$this->assertNull($container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
755755
$this->assertEquals(new Reference('serializer.name_converter.camel_case_to_snake_case'), $container->getDefinition('serializer.normalizer.object')->getArgument(1));
@@ -861,23 +861,33 @@ public function testDeprecatedSerializerCacheOption()
861861
public function testSerializerMapping()
862862
{
863863
$container = $this->createContainerFromFile('serializer_mapping', array('kernel.bundles_metadata' => array('TestBundle' => array('namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/TestBundle', 'parent' => null))));
864+
$projectDir = $container->getParameter('kernel.project_dir');
864865
$configDir = __DIR__.'/Fixtures/TestBundle/Resources/config';
865866
$expectedLoaders = array(
866867
new Definition(AnnotationLoader::class, array(new Reference('annotation_reader'))),
867868
new Definition(XmlFileLoader::class, array($configDir.'/serialization.xml')),
868869
new Definition(YamlFileLoader::class, array($configDir.'/serialization.yml')),
870+
new Definition(YamlFileLoader::class, array($projectDir.'/config/serializer/foo.yml')),
869871
new Definition(XmlFileLoader::class, array($configDir.'/serializer_mapping/files/foo.xml')),
870872
new Definition(YamlFileLoader::class, array($configDir.'/serializer_mapping/files/foo.yml')),
871873
new Definition(YamlFileLoader::class, array($configDir.'/serializer_mapping/serialization.yml')),
872874
new Definition(YamlFileLoader::class, array($configDir.'/serializer_mapping/serialization.yaml')),
873875
);
874876

875877
foreach ($expectedLoaders as $definition) {
878+
if (is_file($arg = $definition->getArgument(0))) {
879+
$definition->replaceArgument(0, strtr($arg, '/', DIRECTORY_SEPARATOR));
880+
}
876881
$definition->setPublic(false);
877882
}
878883

879884
$loaders = $container->getDefinition('serializer.mapping.chain_loader')->getArgument(0);
880-
$this->assertEquals(sort($expectedLoaders), sort($loaders));
885+
foreach ($loaders as $loader) {
886+
if (is_file($arg = $loader->getArgument(0))) {
887+
$loader->replaceArgument(0, strtr($arg, '/', DIRECTORY_SEPARATOR));
888+
}
889+
}
890+
$this->assertEquals($expectedLoaders, $loaders);
881891
}
882892

883893
public function testAssetHelperWhenAssetsAreEnabled()

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/config/serializer/foo.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)