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 ae6c79e82..b7ad05761 100644 --- a/composer.json +++ b/composer.json @@ -57,7 +57,7 @@ "symfony/expression-language": "^4.4|^5.0", "symfony/css-selector": "^4.4|^5.0", "phpoption/phpoption": "^1.1", - "jms/serializer-bundle": "^2.4.3|^3.0.1", + "jms/serializer-bundle": "dev-di", "jms/serializer": "^1.13|^2.0|^3.0", "psr/http-message": "^1.0", "friendsofphp/php-cs-fixer": "^2.0"