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
-------

43895b8dae Add default mapping path for serializer component
This commit is contained in:
Fabien Potencier 2017-11-10 10:56:06 -08:00
commit 93f206e878
3 changed files with 17 additions and 2 deletions

View File

@ -1471,6 +1471,11 @@ class FrameworkExtension extends Extension
}
}
$projectDir = $container->getParameter('kernel.project_dir');
if ($container->fileExists($dir = $projectDir.'/config/serializer', '/^$/')) {
$this->registerMappingFilesFromDir($dir, $fileRecorder);
}
$this->registerMappingFilesFromConfig($container, $config, $fileRecorder);
$chainLoader->replaceArgument(0, $serializerLoaders);

View File

@ -749,7 +749,7 @@ abstract class FrameworkExtensionTest extends TestCase
$argument = $container->getDefinition('serializer.mapping.chain_loader')->getArgument(0);
$this->assertCount(1, $argument);
$this->assertCount(2, $argument);
$this->assertEquals('Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader', $argument[0]->getClass());
$this->assertNull($container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
$this->assertEquals(new Reference('serializer.name_converter.camel_case_to_snake_case'), $container->getDefinition('serializer.normalizer.object')->getArgument(1));
@ -861,11 +861,13 @@ abstract class FrameworkExtensionTest extends TestCase
public function testSerializerMapping()
{
$container = $this->createContainerFromFile('serializer_mapping', array('kernel.bundles_metadata' => array('TestBundle' => array('namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/TestBundle', 'parent' => null))));
$projectDir = $container->getParameter('kernel.project_dir');
$configDir = __DIR__.'/Fixtures/TestBundle/Resources/config';
$expectedLoaders = array(
new Definition(AnnotationLoader::class, array(new Reference('annotation_reader'))),
new Definition(XmlFileLoader::class, array($configDir.'/serialization.xml')),
new Definition(YamlFileLoader::class, array($configDir.'/serialization.yml')),
new Definition(YamlFileLoader::class, array($projectDir.'/config/serializer/foo.yml')),
new Definition(XmlFileLoader::class, array($configDir.'/serializer_mapping/files/foo.xml')),
new Definition(YamlFileLoader::class, array($configDir.'/serializer_mapping/files/foo.yml')),
new Definition(YamlFileLoader::class, array($configDir.'/serializer_mapping/serialization.yml')),
@ -873,11 +875,19 @@ abstract class FrameworkExtensionTest extends TestCase
);
foreach ($expectedLoaders as $definition) {
if (is_file($arg = $definition->getArgument(0))) {
$definition->replaceArgument(0, strtr($arg, '/', DIRECTORY_SEPARATOR));
}
$definition->setPublic(false);
}
$loaders = $container->getDefinition('serializer.mapping.chain_loader')->getArgument(0);
$this->assertEquals(sort($expectedLoaders), sort($loaders));
foreach ($loaders as $loader) {
if (is_file($arg = $loader->getArgument(0))) {
$loader->replaceArgument(0, strtr($arg, '/', DIRECTORY_SEPARATOR));
}
}
$this->assertEquals($expectedLoaders, $loaders);
}
public function testAssetHelperWhenAssetsAreEnabled()