From ed71ee64073ca4610ea22f79148ea60a54beba06 Mon Sep 17 00:00:00 2001 From: matlec Date: Sat, 2 Aug 2025 18:50:49 +0200 Subject: [PATCH] [Serializer] Deprecate XML configuration format --- UPGRADE-7.4.md | 5 + .../CacheWarmer/SerializerCacheWarmerTest.php | 94 ++++++++++++++++--- src/Symfony/Component/Serializer/CHANGELOG.md | 1 + .../Mapping/Loader/XmlFileLoader.php | 4 + .../Mapping/Loader/XmlFileLoaderTest.php | 4 + 5 files changed, 97 insertions(+), 11 deletions(-) diff --git a/UPGRADE-7.4.md b/UPGRADE-7.4.md index 95e098365d66..86831cb79a7a 100644 --- a/UPGRADE-7.4.md +++ b/UPGRADE-7.4.md @@ -51,6 +51,11 @@ Security * Deprecate `AbstractListener::__invoke` * Deprecate `LazyFirewallContext::__invoke()` +Serializer +---------- + +* Deprecate XML configuration format, use YAML or attributes instead + Translation ----------- diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php index 5c19d2a3f530..f017830f6d7a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php @@ -12,6 +12,8 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\IgnoreDeprecations; use Symfony\Bundle\FrameworkBundle\CacheWarmer\SerializerCacheWarmer; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Component\Cache\Adapter\NullAdapter; @@ -39,8 +41,26 @@ private function getArrayPool(string $file): PhpArrayAdapter return $this->arrayPool = new PhpArrayAdapter($file, new NullAdapter()); } - #[DataProvider('loaderProvider')] - public function testWarmUp(array $loaders) + #[DataProvider('yamlLoaderProvider')] + public function testYamlWarmUp(array $loaders) + { + $file = sys_get_temp_dir().'/cache-serializer.php'; + @unlink($file); + + $warmer = new SerializerCacheWarmer($loaders, $file); + $warmer->warmUp(\dirname($file), \dirname($file)); + + $this->assertFileExists($file); + + $arrayPool = new PhpArrayAdapter($file, new NullAdapter()); + + $this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit()); + } + + #[IgnoreDeprecations] + #[Group('legacy')] + #[DataProvider('xmlLoaderProvider')] + public function testXmlWarmUp(array $loaders) { $file = sys_get_temp_dir().'/cache-serializer.php'; @unlink($file); @@ -53,11 +73,31 @@ public function testWarmUp(array $loaders) $arrayPool = $this->getArrayPool($file); $this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit()); + } + + #[DataProvider('yamlLoaderProvider')] + public function testYamlWarmUpAbsoluteFilePath(array $loaders) + { + $file = sys_get_temp_dir().'/0/cache-serializer.php'; + @unlink($file); + + $cacheDir = sys_get_temp_dir().'/1'; + + $warmer = new SerializerCacheWarmer($loaders, $file); + $warmer->warmUp($cacheDir, $cacheDir); + + $this->assertFileExists($file); + $this->assertFileDoesNotExist($cacheDir.'/cache-serializer.php'); + + $arrayPool = new PhpArrayAdapter($file, new NullAdapter()); + $this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit()); } - #[DataProvider('loaderProvider')] - public function testWarmUpAbsoluteFilePath(array $loaders) + #[IgnoreDeprecations] + #[Group('legacy')] + #[DataProvider('xmlLoaderProvider')] + public function testXmlWarmUpAbsoluteFilePath(array $loaders) { $file = sys_get_temp_dir().'/0/cache-serializer.php'; @unlink($file); @@ -73,11 +113,10 @@ public function testWarmUpAbsoluteFilePath(array $loaders) $arrayPool = $this->getArrayPool($file); $this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit()); - $this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit()); } - #[DataProvider('loaderProvider')] - public function testWarmUpWithoutBuildDir(array $loaders) + #[DataProvider('yamlLoaderProvider')] + public function testYamlWarmUpWithoutBuildDir(array $loaders) { $file = sys_get_temp_dir().'/cache-serializer.php'; @unlink($file); @@ -89,30 +128,63 @@ public function testWarmUpWithoutBuildDir(array $loaders) $arrayPool = $this->getArrayPool($file); - $this->assertFalse($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit()); $this->assertFalse($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit()); } - public static function loaderProvider(): array + #[IgnoreDeprecations] + #[Group('legacy')] + #[DataProvider('xmlLoaderProvider')] + public function testXmlWarmUpWithoutBuildDir(array $loaders) + { + $file = sys_get_temp_dir().'/cache-serializer.php'; + @unlink($file); + + $warmer = new SerializerCacheWarmer($loaders, $file); + $warmer->warmUp(\dirname($file)); + + $this->assertFileDoesNotExist($file); + + $arrayPool = new PhpArrayAdapter($file, new NullAdapter()); + + $this->assertFalse($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit()); + } + + public static function yamlLoaderProvider(): array { return [ [ [ new LoaderChain([ - new XmlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/person.xml'), new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/author.yml'), ]), ], ], [ [ - new XmlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/person.xml'), new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/author.yml'), ], ], ]; } + public static function xmlLoaderProvider(): array + { + return [ + [ + [ + new LoaderChain([ + new XmlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/person.xml'), + ]), + ], + ], + [ + [ + new XmlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/person.xml'), + ], + ], + ]; + } + public function testWarmUpWithoutLoader() { $file = sys_get_temp_dir().'/cache-serializer-without-loader.php'; diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 641837562117..a455f8bf22ce 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * Add `CDATA_WRAPPING_NAME_PATTERN` support to `XmlEncoder` * Add support for `can*()` methods to `AttributeLoader` + * Deprecate XML configuration format, use YAML or attributes instead 7.3 --- diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php index ac6fee2db417..40387938a8a9 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php @@ -23,6 +23,8 @@ * Loads XML mapping files. * * @author Kévin Dunglas + * + * @deprecated since Symfony 7.4, use another loader instead */ class XmlFileLoader extends FileLoader { @@ -42,6 +44,8 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool $attributesMetadata = $classMetadata->getAttributesMetadata(); if (isset($this->classes[$classMetadata->getName()])) { + trigger_deprecation('symfony/serializer', '7.4', 'XML configuration format is deprecated, use YAML or attributes instead.'); + $xml = $this->classes[$classMetadata->getName()]; foreach ($xml->attribute as $attribute) { diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php index 45b5aeb10571..b84b87fb9b58 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Serializer\Tests\Mapping\Loader; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\IgnoreDeprecations; use PHPUnit\Framework\TestCase; use Symfony\Component\Serializer\Mapping\AttributeMetadata; use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; @@ -32,6 +34,8 @@ /** * @author Kévin Dunglas */ +#[IgnoreDeprecations] +#[Group('legacy')] class XmlFileLoaderTest extends TestCase { use ContextMappingTestTrait;