diff --git a/UPGRADE-8.0.md b/UPGRADE-8.0.md index b83201a86c831..dbd7114d38905 100644 --- a/UPGRADE-8.0.md +++ b/UPGRADE-8.0.md @@ -341,6 +341,7 @@ Serializer public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string; ``` * Remove `AdvancedNameConverterInterface`, use `NameConverterInterface` instead + * Remove the `CompiledClassMetadataFactory` and `CompiledClassMetadataCacheWarmer` classes TwigBridge ---------- diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 31d7ed05664ef..39e2b0a127a29 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -21,6 +21,7 @@ CHANGELOG public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string; ``` * Remove `AdvancedNameConverterInterface`, use `NameConverterInterface` instead + * Remove the `CompiledClassMetadataFactory` and `CompiledClassMetadataCacheWarmer` classes 7.3 --- diff --git a/src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php b/src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php deleted file mode 100644 index 1bd085024d071..0000000000000 --- a/src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php +++ /dev/null @@ -1,55 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Serializer\CacheWarmer; - -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; -use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryCompiler; -use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; - -trigger_deprecation('symfony/serializer', '7.3', 'The "%s" class is deprecated.', CompiledClassMetadataCacheWarmer::class); - -/** - * @author Fabien Bourigault - * - * @deprecated since Symfony 7.3 - */ -final class CompiledClassMetadataCacheWarmer implements CacheWarmerInterface -{ - public function __construct( - private readonly array $classesToCompile, - private readonly ClassMetadataFactoryInterface $classMetadataFactory, - private readonly ClassMetadataFactoryCompiler $classMetadataFactoryCompiler, - private readonly Filesystem $filesystem, - ) { - } - - public function warmUp(string $cacheDir, ?string $buildDir = null): array - { - $metadatas = []; - - foreach ($this->classesToCompile as $classToCompile) { - $metadatas[] = $this->classMetadataFactory->getMetadataFor($classToCompile); - } - - $code = $this->classMetadataFactoryCompiler->compile($metadatas); - - $this->filesystem->dumpFile("{$cacheDir}/serializer.class.metadata.php", $code); - - return []; - } - - public function isOptional(): bool - { - return true; - } -} diff --git a/src/Symfony/Component/Serializer/Mapping/Factory/CompiledClassMetadataFactory.php b/src/Symfony/Component/Serializer/Mapping/Factory/CompiledClassMetadataFactory.php deleted file mode 100644 index 759da166d4fdd..0000000000000 --- a/src/Symfony/Component/Serializer/Mapping/Factory/CompiledClassMetadataFactory.php +++ /dev/null @@ -1,79 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Serializer\Mapping\Factory; - -use Symfony\Component\Serializer\Mapping\AttributeMetadata; -use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; -use Symfony\Component\Serializer\Mapping\ClassMetadata; -use Symfony\Component\Serializer\Mapping\ClassMetadataInterface; - -trigger_deprecation('symfony/serializer', '7.3', 'The "%s" class is deprecated.', CompiledClassMetadataFactory::class); - -/** - * @author Fabien Bourigault - * - * @deprecated since Symfony 7.3 - */ -final class CompiledClassMetadataFactory implements ClassMetadataFactoryInterface -{ - private array $compiledClassMetadata = []; - - private array $loadedClasses = []; - - public function __construct( - string $compiledClassMetadataFile, - private readonly ClassMetadataFactoryInterface $classMetadataFactory, - ) { - if (!file_exists($compiledClassMetadataFile)) { - throw new \RuntimeException("File \"{$compiledClassMetadataFile}\" could not be found."); - } - - $compiledClassMetadata = require $compiledClassMetadataFile; - if (!\is_array($compiledClassMetadata)) { - throw new \RuntimeException(\sprintf('Compiled metadata must be of the type array, %s given.', \gettype($compiledClassMetadata))); - } - - $this->compiledClassMetadata = $compiledClassMetadata; - } - - public function getMetadataFor(string|object $value): ClassMetadataInterface - { - $className = \is_object($value) ? $value::class : $value; - - if (!isset($this->compiledClassMetadata[$className])) { - return $this->classMetadataFactory->getMetadataFor($value); - } - - if (!isset($this->loadedClasses[$className])) { - $classMetadata = new ClassMetadata($className); - foreach ($this->compiledClassMetadata[$className][0] as $name => $compiledAttributesMetadata) { - $classMetadata->attributesMetadata[$name] = $attributeMetadata = new AttributeMetadata($name); - [$attributeMetadata->groups, $attributeMetadata->maxDepth, $attributeMetadata->serializedName] = $compiledAttributesMetadata; - } - $classMetadata->classDiscriminatorMapping = $this->compiledClassMetadata[$className][1] - ? new ClassDiscriminatorMapping(...$this->compiledClassMetadata[$className][1]) - : null - ; - - $this->loadedClasses[$className] = $classMetadata; - } - - return $this->loadedClasses[$className]; - } - - public function hasMetadataFor(mixed $value): bool - { - $className = \is_object($value) ? $value::class : $value; - - return isset($this->compiledClassMetadata[$className]) || $this->classMetadataFactory->hasMetadataFor($value); - } -} diff --git a/src/Symfony/Component/Serializer/Tests/CacheWarmer/CompiledClassMetadataCacheWarmerTest.php b/src/Symfony/Component/Serializer/Tests/CacheWarmer/CompiledClassMetadataCacheWarmerTest.php deleted file mode 100644 index c9f5081b680b0..0000000000000 --- a/src/Symfony/Component/Serializer/Tests/CacheWarmer/CompiledClassMetadataCacheWarmerTest.php +++ /dev/null @@ -1,70 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Serializer\Tests\CacheWarmer; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; -use Symfony\Component\Serializer\CacheWarmer\CompiledClassMetadataCacheWarmer; -use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryCompiler; -use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; - -/** - * @group legacy - */ -final class CompiledClassMetadataCacheWarmerTest extends TestCase -{ - public function testItImplementsCacheWarmerInterface() - { - $classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class); - $filesystem = $this->createMock(Filesystem::class); - - $compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], $classMetadataFactory, new ClassMetadataFactoryCompiler(), $filesystem); - - $this->assertInstanceOf(CacheWarmerInterface::class, $compiledClassMetadataCacheWarmer); - } - - public function testItIsAnOptionalCacheWarmer() - { - $classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class); - $filesystem = $this->createMock(Filesystem::class); - - $compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], $classMetadataFactory, new ClassMetadataFactoryCompiler(), $filesystem); - - $this->assertTrue($compiledClassMetadataCacheWarmer->isOptional()); - } - - public function testItDumpCompiledClassMetadatas() - { - $classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class); - - $code = <<createMock(Filesystem::class); - $filesystem - ->expects($this->once()) - ->method('dumpFile') - ->with('/var/cache/prod/serializer.class.metadata.php', $code) - ; - - $compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], $classMetadataFactory, new ClassMetadataFactoryCompiler(), $filesystem); - - $compiledClassMetadataCacheWarmer->warmUp('/var/cache/prod'); - } -} diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Factory/CompiledClassMetadataFactoryTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Factory/CompiledClassMetadataFactoryTest.php deleted file mode 100644 index e77a8bf3ee63f..0000000000000 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Factory/CompiledClassMetadataFactoryTest.php +++ /dev/null @@ -1,142 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Serializer\Tests\Mapping\Factory; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Serializer\Mapping\AttributeMetadata; -use Symfony\Component\Serializer\Mapping\ClassMetadata; -use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; -use Symfony\Component\Serializer\Mapping\Factory\CompiledClassMetadataFactory; -use Symfony\Component\Serializer\Tests\Fixtures\Attributes\SerializedNameDummy; -use Symfony\Component\Serializer\Tests\Fixtures\Dummy; - -/** - * @author Fabien Bourigault - * - * @group legacy - */ -final class CompiledClassMetadataFactoryTest extends TestCase -{ - public function testItImplementsClassMetadataFactoryInterface() - { - $classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class); - $compiledClassMetadataFactory = new CompiledClassMetadataFactory(__DIR__.'/../../Fixtures/serializer.class.metadata.php', $classMetadataFactory); - - $this->assertInstanceOf(ClassMetadataFactoryInterface::class, $compiledClassMetadataFactory); - } - - public function testItThrowAnExceptionWhenCacheFileIsNotFound() - { - $classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class); - - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessageMatches('#File ".*/Fixtures/not-found-serializer.class.metadata.php" could not be found.#'); - - new CompiledClassMetadataFactory(__DIR__.'/../../Fixtures/not-found-serializer.class.metadata.php', $classMetadataFactory); - } - - public function testItThrowAnExceptionWhenMetadataIsNotOfTypeArray() - { - $classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class); - - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Compiled metadata must be of the type array, object given.'); - - new CompiledClassMetadataFactory(__DIR__.'/../../Fixtures/object-metadata.php', $classMetadataFactory); - } - - /** - * @dataProvider valueProvider - */ - public function testItReturnsTheCompiledMetadata($value) - { - $classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class); - $compiledClassMetadataFactory = new CompiledClassMetadataFactory(__DIR__.'/../../Fixtures/serializer.class.metadata.php', $classMetadataFactory); - - $classMetadataFactory - ->expects($this->never()) - ->method('getMetadataFor') - ; - - $expected = new ClassMetadata(Dummy::class); - $expected->addAttributeMetadata(new AttributeMetadata('foo')); - $expected->addAttributeMetadata(new AttributeMetadata('bar')); - $expected->addAttributeMetadata(new AttributeMetadata('baz')); - $expected->addAttributeMetadata(new AttributeMetadata('qux')); - - $this->assertEquals($expected, $compiledClassMetadataFactory->getMetadataFor($value)); - } - - public function testItDelegatesGetMetadataForCall() - { - $classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class); - $compiledClassMetadataFactory = new CompiledClassMetadataFactory(__DIR__.'/../../Fixtures/serializer.class.metadata.php', $classMetadataFactory); - - $classMetadata = new ClassMetadata(SerializedNameDummy::class); - - $classMetadataFactory - ->expects($this->once()) - ->method('getMetadataFor') - ->with(SerializedNameDummy::class) - ->willReturn($classMetadata) - ; - - $this->assertEquals($classMetadata, $compiledClassMetadataFactory->getMetadataFor(SerializedNameDummy::class)); - } - - public function testItReturnsTheSameInstance() - { - $classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class); - $compiledClassMetadataFactory = new CompiledClassMetadataFactory(__DIR__.'/../../Fixtures/serializer.class.metadata.php', $classMetadataFactory); - - $this->assertSame($compiledClassMetadataFactory->getMetadataFor(Dummy::class), $compiledClassMetadataFactory->getMetadataFor(Dummy::class)); - } - - /** - * @dataProvider valueProvider - */ - public function testItHasMetadataFor($value) - { - $classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class); - $compiledClassMetadataFactory = new CompiledClassMetadataFactory(__DIR__.'/../../Fixtures/serializer.class.metadata.php', $classMetadataFactory); - - $classMetadataFactory - ->expects($this->never()) - ->method('hasMetadataFor') - ; - - $this->assertTrue($compiledClassMetadataFactory->hasMetadataFor($value)); - } - - public function testItDelegatesHasMetadataForCall() - { - $classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class); - $compiledClassMetadataFactory = new CompiledClassMetadataFactory(__DIR__.'/../../Fixtures/serializer.class.metadata.php', $classMetadataFactory); - - $classMetadataFactory - ->expects($this->once()) - ->method('hasMetadataFor') - ->with(SerializedNameDummy::class) - ->willReturn(true) - ; - - $this->assertTrue($compiledClassMetadataFactory->hasMetadataFor(SerializedNameDummy::class)); - } - - public static function valueProvider() - { - return [ - [Dummy::class], - [new Dummy()], - ]; - } -}