Skip to content

Commit

Permalink
bug #1873 manually decorate the core JMS handler registry (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.3-dev branch.

Discussion
----------

manually decorate the core JMS handler registry

This works around the fact that custom handlers are processed in
JMSSerializerBundle after Symfony's DecoratorServicePass has been
executed (which means that we cannot use proper service decoration).

The old code still used to work on Symfony 2.x applications where
FOSRestBundle was registered after JMSSerializerBundle. In those cases,
custom handlers were processed before the handler registry was replaced
by the compiler pass coming from FOSRestBundle.

More recent Symfony applications have not been affected by this bug as
the compiler pass priority would have ensured the correct order in which
the passes would have been executed.

Commits
-------

17c8de8 manually decorate the core JMS handler registry
  • Loading branch information
xabbuh committed Feb 27, 2018
2 parents c7fd5d6 + 17c8de8 commit 120ede1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
52 changes: 52 additions & 0 deletions DependencyInjection/Compiler/HandlerRegistryDecorationPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\JMSHandlerRegistry;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Decorates the handler registry from JMSSerializerBundle.
*
* The logic is borrowed from the core Symfony DecoratorServicePass, but is implemented here to respect the fact that
* custom handlers are registered in JMSSerializerBundle in a compiler pass that is executed after decorated services
* have been resolved.
*
* @author Christian Flothmann <[email protected]>
*/
class HandlerRegistryDecorationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->has('fos_rest.serializer.jms_handler_registry')) {
return;
}

$jmsHandlerRegistry = $container->findDefinition('fos_rest.serializer.jms_handler_registry');
$public = $jmsHandlerRegistry->isPublic();
$jmsHandlerRegistry->setPublic(false);
$container->setDefinition('fos_rest.serializer.jms_handler_registry.inner', $jmsHandlerRegistry);

$fosRestHandlerRegistry = $container->register('jms_serializer.handler_registry', JMSHandlerRegistry::class)
->setPublic($public)
->addArgument(new Reference('fos_rest.serializer.jms_handler_registry.inner'));

// remap existing aliases (they have already been replaced with the actual definition by Symfony's ReplaceAliasByActualDefinitonPass)
foreach ($container->getDefinitions() as $id => $definition) {
if ('fos_rest.serializer.jms_handler_registry.inner' !== $id && $definition === $jmsHandlerRegistry) {
$container->setDefinition($id, $fosRestHandlerRegistry);
}
}
}
}
21 changes: 3 additions & 18 deletions DependencyInjection/Compiler/JMSHandlersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

namespace FOS\RestBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

Expand All @@ -28,7 +27,8 @@ final class JMSHandlersPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
if ($container->has('jms_serializer.handler_registry')) {
$this->registerHandlerRegistry($container);
// 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 All @@ -37,19 +37,4 @@ public function process(ContainerBuilder $container)
$container->removeDefinition('fos_rest.serializer.exception_normalizer.jms');
$container->getParameterBag()->remove('jms_serializer.form_error_handler.class');
}

/**
* Inlined because {@link JMS\SerializerBundle\DependencyInjection\Compiler\CustomHandlersPass}
* must be executed before replacing jms_serializer.handler_registry.
*/
public function registerHandlerRegistry(ContainerBuilder $container)
{
$handlerRegistry = new Definition('FOS\RestBundle\Serializer\JMSHandlerRegistry', array(new Reference('fos_rest.serializer.jms_handler_registry')));

$oldRegistry = $container->getDefinition('jms_serializer.handler_registry');
$oldRegistry->setPublic(false);

$container->setDefinition('jms_serializer.handler_registry', $handlerRegistry);
$container->setDefinition('fos_rest.serializer.jms_handler_registry', $oldRegistry);
}
}
2 changes: 2 additions & 0 deletions FOSRestBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FOS\RestBundle;

use FOS\RestBundle\DependencyInjection\Compiler\ConfigurationCheckPass;
use FOS\RestBundle\DependencyInjection\Compiler\HandlerRegistryDecorationPass;
use FOS\RestBundle\DependencyInjection\Compiler\JMSFormErrorHandlerPass;
use FOS\RestBundle\DependencyInjection\Compiler\JMSHandlersPass;
use FOS\RestBundle\DependencyInjection\Compiler\FormatListenerRulesPass;
Expand Down Expand Up @@ -40,5 +41,6 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new TwigExceptionPass());
$container->addCompilerPass(new JMSFormErrorHandlerPass());
$container->addCompilerPass(new JMSHandlersPass(), PassConfig::TYPE_BEFORE_REMOVING, -10);
$container->addCompilerPass(new HandlerRegistryDecorationPass(), PassConfig::TYPE_AFTER_REMOVING);
}
}

0 comments on commit 120ede1

Please sign in to comment.