Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3023e4b

Browse filesBrowse files
committed
feature #21924 [FrameworkBundle] Allow to configure Serializer mapping paths (chalasr)
This PR was merged into the 3.3-dev branch. Discussion ---------- [FrameworkBundle] Allow to configure Serializer mapping paths | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21187 | License | MIT | Doc PR | todo Follows #19086 for the Serializer Commits ------- 5446903 [FrameworkBundle] Allow configuring serializer mapping paths
2 parents 5dcef29 + 5446903 commit 3023e4b
Copy full SHA for 3023e4b

File tree

15 files changed

+112
-38
lines changed
Filter options

15 files changed

+112
-38
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ CHANGELOG
3737
`Symfony\Bundle\WebServerBundle\WebServerBundle` in your AppKernel to use them.
3838
* Added `$defaultLocale` as 3rd argument of `Translator::__construct()`
3939
making `Translator` works with any PSR-11 container
40+
* Added `framework.serializer.mapping` config option allowing to define custom
41+
serialization mapping files and directories
4042

4143
3.2.0
4244
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,15 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode)
687687
->scalarNode('cache')->end()
688688
->scalarNode('name_converter')->end()
689689
->scalarNode('circular_reference_handler')->end()
690+
->arrayNode('mapping')
691+
->addDefaultsIfNotSet()
692+
->fixXmlConfig('path')
693+
->children()
694+
->arrayNode('paths')
695+
->prototype('scalar')->end()
696+
->end()
697+
->end()
698+
->end()
690699
->end()
691700
->end()
692701
->end()

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+29-36Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
986986
$container->setParameter('validator.translation_domain', $config['translation_domain']);
987987

988988
$files = array('xml' => array(), 'yml' => array());
989-
$this->getValidatorMappingFiles($container, $files);
990-
$this->getValidatorMappingFilesFromConfig($container, $config, $files);
989+
$this->registerValidatorMapping($container, $config, $files);
991990

992991
if (!empty($files['xml'])) {
993992
$validatorBuilder->addMethodCall('addXmlMappings', array($files['xml']));
@@ -1028,51 +1027,54 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
10281027
}
10291028
}
10301029

1031-
private function getValidatorMappingFiles(ContainerBuilder $container, array &$files)
1030+
private function registerValidatorMapping(ContainerBuilder $container, array $config, array &$files)
10321031
{
1032+
$fileRecorder = function ($extension, $path) use (&$files) {
1033+
$files['yaml' === $extension ? 'yml' : $extension][] = $path;
1034+
};
1035+
10331036
if (interface_exists('Symfony\Component\Form\FormInterface')) {
10341037
$reflClass = new \ReflectionClass('Symfony\Component\Form\FormInterface');
1035-
$files['xml'][] = dirname($reflClass->getFileName()).'/Resources/config/validation.xml';
1038+
$fileRecorder('xml', dirname($reflClass->getFileName()).'/Resources/config/validation.xml');
10361039
}
10371040

10381041
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
10391042
$dirname = $bundle['path'];
10401043

10411044
if ($container->fileExists($file = $dirname.'/Resources/config/validation.yml', false)) {
1042-
$files['yml'][] = $file;
1045+
$fileRecorder('yml', $file);
10431046
}
10441047

10451048
if ($container->fileExists($file = $dirname.'/Resources/config/validation.xml', false)) {
1046-
$files['xml'][] = $file;
1049+
$fileRecorder('xml', $file);
10471050
}
10481051

10491052
if ($container->fileExists($dir = $dirname.'/Resources/config/validation', '/^$/')) {
1050-
$this->getValidatorMappingFilesFromDir($dir, $files);
1053+
$this->registerMappingFilesFromDir($dir, $fileRecorder);
10511054
}
10521055
}
1056+
1057+
$this->registerMappingFilesFromConfig($container, $config, $fileRecorder);
10531058
}
10541059

1055-
private function getValidatorMappingFilesFromDir($dir, array &$files)
1060+
private function registerMappingFilesFromDir($dir, callable $fileRecorder)
10561061
{
10571062
foreach (Finder::create()->followLinks()->files()->in($dir)->name('/\.(xml|ya?ml)$/') as $file) {
1058-
$extension = $file->getExtension();
1059-
$files['yaml' === $extension ? 'yml' : $extension][] = $file->getRealpath();
1063+
$fileRecorder($file->getExtension(), $file->getRealPath());
10601064
}
10611065
}
10621066

