From d64d001ce34d6420f1cf6e54096e91cea358c89a Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Sat, 9 Oct 2021 23:25:57 +0200 Subject: [PATCH 1/7] do not decorate the jms handler registry for jms v4 --- .../Compiler/HandlerRegistryDecorationPass.php | 1 + Tests/Functional/DependencyInjectionTest.php | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php b/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php index 46040de62..196cd15bf 100644 --- a/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php +++ b/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php @@ -33,6 +33,7 @@ class HandlerRegistryDecorationPass implements CompilerPassInterface { public function process(ContainerBuilder $container) { + // skip if JMSSerializerBundle is not installed or if JMSSerializerBundle >= 4.0 if (!$container->has('fos_rest.serializer.jms_handler_registry')) { return; } diff --git a/Tests/Functional/DependencyInjectionTest.php b/Tests/Functional/DependencyInjectionTest.php index df1ef5f60..8a33faf12 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') From 387b4335418ab57a3b08c68c316f8a1472e88b9e Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Sat, 7 Aug 2021 09:04:32 +0200 Subject: [PATCH 2/7] 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 196cd15bf..b96be3f76 100644 --- a/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php +++ b/DependencyInjection/Compiler/HandlerRegistryDecorationPass.php @@ -34,7 +34,7 @@ class HandlerRegistryDecorationPass implements CompilerPassInterface public function process(ContainerBuilder $container) { // skip if JMSSerializerBundle is not installed or if JMSSerializerBundle >= 4.0 - 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 dc948a948..200257b4b 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) { 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 db23512b7..0678b1630 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; @@ -42,6 +43,7 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new TwigExceptionPass(), PassConfig::TYPE_BEFORE_REMOVING, -10); $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 e0d04e4d3..c208ada49 100644 --- a/composer.json +++ b/composer.json @@ -61,7 +61,7 @@ "symfony/css-selector": "^3.4|^4.3", "symfony/templating": "^3.4|^4.3", "phpoption/phpoption": "^1.1", - "jms/serializer-bundle": "^2.3.1|^3.0", + "jms/serializer-bundle": "^2.3.1|^3.0|^4.0", "jms/serializer": "^1.13|^2.0|^3.0", "psr/http-message": "^1.0", "friendsofphp/php-cs-fixer": "^2.0" From 2b275cc9450628b1d6c4dfc69208f341c1278ea0 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Fri, 27 Jan 2023 00:38:50 +0100 Subject: [PATCH 3/7] Allow JMS bundle 5 too --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c208ada49..d72d6d2bb 100644 --- a/composer.json +++ b/composer.json @@ -61,7 +61,7 @@ "symfony/css-selector": "^3.4|^4.3", "symfony/templating": "^3.4|^4.3", "phpoption/phpoption": "^1.1", - "jms/serializer-bundle": "^2.3.1|^3.0|^4.0", + "jms/serializer-bundle": "^2.3.1|^3.0|^4.0|^5.0", "jms/serializer": "^1.13|^2.0|^3.0", "psr/http-message": "^1.0", "friendsofphp/php-cs-fixer": "^2.0" From ad14507ea1addbf1770d4048cfe0890c25bccd23 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Fri, 27 Jan 2023 08:48:51 +0100 Subject: [PATCH 4/7] Cherry pick CI upgrades from 3.x --- .github/workflows/continuous-integration.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 9bc4350e5..aa77aab80 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -37,7 +37,7 @@ jobs: steps: - name: "Checkout" - uses: "actions/checkout@v2" + uses: "actions/checkout@v3" - name: "Install PHP with XDebug" uses: "shivammathur/setup-php@v2" @@ -45,6 +45,7 @@ jobs: with: php-version: "${{ matrix.php-version }}" coverage: "xdebug" + tools: "composer:v2,flex" - name: "Install PHP without coverage" uses: "shivammathur/setup-php@v2" @@ -52,9 +53,10 @@ jobs: with: php-version: "${{ matrix.php-version }}" coverage: "none" + tools: "composer:v2,flex" - name: "Cache dependencies installed with composer" - uses: "actions/cache@v2" + uses: "actions/cache@v3" with: path: "~/.composer/cache" key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}" @@ -64,7 +66,7 @@ jobs: env: SYMFONY_REQUIRE: "${{ matrix.symfony-require }}" run: | - composer global require --no-progress --no-scripts --no-plugins symfony/flex + composer remove friendsofphp/php-cs-fixer --dev --no-update composer update --no-interaction --no-progress ${{ matrix.composer-flags }} - name: "Run PHPUnit" From 98cc2b93b0ad82a3eae6ac597ec616d632fea91d Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Tue, 7 Feb 2023 11:43:22 +0100 Subject: [PATCH 5/7] Remove JMSHandlerRegistryV4DecorationPass.php --- .../JMSHandlerRegistryV4DecorationPass.php | 49 ------------------- 1 file changed, 49 deletions(-) delete mode 100644 DependencyInjection/Compiler/JMSHandlerRegistryV4DecorationPass.php 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); - } -} From 07d717a31b7f37df972067055b2b4b67b3be1c03 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Tue, 7 Feb 2023 11:46:40 +0100 Subject: [PATCH 6/7] Remove JMSHandlerRegistryV4DecorationPass.php from bundle configuration too --- FOSRestBundle.php | 1 - 1 file changed, 1 deletion(-) diff --git a/FOSRestBundle.php b/FOSRestBundle.php index 0678b1630..2da6996cd 100644 --- a/FOSRestBundle.php +++ b/FOSRestBundle.php @@ -43,7 +43,6 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new TwigExceptionPass(), PassConfig::TYPE_BEFORE_REMOVING, -10); $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); } } From 420ff622b1ada4319d9cba2043127c0bc021d263 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Tue, 7 Feb 2023 11:50:18 +0100 Subject: [PATCH 7/7] Fix CS --- FOSRestBundle.php | 1 - 1 file changed, 1 deletion(-) diff --git a/FOSRestBundle.php b/FOSRestBundle.php index 2da6996cd..db23512b7 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;