Skip to content

Commit

Permalink
Merge pull request #915 from schmittjoh/enable-enum
Browse files Browse the repository at this point in the history
Allows to enable enum support
  • Loading branch information
goetas authored Jan 9, 2023
2 parents 437d5e6 + 8154295 commit cdc876a
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 22 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions DependencyInjection/JMSSerializerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -201,15 +200,25 @@ 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);
} else {
$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)
Expand Down
13 changes: 13 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<argument>jms_serializer</argument>
</service>

<service id="jms_serializer.enum_subscriber" class="JMS\Serializer\EventDispatcher\Subscriber\EnumSubscriber" public="false">
<tag name="jms_serializer.event_subscriber" />
</service>

<!-- Handlers -->
<service id="jms_serializer.handler_registry" class="JMS\Serializer\Handler\LazyHandlerRegistry">
<argument type="service" id="jms_serializer.handler_registry.service_locator" />
Expand All @@ -53,6 +57,10 @@
<tag name="container.service_locator" />
</service>

<service id="jms_serializer.enum_handler" class="JMS\Serializer\Handler\EnumHandler" public="false">
<tag name="jms_serializer.subscribing_handler" />
</service>

<service id="jms_serializer.array_collection_handler" class="JMS\Serializer\Handler\ArrayCollectionHandler" public="false">
<argument type="constant">false</argument>
<tag name="jms_serializer.subscribing_handler" />
Expand Down Expand Up @@ -128,6 +136,11 @@
<argument id="jms_serializer.metadata.typed_properties_driver.inner" type="service"/>
<argument type="service" id="jms_serializer.type_parser" on-invalid="null" />
</service>

<service id="jms_serializer.metadata.enum_driver" class="JMS\Serializer\Metadata\Driver\EnumPropertiesDriver" public="false">
<argument id="jms_serializer.metadata.enum_driver.inner" type="service"/>
</service>

<service id="jms_serializer.metadata.doc_block_driver" class="JMS\Serializer\Metadata\Driver\DocBlockDriver" public="false">
<argument id="jms_serializer.metadata.doc_block_driver.inner" type="service"/>
<argument type="service" id="jms_serializer.type_parser" on-invalid="null" />
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
25 changes: 25 additions & 0 deletions Tests/DependencyInjection/Fixture/ObjectUsingEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace JMS\SerializerBundle\Tests\DependencyInjection\Fixture;

class ObjectUsingEnum
{
private Card $one;
private $two;
private array $three;

public function __construct()
{
$this->one = Card::Black;
$this->two = Card::Red;
$this->three = [Card::Red, Card::Black];
}
}

enum Card
{
case Black;
case Red;
}
29 changes: 29 additions & 0 deletions Tests/DependencyInjection/JMSSerializerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Expand Down
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/TwigExtensionPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
15 changes: 0 additions & 15 deletions Tests/bootstrap.php

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"require": {
"php": "^7.2 || ^8.0",
"jms/serializer": "^3.18",
"jms/serializer": "^3.20",
"jms/metadata": "^2.5",
"symfony/dependency-injection": "^3.4 || ^4.0 || ^5.0 || ^6.0",
"symfony/framework-bundle": "^3.4 || ^4.0 || ^5.0 || ^6.0"
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
processIsolation="false"
stopOnFailure="false"
colors="true"
bootstrap="Tests/bootstrap.php"
bootstrap="vendor/autoload.php"
>

<testsuites>
Expand Down

0 comments on commit cdc876a

Please sign in to comment.