1063-
private function getValidatorMappingFilesFromConfig(ContainerBuilder $container, array $config, array &$files)
1067+
private function registerMappingFilesFromConfig(ContainerBuilder $container, array $config, callable $fileRecorder)
10641068
{
10651069
foreach ($config['mapping']['paths'] as $path) {
10661070
if (is_dir($path)) {
1067-
$this->getValidatorMappingFilesFromDir($path, $files);
1071+
$this->registerMappingFilesFromDir($path, $fileRecorder);
10681072
$container->addResource(new DirectoryResource($path, '/^$/'));
10691073
} elseif ($container->fileExists($path, false)) {
1070-
if (preg_match('/\.(xml|ya?ml)$/', $path, $matches)) {
1071-
$extension = $matches[1];
1072-
$files['yaml' === $extension ? 'yml' : $extension][] = $path;
1073-
} else {
1074+
if (!preg_match('/\.(xml|ya?ml)$/', $path, $matches)) {
10741075
throw new \RuntimeException(sprintf('Unsupported mapping type in "%s", supported types are XML & Yaml.', $path));
10751076
}
1077+
$fileRecorder($matches[1], $path);
10761078
} else {
10771079
throw new \RuntimeException(sprintf('Could not open file or directory "%s".', $path));
10781080
}
@@ -1230,39 +1232,30 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
12301232
$serializerLoaders[] = $annotationLoader;
12311233
}
12321234

1235+
$fileRecorder = function ($extension, $path) use (&$serializerLoaders) {
1236+
$definition = new Definition(in_array($extension, array('yaml', 'yml')) ? 'Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader' : 'Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader', array($path));
1237+
$definition->setPublic(false);
1238+
$serializerLoaders[] = $definition;
1239+
};
1240+
12331241
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
12341242
$dirname = $bundle['path'];
12351243

12361244
if ($container->fileExists($file = $dirname.'/Resources/config/serialization.xml', false)) {
1237-
$definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader', array($file));
1238-
$definition->setPublic(false);
1239-
1240-
$serializerLoaders[] = $definition;
1245+
$fileRecorder('xml', $file);
12411246
}
12421247

12431248
if ($container->fileExists($file = $dirname.'/Resources/config/serialization.yml', false)) {
1244-
$definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader', array($file));
1245-
$definition->setPublic(false);
1246-
1247-
$serializerLoaders[] = $definition;
1249+
$fileRecorder('yml', $file);
12481250
}
12491251

12501252
if ($container->fileExists($dir = $dirname.'/Resources/config/serialization')) {
1251-
foreach (Finder::create()->followLinks()->files()->in($dir)->name('*.xml') as $file) {
1252-
$definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader', array($file->getPathname()));
1253-
$definition->setPublic(false);
1254-
1255-
$serializerLoaders[] = $definition;
1256-
}
1257-
foreach (Finder::create()->followLinks()->files()->in($dir)->name('*.yml') as $file) {
1258-
$definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader', array($file->getPathname()));
1259-
$definition->setPublic(false);
1260-
1261-
$serializerLoaders[] = $definition;
1262-
}
1253+
$this->registerMappingFilesFromDir($dir, $fileRecorder);
12631254
}
12641255
}
12651256

1257+
$this->registerMappingFilesFromConfig($container, $config, $fileRecorder);
1258+
12661259
$chainLoader->replaceArgument(0, $serializerLoaders);
12671260
$container->getDefinition('serializer.mapping.cache_warmer')->replaceArgument(0, $serializerLoaders);
12681261

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@
176176
<xsd:complexType name="validation">
177177
<xsd:choice minOccurs="0" maxOccurs="unbounded">
178178
<xsd:element name="static-method" type="xsd:string" />
179-
<xsd:element name="mapping" type="validation_mapping" />
179+
<xsd:element name="mapping" type="file_mapping" />
180180
</xsd:choice>
181181

182182
<xsd:attribute name="enabled" type="xsd:boolean" />
@@ -185,7 +185,7 @@
185185
<xsd:attribute name="static-method" type="xsd:boolean" />
186186
</xsd:complexType>
187187

188-
<xsd:complexType name="validation_mapping">
188+
<xsd:complexType name="file_mapping">
189189
<xsd:sequence>
190190
<xsd:element name="path" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
191191
</xsd:sequence>
@@ -204,6 +204,9 @@
204204
</xsd:complexType>
205205

206206
<xsd:complexType name="serializer">
207+
<xsd:choice minOccurs="0" maxOccurs="unbounded">
208+
<xsd:element name="mapping" type="file_mapping" />
209+
</xsd:choice>
207210
<xsd:attribute name="enabled" type="xsd:boolean" />
208211
<xsd:attribute name="cache" type="xsd:string" />
209212
<xsd:attribute name="enable-annotations" type="xsd:boolean" />

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ protected static function getBundleDefaultConfig()
226226
'serializer' => array(
227227
'enabled' => !class_exists(FullStack::class),
228228
'enable_annotations' => !class_exists(FullStack::class),
229+
'mapping' => array('paths' => array()),
229230
),
230231
'property_access' => array(
231232
'magic_call' => false,

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serialization.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serialization.xml
Whitespace-only changes.

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serialization.yml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serialization.yml
Whitespace-only changes.

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serializer_mapping/files/foo.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serializer_mapping/files/foo.xml
Whitespace-only changes.

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serializer_mapping/files/foo.yml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serializer_mapping/files/foo.yml
Whitespace-only changes.

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yaml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yaml
Whitespace-only changes.

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yml
Whitespace-only changes.
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', array(
4+
'annotations' => array('enabled' => true),
5+
'serializer' => array(
6+
'enable_annotations' => true,
7+
'mapping' => array(
8+
'paths' => array(
9+
'%kernel.root_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/files',
10+
'%kernel.root_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yml',
11+
'%kernel.root_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yaml',
12+
),
13+
),
14+
),
15+
));
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony">
6+
7+
<framework:config>
8+
<framework:annotations enabled="true" />
9+
<framework:serializer enable-annotations="true">
10+
<framework:mapping>
11+
<framework:path>%kernel.root_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/files</framework:path>
12+
<framework:path>%kernel.root_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yml</framework:path>
13+
<framework:path>%kernel.root_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yaml</framework:path>
14+
</framework:mapping>
15+
</framework:serializer>
16+
</framework:config>
17+
</container>
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
framework:
2+
annotations:
3+
enabled: true
4+
serializer:
5+
enable_annotations: true
6+
mapping:
7+
paths:
8+
- "%kernel.root_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/files"
9+
- "%kernel.root_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yml"
10+
- "%kernel.root_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yaml"

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
use Symfony\Component\Cache\Adapter\RedisAdapter;
2929
use Symfony\Component\DependencyInjection\ChildDefinition;
3030
use Symfony\Component\DependencyInjection\ContainerBuilder;
31+
use Symfony\Component\DependencyInjection\Definition;
3132
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
3233
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
3334
use Symfony\Component\DependencyInjection\Reference;
3435
use Symfony\Component\PropertyAccess\PropertyAccessor;
36+
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
3537
use Symfony\Component\Serializer\Serializer;
3638
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
3739
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
@@ -800,6 +802,28 @@ public function testDeprecatedSerializerCacheOption()
800802
$this->assertEquals(new Reference('foo'), $cache);
801803
}
802804

