Skip to content

Commit 120ede1

Browse files
committed
bug #1873 manually decorate the core JMS handler registry (xabbuh)
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
2 parents c7fd5d6 + 17c8de8 commit 120ede1

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\DependencyInjection\Compiler;
13+
14+
use FOS\RestBundle\Serializer\JMSHandlerRegistry;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
/**
20+
* Decorates the handler registry from JMSSerializerBundle.
21+
*
22+
* The logic is borrowed from the core Symfony DecoratorServicePass, but is implemented here to respect the fact that
23+
* custom handlers are registered in JMSSerializerBundle in a compiler pass that is executed after decorated services
24+
* have been resolved.
25+
*
26+
* @author Christian Flothmann <[email protected]>
27+
*/
28+
class HandlerRegistryDecorationPass implements CompilerPassInterface
29+
{
30+
public function process(ContainerBuilder $container)
31+
{
32+
if (!$container->has('fos_rest.serializer.jms_handler_registry')) {
33+
return;
34+
}
35+
36+
$jmsHandlerRegistry = $container->findDefinition('fos_rest.serializer.jms_handler_registry');
37+
$public = $jmsHandlerRegistry->isPublic();
38+
$jmsHandlerRegistry->setPublic(false);
39+
$container->setDefinition('fos_rest.serializer.jms_handler_registry.inner', $jmsHandlerRegistry);
40+
41+
$fosRestHandlerRegistry = $container->register('jms_serializer.handler_registry', JMSHandlerRegistry::class)
42+
->setPublic($public)
43+
->addArgument(new Reference('fos_rest.serializer.jms_handler_registry.inner'));
44+
45+
// remap existing aliases (they have already been replaced with the actual definition by Symfony's ReplaceAliasByActualDefinitonPass)
46+
foreach ($container->getDefinitions() as $id => $definition) {
47+
if ('fos_rest.serializer.jms_handler_registry.inner' !== $id && $definition === $jmsHandlerRegistry) {
48+
$container->setDefinition($id, $fosRestHandlerRegistry);
49+
}
50+
}
51+
}
52+
}

DependencyInjection/Compiler/JMSHandlersPass.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
namespace FOS\RestBundle\DependencyInjection\Compiler;
1313

14-
use Symfony\Component\DependencyInjection\Reference;
15-
use Symfony\Component\DependencyInjection\Definition;
14+
use Symfony\Component\DependencyInjection\Alias;
1615
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1716
use Symfony\Component\DependencyInjection\ContainerBuilder;
1817

@@ -28,7 +27,8 @@ final class JMSHandlersPass implements CompilerPassInterface
2827
public function process(ContainerBuilder $container)
2928
{
3029
if ($container->has('jms_serializer.handler_registry')) {
31-
$this->registerHandlerRegistry($container);
30+
// the public alias prevents the handler registry definition from being removed
31+
$container->setAlias('fos_rest.serializer.jms_handler_registry', new Alias('jms_serializer.handler_registry', true));
3232

3333
return;
3434
}
@@ -37,19 +37,4 @@ public function process(ContainerBuilder $container)
3737
$container->removeDefinition('fos_rest.serializer.exception_normalizer.jms');
3838
$container->getParameterBag()->remove('jms_serializer.form_error_handler.class');
3939
}
40-
41-
/**
42-
* Inlined because {@link JMS\SerializerBundle\DependencyInjection\Compiler\CustomHandlersPass}
43-
* must be executed before replacing jms_serializer.handler_registry.
44-
*/
45-
public function registerHandlerRegistry(ContainerBuilder $container)
46-
{
47-
$handlerRegistry = new Definition('FOS\RestBundle\Serializer\JMSHandlerRegistry', array(new Reference('fos_rest.serializer.jms_handler_registry')));
48-
49-
$oldRegistry = $container->getDefinition('jms_serializer.handler_registry');
50-
$oldRegistry->setPublic(false);
51-
52-
$container->setDefinition('jms_serializer.handler_registry', $handlerRegistry);
53-
$container->setDefinition('fos_rest.serializer.jms_handler_registry', $oldRegistry);
54-
}
5540
}

FOSRestBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace FOS\RestBundle;
1313

1414
use FOS\RestBundle\DependencyInjection\Compiler\ConfigurationCheckPass;
15+
use FOS\RestBundle\DependencyInjection\Compiler\HandlerRegistryDecorationPass;
1516
use FOS\RestBundle\DependencyInjection\Compiler\JMSFormErrorHandlerPass;
1617
use FOS\RestBundle\DependencyInjection\Compiler\JMSHandlersPass;
1718
use FOS\RestBundle\DependencyInjection\Compiler\FormatListenerRulesPass;
@@ -40,5 +41,6 @@ public function build(ContainerBuilder $container)
4041
$container->addCompilerPass(new TwigExceptionPass());
4142
$container->addCompilerPass(new JMSFormErrorHandlerPass());
4243
$container->addCompilerPass(new JMSHandlersPass(), PassConfig::TYPE_BEFORE_REMOVING, -10);
44+
$container->addCompilerPass(new HandlerRegistryDecorationPass(), PassConfig::TYPE_AFTER_REMOVING);
4345
}
4446
}

0 commit comments

Comments
 (0)