Skip to content

Commit a074bb8

Browse files
committed
use symfony decoration for jms serializer handler registry
1 parent b41c853 commit a074bb8

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

DependencyInjection/Compiler/HandlerRegistryDecorationPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class HandlerRegistryDecorationPass implements CompilerPassInterface
3333
{
3434
public function process(ContainerBuilder $container): void
3535
{
36-
if (!$container->has('fos_rest.serializer.jms_handler_registry')) {
36+
if (!$container->has('fos_rest.serializer.jms_handler_registry') || $container->has('jms_serializer.handler_registry.service_locator')) {
3737
return;
3838
}
3939

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\JMSHandlerRegistryV2;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
20+
/**
21+
* Decorates the handler registry from JMSSerializerBundle.
22+
*
23+
* It works as HandlerRegistryDecorationPass but uses the symfony built-in decoration mechanism.
24+
* This way of decoration is possible only starting from jms/serializer-bundle:4.0 .
25+
*
26+
* @author Asmir Mustafic <[email protected]>
27+
*
28+
* @internal
29+
*/
30+
class JMSHandlerRegistryV4DecorationPass implements CompilerPassInterface
31+
{
32+
public function process(ContainerBuilder $container): void
33+
{
34+
// skip if jms/serializer-bundle is not installed or < 4.0
35+
if (!$container->has('jms_serializer.handler_registry') || !$container->has('jms_serializer.handler_registry.service_locator')) {
36+
return;
37+
}
38+
39+
$fosRestHandlerRegistry = new Definition(
40+
JMSHandlerRegistryV2::class,
41+
[
42+
new Reference('fos_rest.serializer.jms_handler_registry.inner'),
43+
]
44+
);
45+
46+
$fosRestHandlerRegistry->setDecoratedService('jms_serializer.handler_registry');
47+
$container->setDefinition('fos_rest.serializer.jms_handler_registry', $fosRestHandlerRegistry);
48+
}
49+
}

DependencyInjection/Compiler/JMSHandlersPass.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ final class JMSHandlersPass implements CompilerPassInterface
2727
public function process(ContainerBuilder $container): void
2828
{
2929
if ($container->has('jms_serializer.handler_registry')) {
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));
30+
// perform the aliasing only when jms-serializer-bundle < 4.0
31+
if (!$container->has('jms_serializer.handler_registry.service_locator')) {
32+
// the public alias prevents the handler registry definition from being removed
33+
$container->setAlias('fos_rest.serializer.jms_handler_registry', new Alias('jms_serializer.handler_registry', true));
34+
}
3235

3336
return;
3437
}

FOSRestBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use FOS\RestBundle\DependencyInjection\Compiler\ConfigurationCheckPass;
1515
use FOS\RestBundle\DependencyInjection\Compiler\HandlerRegistryDecorationPass;
1616
use FOS\RestBundle\DependencyInjection\Compiler\JMSFormErrorHandlerPass;
17+
use FOS\RestBundle\DependencyInjection\Compiler\JMSHandlerRegistryV4DecorationPass;
1718
use FOS\RestBundle\DependencyInjection\Compiler\JMSHandlersPass;
1819
use FOS\RestBundle\DependencyInjection\Compiler\FormatListenerRulesPass;
1920
use FOS\RestBundle\DependencyInjection\Compiler\SerializerConfigurationPass;
@@ -39,6 +40,7 @@ public function build(ContainerBuilder $container)
3940
$container->addCompilerPass(new FormatListenerRulesPass());
4041
$container->addCompilerPass(new JMSFormErrorHandlerPass());
4142
$container->addCompilerPass(new JMSHandlersPass(), PassConfig::TYPE_BEFORE_REMOVING, -10);
43+
$container->addCompilerPass(new JMSHandlerRegistryV4DecorationPass());
4244
$container->addCompilerPass(new HandlerRegistryDecorationPass(), PassConfig::TYPE_AFTER_REMOVING);
4345
}
4446
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"symfony/expression-language": "^4.4|^5.0",
5858
"symfony/css-selector": "^4.4|^5.0",
5959
"phpoption/phpoption": "^1.1",
60-
"jms/serializer-bundle": "^2.4.3|^3.0.1",
60+
"jms/serializer-bundle": "dev-di",
6161
"jms/serializer": "^1.13|^2.0|^3.0",
6262
"psr/http-message": "^1.0",
6363
"friendsofphp/php-cs-fixer": "^2.0"

0 commit comments

Comments
 (0)