805+
public function testSerializerMapping()
806+
{
807+
$container = $this->createContainerFromFile('serializer_mapping', array('kernel.bundles_metadata' => array('TestBundle' => array('namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/TestBundle', 'parent' => null))));
808+
$configDir = __DIR__.'/Fixtures/TestBundle/Resources/config';
809+
$expectedLoaders = array(
810+
new Definition(AnnotationLoader::class, array(new Reference('annotation_reader'))),
811+
new Definition(XmlFileLoader::class, array($configDir.'/serialization.xml')),
812+
new Definition(YamlFileLoader::class, array($configDir.'/serialization.yml')),
813+
new Definition(XmlFileLoader::class, array($configDir.'/serializer_mapping/files/foo.xml')),
814+
new Definition(YamlFileLoader::class, array($configDir.'/serializer_mapping/files/foo.yml')),
815+
new Definition(YamlFileLoader::class, array($configDir.'/serializer_mapping/serialization.yml')),
816+
new Definition(YamlFileLoader::class, array($configDir.'/serializer_mapping/serialization.yaml')),
817+
);
818+
819+
foreach ($expectedLoaders as $definition) {
820+
$definition->setPublic(false);
821+
}
822+
823+
$loaders = $container->getDefinition('serializer.mapping.chain_loader')->getArgument(0);
824+
$this->assertEquals(sort($expectedLoaders), sort($loaders));
825+
}
826+
803827
public function testAssetHelperWhenAssetsAreEnabled()
804828
{
805829
$container = $this->createContainerFromFile('full');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.