diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 2d88ce1a..fa883f65 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -56,6 +56,7 @@ public function getConfigTreeBuilder() private function addConfigNodes($root): void { + $root->scalarNode('enum_support')->defaultValue(false)->end(); $this->addHandlersSection($root); $this->addSubscribersSection($root); $this->addObjectConstructorsSection($root); diff --git a/DependencyInjection/JMSSerializerExtension.php b/DependencyInjection/JMSSerializerExtension.php index f5401d32..339e17b1 100644 --- a/DependencyInjection/JMSSerializerExtension.php +++ b/DependencyInjection/JMSSerializerExtension.php @@ -11,7 +11,6 @@ use JMS\Serializer\Handler\SymfonyUidHandler; use JMS\Serializer\Metadata\Driver\AttributeDriver\AttributeReader; use JMS\Serializer\Metadata\Driver\DocBlockDriver; -use JMS\Serializer\Metadata\Driver\TypedPropertiesDriver; use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Alias; @@ -201,8 +200,8 @@ private function loadInternal(array $config, ScopedContainer $container, array $ $container->removeDefinition('jms_serializer.metadata.doc_block_driver'); } - // enable the typed props reader on php 7.4 - if (PHP_VERSION_ID >= 70400 && class_exists(TypedPropertiesDriver::class)) { + // enable the typed props reader on php 7.4+ + if (PHP_VERSION_ID >= 70400) { $container->getDefinition('jms_serializer.metadata.typed_properties_driver') ->setDecoratedService('jms_serializer.metadata_driver') ->setPublic(false); @@ -210,6 +209,16 @@ private function loadInternal(array $config, ScopedContainer $container, array $ $container->removeDefinition('jms_serializer.metadata.typed_properties_driver'); } + if ($config['enum_support']) { + $container->getDefinition('jms_serializer.metadata.enum_driver') + ->setDecoratedService('jms_serializer.metadata_driver') + ->setPublic(false); + } else { + $container->removeDefinition('jms_serializer.metadata.enum_driver'); + $container->removeDefinition('jms_serializer.enum_handler'); + $container->removeDefinition('jms_serializer.enum_subscriber'); + } + // enable the attribute reader on php 8 if (PHP_VERSION_ID >= 80000 && class_exists(AttributeReader::class)) { $container->register('jms_serializer.metadata.annotation_and_attributes_reader', AttributeReader::class) diff --git a/Resources/config/services.xml b/Resources/config/services.xml index a5b76e99..60cbb2ce 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -35,6 +35,10 @@ jms_serializer + + + + @@ -53,6 +57,10 @@ + + + + false @@ -128,6 +136,11 @@ + + + + + diff --git a/Resources/doc/configuration.rst b/Resources/doc/configuration.rst index 726d4755..7bb5546a 100644 --- a/Resources/doc/configuration.rst +++ b/Resources/doc/configuration.rst @@ -195,6 +195,7 @@ values: # config.yml jms_serializer: profiler: %kernel.debug% + enum_support: true # BHP 8.1 Enums support, false by default for backward compatibility twig_enabled: 'default' # on which instance is twig enabled handlers: datetime: diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 3f2e634d..55e9fcf9 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -25,7 +25,7 @@ private function getContainer(array $configs = []) $container->setParameter('kernel.bundles', $bundles); $container->setParameter('kernel.bundles_metadata', array_map(static function (string $class): array { return [ - 'path' => (new $class)->getPath(), + 'path' => (new $class())->getPath(), 'namespace' => (new \ReflectionClass($class))->getNamespaceName(), ]; }, $bundles)); diff --git a/Tests/DependencyInjection/Fixture/ObjectUsingEnum.php b/Tests/DependencyInjection/Fixture/ObjectUsingEnum.php new file mode 100644 index 00000000..87cc3245 --- /dev/null +++ b/Tests/DependencyInjection/Fixture/ObjectUsingEnum.php @@ -0,0 +1,25 @@ +one = Card::Black; + $this->two = Card::Red; + $this->three = [Card::Red, Card::Black]; + } +} + +enum Card +{ + case Black; + case Red; +} diff --git a/Tests/DependencyInjection/JMSSerializerExtensionTest.php b/Tests/DependencyInjection/JMSSerializerExtensionTest.php index c980520d..bd9333a6 100644 --- a/Tests/DependencyInjection/JMSSerializerExtensionTest.php +++ b/Tests/DependencyInjection/JMSSerializerExtensionTest.php @@ -16,6 +16,7 @@ use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\CastDateToIntEventSubscriber; use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\IncludeInterfaces\AnInterfaceImplementation; use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\IncludeInterfaces\AnObject; +use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnum; use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingExpressionLanguage; use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingExpressionProperties; use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SimpleObject; @@ -579,6 +580,34 @@ public function testEmptyJsonVisitorOptions() ]); } + public function testEnumSupportCanBeEnabled() + { + if (PHP_VERSION_ID < 80100) { + $this->markTestSkipped('Enum is available only on PHP 8.1+'); + } + + $container = $this->getContainerForConfig([ + ['enum_support' => true], + ]); + $serializer = $container->get('jms_serializer'); + + $object = new ObjectUsingEnum(); + $this->assertEquals('{"one":"Black","two":"Red","three":["Red","Black"]}', $serializer->serialize($object, 'json')); + } + + public function testEnumSupportIsDisabledByDefault() + { + if (PHP_VERSION_ID < 80100) { + $this->markTestSkipped('Enum is available only on PHP 8.1+'); + } + + $container = $this->getContainerForConfig([[]]); + $serializer = $container->get('jms_serializer'); + + $object = new ObjectUsingEnum(); + $this->assertEquals('{"one":{"name":"Black"},"two":{"name":"Red"},"three":[{"name":"Red"},{"name":"Black"}]}', $serializer->serialize($object, 'json')); + } + public function testExpressionLanguage() { if (!interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) { diff --git a/Tests/DependencyInjection/TwigExtensionPassTest.php b/Tests/DependencyInjection/TwigExtensionPassTest.php index 98f3cf0c..01d1c945 100644 --- a/Tests/DependencyInjection/TwigExtensionPassTest.php +++ b/Tests/DependencyInjection/TwigExtensionPassTest.php @@ -25,7 +25,7 @@ private function getContainer(array $bundles = ['TwigBundle' => TwigBundle::clas $container->setParameter('kernel.bundles', $bundles); $container->setParameter('kernel.bundles_metadata', array_map(static function (string $class): array { return [ - 'path' => (new $class)->getPath(), + 'path' => (new $class())->getPath(), 'namespace' => (new \ReflectionClass($class))->getNamespaceName(), ]; }, $bundles)); diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php deleted file mode 100644 index 751fa06c..00000000 --- a/Tests/bootstrap.php +++ /dev/null @@ -1,15 +0,0 @@ -