Skip to content

Commit

Permalink
use symfony decoration for jms serializer handler registry
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Aug 8, 2021
1 parent b41c853 commit a074bb8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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): 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;
}
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 All @@ -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);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a074bb8

Please sign in to comment.