Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport #2319 - Compat with JMS Serializer bundle 4+ #2383

Open
wants to merge 7 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,26 @@ jobs:

steps:
- name: "Checkout"
uses: "actions/checkout@v2"
uses: "actions/checkout@v3"

- name: "Install PHP with XDebug"
uses: "shivammathur/setup-php@v2"
if: "${{ matrix.coverage != '' }}"
with:
php-version: "${{ matrix.php-version }}"
coverage: "xdebug"
tools: "composer:v2,flex"

- name: "Install PHP without coverage"
uses: "shivammathur/setup-php@v2"
if: "${{ matrix.coverage == '' }}"
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') }}"
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class HandlerRegistryDecorationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->has('fos_rest.serializer.jms_handler_registry')) {
// 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* 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 <[email protected]>
*
* @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);
}
}
7 changes: 5 additions & 2 deletions DependencyInjection/Compiler/JMSHandlersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions FOSRestBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions Tests/Functional/DependencyInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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|^5.0",
"jms/serializer": "^1.13|^2.0|^3.0",
"psr/http-message": "^1.0",
"friendsofphp/php-cs-fixer": "^2.0"
Expand Down