From f2904a98ee8a4b383f65f1e8c348e28da8c0ad3f Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Sat, 7 Aug 2021 09:04:32 +0200 Subject: [PATCH 1/2] add support for jms/serializer-bundle 4.0 and the new decoration strategy --- .../HandlerRegistryDecorationPass.php | 2 +- .../JMSHandlerRegistryV4DecorationPass.php | 49 +++++++++++++++++++ .../Compiler/JMSHandlersPass.php | 7 ++- FOSRestBundle.php | 2 + composer.json | 2 +- 5 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 DependencyInjection/Compiler/JMSHandlerRegistryV4DecorationPass.php diff --git a/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php b/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php index 432e41438..6375b83e3 100644 --- a/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php +++ b/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php @@ -33,7 +33,7 @@ class HandlerRegistryDecorationPass implements CompilerPassInterface { public function process(ContainerBuilder $container): void { - if (!$container->has('fos_rest.serializer.jms_handler_registry')) { + if (!$container->has('fos_rest.serializer.jms_handler_registry') || $container->has('jms_serializer.handler_registry.service_locator')) { return; } diff --git a/DependencyInjection/Compiler/JMSHandlerRegistryV4DecorationPass.php b/DependencyInjection/Compiler/JMSHandlerRegistryV4DecorationPass.php new file mode 100644 index 000000000..70a3bdaa6 --- /dev/null +++ b/DependencyInjection/Compiler/JMSHandlerRegistryV4DecorationPass.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\RestBundle\DependencyInjection\Compiler; + +use FOS\RestBundle\Serializer\JMSHandlerRegistryV2; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Reference; + +/** + * Decorates the handler registry from JMSSerializerBundle. + * + * It works as HandlerRegistryDecorationPass but uses the symfony built-in decoration mechanism. + * This way of decoration is possible only starting from jms/serializer-bundle:4.0 . + * + * @author Asmir Mustafic + * + * @internal + */ +class JMSHandlerRegistryV4DecorationPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container): void + { + // skip if jms/serializer-bundle is not installed or < 4.0 + if (!$container->has('jms_serializer.handler_registry') || !$container->has('jms_serializer.handler_registry.service_locator')) { + return; + } + + $fosRestHandlerRegistry = new Definition( + JMSHandlerRegistryV2::class, + [ + new Reference('fos_rest.serializer.jms_handler_registry.inner'), + ] + ); + + $fosRestHandlerRegistry->setDecoratedService('jms_serializer.handler_registry'); + $container->setDefinition('fos_rest.serializer.jms_handler_registry', $fosRestHandlerRegistry); + } +} diff --git a/DependencyInjection/Compiler/JMSHandlersPass.php b/DependencyInjection/Compiler/JMSHandlersPass.php index 77bf8eafb..7e447ab23 100644 --- a/DependencyInjection/Compiler/JMSHandlersPass.php +++ b/DependencyInjection/Compiler/JMSHandlersPass.php @@ -27,8 +27,11 @@ final class JMSHandlersPass implements CompilerPassInterface public function process(ContainerBuilder $container): void { if ($container->has('jms_serializer.handler_registry')) { - // the public alias prevents the handler registry definition from being removed - $container->setAlias('fos_rest.serializer.jms_handler_registry', new Alias('jms_serializer.handler_registry', true)); + // perform the aliasing only when jms-serializer-bundle < 4.0 + if (!$container->has('jms_serializer.handler_registry.service_locator')) { + // the public alias prevents the handler registry definition from being removed + $container->setAlias('fos_rest.serializer.jms_handler_registry', new Alias('jms_serializer.handler_registry', true)); + } return; } diff --git a/FOSRestBundle.php b/FOSRestBundle.php index 5166a71ea..5583998e0 100644 --- a/FOSRestBundle.php +++ b/FOSRestBundle.php @@ -14,6 +14,7 @@ use FOS\RestBundle\DependencyInjection\Compiler\ConfigurationCheckPass; use FOS\RestBundle\DependencyInjection\Compiler\HandlerRegistryDecorationPass; use FOS\RestBundle\DependencyInjection\Compiler\JMSFormErrorHandlerPass; +use FOS\RestBundle\DependencyInjection\Compiler\JMSHandlerRegistryV4DecorationPass; use FOS\RestBundle\DependencyInjection\Compiler\JMSHandlersPass; use FOS\RestBundle\DependencyInjection\Compiler\FormatListenerRulesPass; use FOS\RestBundle\DependencyInjection\Compiler\SerializerConfigurationPass; @@ -39,6 +40,7 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new FormatListenerRulesPass()); $container->addCompilerPass(new JMSFormErrorHandlerPass()); $container->addCompilerPass(new JMSHandlersPass(), PassConfig::TYPE_BEFORE_REMOVING, -10); + $container->addCompilerPass(new JMSHandlerRegistryV4DecorationPass()); $container->addCompilerPass(new HandlerRegistryDecorationPass(), PassConfig::TYPE_AFTER_REMOVING); } } diff --git a/composer.json b/composer.json index 5b38e94e5..fe291dec4 100644 --- a/composer.json +++ b/composer.json @@ -57,7 +57,7 @@ "symfony/browser-kit": "^4.4|^5.0", "symfony/expression-language": "^4.4|^5.0", "symfony/css-selector": "^4.4|^5.0", - "jms/serializer-bundle": "^2.4.3|^3.0.1", + "jms/serializer-bundle": "^2.4.3|^3.0.1|^4.0", "jms/serializer": "^1.13|^2.0|^3.0", "psr/http-message": "^1.0", "friendsofphp/php-cs-fixer": "^3.0" From aa383604c6717c24bb9d787f3132edc4c2df1594 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Sat, 9 Oct 2021 23:25:57 +0200 Subject: [PATCH 2/2] do not decorate the jms handler registry for jms v4 --- .../HandlerRegistryDecorationPass.php | 1 + .../JMSHandlerRegistryV4DecorationPass.php | 49 ------------------- FOSRestBundle.php | 2 - Tests/Functional/DependencyInjectionTest.php | 5 ++ 4 files changed, 6 insertions(+), 51 deletions(-) delete mode 100644 DependencyInjection/Compiler/JMSHandlerRegistryV4DecorationPass.php diff --git a/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php b/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php index 6375b83e3..a2ca348fd 100644 --- a/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php +++ b/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php @@ -33,6 +33,7 @@ class HandlerRegistryDecorationPass implements CompilerPassInterface { public function process(ContainerBuilder $container): void { + // skip if JMSSerializerBundle is not installed or if JMSSerializerBundle >= 4.0 if (!$container->has('fos_rest.serializer.jms_handler_registry') || $container->has('jms_serializer.handler_registry.service_locator')) { return; } diff --git a/DependencyInjection/Compiler/JMSHandlerRegistryV4DecorationPass.php b/DependencyInjection/Compiler/JMSHandlerRegistryV4DecorationPass.php deleted file mode 100644 index 70a3bdaa6..000000000 --- a/DependencyInjection/Compiler/JMSHandlerRegistryV4DecorationPass.php +++ /dev/null @@ -1,49 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\RestBundle\DependencyInjection\Compiler; - -use FOS\RestBundle\Serializer\JMSHandlerRegistryV2; -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Definition; -use Symfony\Component\DependencyInjection\Reference; - -/** - * Decorates the handler registry from JMSSerializerBundle. - * - * It works as HandlerRegistryDecorationPass but uses the symfony built-in decoration mechanism. - * This way of decoration is possible only starting from jms/serializer-bundle:4.0 . - * - * @author Asmir Mustafic - * - * @internal - */ -class JMSHandlerRegistryV4DecorationPass implements CompilerPassInterface -{ - public function process(ContainerBuilder $container): void - { - // skip if jms/serializer-bundle is not installed or < 4.0 - if (!$container->has('jms_serializer.handler_registry') || !$container->has('jms_serializer.handler_registry.service_locator')) { - return; - } - - $fosRestHandlerRegistry = new Definition( - JMSHandlerRegistryV2::class, - [ - new Reference('fos_rest.serializer.jms_handler_registry.inner'), - ] - ); - - $fosRestHandlerRegistry->setDecoratedService('jms_serializer.handler_registry'); - $container->setDefinition('fos_rest.serializer.jms_handler_registry', $fosRestHandlerRegistry); - } -} diff --git a/FOSRestBundle.php b/FOSRestBundle.php index 5583998e0..5166a71ea 100644 --- a/FOSRestBundle.php +++ b/FOSRestBundle.php @@ -14,7 +14,6 @@ use FOS\RestBundle\DependencyInjection\Compiler\ConfigurationCheckPass; use FOS\RestBundle\DependencyInjection\Compiler\HandlerRegistryDecorationPass; use FOS\RestBundle\DependencyInjection\Compiler\JMSFormErrorHandlerPass; -use FOS\RestBundle\DependencyInjection\Compiler\JMSHandlerRegistryV4DecorationPass; use FOS\RestBundle\DependencyInjection\Compiler\JMSHandlersPass; use FOS\RestBundle\DependencyInjection\Compiler\FormatListenerRulesPass; use FOS\RestBundle\DependencyInjection\Compiler\SerializerConfigurationPass; @@ -40,7 +39,6 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new FormatListenerRulesPass()); $container->addCompilerPass(new JMSFormErrorHandlerPass()); $container->addCompilerPass(new JMSHandlersPass(), PassConfig::TYPE_BEFORE_REMOVING, -10); - $container->addCompilerPass(new JMSHandlerRegistryV4DecorationPass()); $container->addCompilerPass(new HandlerRegistryDecorationPass(), PassConfig::TYPE_AFTER_REMOVING); } } diff --git a/Tests/Functional/DependencyInjectionTest.php b/Tests/Functional/DependencyInjectionTest.php index f84bbb094..a7b435852 100644 --- a/Tests/Functional/DependencyInjectionTest.php +++ b/Tests/Functional/DependencyInjectionTest.php @@ -16,6 +16,7 @@ use FOS\RestBundle\Serializer\JMSHandlerRegistryV2; use FOS\RestBundle\Serializer\Normalizer\FormErrorHandler; use JMS\Serializer\Visitor\SerializationVisitorInterface; +use JMS\SerializerBundle\Debug\TraceableHandlerRegistry; use JMS\SerializerBundle\JMSSerializerBundle; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -33,6 +34,10 @@ public function testSerializerRelatedServicesAreNotRemovedWhenJmsSerializerBundl $this->assertInstanceOf(FormErrorHandler::class, $container->get('test.jms_serializer.form_error_handler')); + if (class_exists(TraceableHandlerRegistry::class)) { + $this->markTestIncomplete('Starting from jms/serializer-bundle 4.0 the handler registry is not decorated anymore'); + } + $this->assertInstanceOf( interface_exists(SerializationVisitorInterface::class) ? JMSHandlerRegistryV2::class : JMSHandlerRegistry::class, $container->get('test.jms_serializer.handler_registry')