From 32095e277f008ca8c31e035c27884572699bd0f2 Mon Sep 17 00:00:00 2001 From: butschster Date: Fri, 8 Mar 2024 14:20:09 +0400 Subject: [PATCH] Adds support for Symfony 7.x --- composer.json | 8 ++++---- src/Config/SerializerConfig.php | 15 ++++++++++++++- .../Bootloader/SerializerBootloaderTest.php | 17 ++++++++++++----- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index fd4a962..d8df065 100644 --- a/composer.json +++ b/composer.json @@ -16,13 +16,13 @@ "spiral/config": "^3.7", "doctrine/annotations": "^1.12 || ^2.0", "spiral/serializer": "^3.7", - "symfony/serializer": "^5.4 || ^6.0", - "symfony/property-access": "^5.4 || ^6.0" + "symfony/serializer": "^5.4 || ^6.0 || ^7.0", + "symfony/property-access": "^5.4 || ^6.0 || ^7.0" }, "require-dev": { "phpunit/phpunit": "^10.1", - "spiral/testing": "^3.0", - "symfony/yaml": "^5.4 || ^6.0", + "spiral/testing": "^2.7", + "symfony/yaml": "^5.4 || ^6.0 || ^7.0", "vimeo/psalm": "^5.9" }, "suggest": { diff --git a/src/Config/SerializerConfig.php b/src/Config/SerializerConfig.php index 5f21c24..a82e5bf 100644 --- a/src/Config/SerializerConfig.php +++ b/src/Config/SerializerConfig.php @@ -7,6 +7,7 @@ use Doctrine\Common\Annotations\AnnotationReader; use Symfony\Component\Messenger\Transport\Serialization\Normalizer\FlattenExceptionNormalizer; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; +use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader; use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface; use Spiral\Core\InjectableConfig; @@ -37,6 +38,18 @@ public function getNormalizers(): array public function getMetadataLoader(): LoaderInterface { - return $this->config['metadataLoader'] ?? new AnnotationLoader(new AnnotationReader()); + if (!empty($this->config['metadataLoader'])) { + return $this->config['metadataLoader']; + } + + if (\class_exists(AttributeLoader::class)) { + return new AttributeLoader(); + } + + if (\class_exists(AnnotationLoader::class)) { + return new AnnotationLoader(new AnnotationReader()); + } + + throw new \LogicException('No metadata loader found'); } } diff --git a/tests/src/Unit/Bootloader/SerializerBootloaderTest.php b/tests/src/Unit/Bootloader/SerializerBootloaderTest.php index dc2d598..438755a 100644 --- a/tests/src/Unit/Bootloader/SerializerBootloaderTest.php +++ b/tests/src/Unit/Bootloader/SerializerBootloaderTest.php @@ -18,6 +18,7 @@ use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Encoder\XmlEncoder; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; +use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader; use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface; use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer; use Symfony\Component\Serializer\Normalizer\UidNormalizer; @@ -36,8 +37,8 @@ public function testConfigureEncoders(): void 'encoders' => [ JsonEncoder::class, new CsvEncoder(), - new Autowire(XmlEncoder::class) - ] + new Autowire(XmlEncoder::class), + ], ])); $this->assertCount(3, $registry->all()); @@ -49,7 +50,13 @@ public function testConfigureEncoders(): void public function testConfigureNormalizers(): void { $container = new Container(); - $container->bind(LoaderInterface::class, new AnnotationLoader(new AnnotationReader())); + + if (\class_exists(AttributeLoader::class)) { + $container->bind(LoaderInterface::class, new AttributeLoader()); + } elseif (\class_exists(AnnotationLoader::class)) { + $container->bind(LoaderInterface::class, new AnnotationLoader(new AnnotationReader())); + } + $container->bind(EnvironmentInterface::class, new Environment()); $bootloader = new SerializerBootloader($container); @@ -61,8 +68,8 @@ public function testConfigureNormalizers(): void 'normalizers' => [ UnwrappingDenormalizer::class, new UidNormalizer(), - new Autowire(JsonSerializableNormalizer::class) - ] + new Autowire(JsonSerializableNormalizer::class), + ], ]), $container); $this->assertCount(3, $registry->all());