Skip to content

Commit 83f0bff

Browse files
authored
Merge pull request #602 from schmittjoh/private-subscribing-handlers
Allow subscribing handlers to be private
2 parents 4748036 + f9f8420 commit 83f0bff

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

DependencyInjection/Compiler/CustomHandlersPass.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use JMS\Serializer\Handler\HandlerRegistry;
77
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\DependencyInjection\Reference;
910

1011
class CustomHandlersPass implements CompilerPassInterface
1112
{
@@ -29,7 +30,11 @@ public function process(ContainerBuilder $container)
2930

3031
foreach ($directions as $direction) {
3132
$method = isset($attrs['method']) ? $attrs['method'] : HandlerRegistry::getDefaultMethod($direction, $attrs['type'], $attrs['format']);
32-
$handlers[$direction][$attrs['type']][$attrs['format']] = array($id, $method);
33+
if ($container->getDefinition($id)->isPublic()) {
34+
$handlers[$direction][$attrs['type']][$attrs['format']] = array($id, $method);
35+
} else {
36+
$handlers[$direction][$attrs['type']][$attrs['format']] = array(new Reference($id), $method);
37+
}
3338
}
3439
}
3540
}
@@ -53,7 +58,11 @@ public function process(ContainerBuilder $container)
5358

5459
foreach ($directions as $direction) {
5560
$method = isset($methodData['method']) ? $methodData['method'] : HandlerRegistry::getDefaultMethod($direction, $methodData['type'], $methodData['format']);
56-
$handlers[$direction][$methodData['type']][$methodData['format']] = array($id, $method);
61+
if ($container->getDefinition($id)->isPublic()) {
62+
$handlers[$direction][$methodData['type']][$methodData['format']] = array($id, $method);
63+
} else {
64+
$handlers[$direction][$methodData['type']][$methodData['format']] = array(new Reference($id), $method);
65+
}
5766
}
5867
}
5968
}

Tests/DependencyInjection/CustomHandlerPassTest.php

+46-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass;
2828
use Symfony\Component\DependencyInjection\ContainerBuilder;
2929
use Symfony\Component\DependencyInjection\Definition;
30+
use Symfony\Component\DependencyInjection\Reference;
3031

3132
class CustomHandlerPassTest extends TestCase
3233
{
@@ -79,12 +80,35 @@ public function testHandler()
7980

8081
$args = $container->getDefinition('jms_serializer.handler_registry')->getArguments();
8182

82-
$this->assertEquals([
83+
$this->assertSame([
8384
2 => ['DateTime' => ['json' => ['my_service', 'deserializeDateTimeFromjson']]],
8485
1 => ['DateTime' => ['json' => ['my_service', 'serializeDateTimeTojson']]]
8586
], $args[1]);
8687
}
8788

89+
public function testHandlerCanBePrivate()
90+
{
91+
$container = $this->getContainer();
92+
93+
$def = new Definition('Foo');
94+
$def->setPublic(false);
95+
$def->addTag('jms_serializer.handler', [
96+
'type' => 'DateTime',
97+
'format' => 'json',
98+
]);
99+
$container->setDefinition('my_service', $def);
100+
101+
$pass = new CustomHandlersPass();
102+
$pass->process($container);
103+
104+
$args = $container->getDefinition('jms_serializer.handler_registry')->getArguments();
105+
106+
$this->assertEquals([
107+
2 => ['DateTime' => ['json' => [new Reference('my_service'), 'deserializeDateTimeFromjson']]],
108+
1 => ['DateTime' => ['json' => [new Reference('my_service'), 'serializeDateTimeTojson']]]
109+
], $args[1]);
110+
}
111+
88112
public function testHandlerDirection()
89113
{
90114
$container = $this->getContainer();
@@ -102,7 +126,7 @@ public function testHandlerDirection()
102126

103127
$args = $container->getDefinition('jms_serializer.handler_registry')->getArguments();
104128

105-
$this->assertEquals([
129+
$this->assertSame([
106130
1 => ['DateTime' => ['json' => ['my_service', 'serializeDateTimeTojson']]]
107131
], $args[1]);
108132
}
@@ -156,11 +180,30 @@ public function testSubscribingHandler()
156180

157181
$args = $container->getDefinition('jms_serializer.handler_registry')->getArguments();
158182

159-
$this->assertEquals([
183+
$this->assertSame([
160184
1 => ['DateTime' => ['json' => ['my_service', 'onDateTime']]]
161185
], $args[1]);
162186
}
163187

188+
public function testSubscribingHandlerCanBePrivate()
189+
{
190+
$container = $this->getContainer();
191+
192+
$def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SubscribingHandler');
193+
$def->addTag('jms_serializer.subscribing_handler');
194+
$def->setPublic(false);
195+
$container->setDefinition('my_service', $def);
196+
197+
$pass = new CustomHandlersPass();
198+
$pass->process($container);
199+
200+
$args = $container->getDefinition('jms_serializer.handler_registry')->getArguments();
201+
202+
$this->assertEquals([
203+
1 => ['DateTime' => ['json' => [new Reference('my_service'), 'onDateTime']]]
204+
], $args[1]);
205+
}
206+
164207
/**
165208
* @expectedException RuntimeException
166209
* @expectedExceptionMessage The service "my_service" must implement the SubscribingHandlerInterface

0 commit comments

Comments
 (0)