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

manually decorate the core JMS handler registry #1873

Merged
merged 1 commit into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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.
  • Loading branch information
xabbuh committed Feb 27, 2018
commit 17c8de82fe118961b5b71d06e1a040945f303440
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 <christian.flothmann@sensiolabs.de>
*/
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);
}
}