Skip to content

Commit 9a9b31c

Browse files
committed
Replicate #13293
1 parent 0bee27f commit 9a9b31c

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

src/Symfony/Component/EventDispatcher/DependencyInjection/CompiledRegisterListenersPass.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,12 @@ public function process(ContainerBuilder $container)
102102
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
103103
}
104104

105+
if ($def->isAbstract()) {
106+
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
107+
}
108+
105109
// We must assume that the class value has been correctly filled, even if the service is created by a factory
106-
$class = $def->getClass();
110+
$class = $container->getParameterBag()->resolveValue($def->getClass());
107111

108112
$refClass = new \ReflectionClass($class);
109113
$interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';

src/Symfony/Component/EventDispatcher/Tests/DependencyInjection/CompiledRegisterListenersPassTest.php

+85
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,91 @@ public function testAbstractEventListener()
139139
$registerListenersPass = new CompiledRegisterListenersPass();
140140
$registerListenersPass->process($container);
141141
}
142+
143+
/**
144+
* @expectedException \InvalidArgumentException
145+
* @expectedExceptionMessage The service "foo" must not be abstract as event subscribers are lazy-loaded.
146+
*/
147+
public function testAbstractEventSubscriber()
148+
{
149+
$container = new ContainerBuilder();
150+
$container->register('foo', 'stdClass')->setAbstract(true)->addTag('kernel.event_subscriber', array());
151+
$container->register('event_dispatcher', 'stdClass');
152+
153+
$registerListenersPass = new CompiledRegisterListenersPass();
154+
$registerListenersPass->process($container);
155+
}
156+
157+
public function testEventSubscriberResolvableClassName()
158+
{
159+
$container = new ContainerBuilder();
160+
161+
$container->setParameter('subscriber.class', 'Symfony\Component\EventDispatcher\Tests\DependencyInjection\CompiledSubscriberService');
162+
$container->register('foo', '%subscriber.class%')->addTag('kernel.event_subscriber', array());
163+
$container->register('event_dispatcher', 'stdClass');
164+
165+
$registerListenersPass = new CompiledRegisterListenersPass();
166+
$registerListenersPass->process($container);
167+
168+
$definition = $container->getDefinition('event_dispatcher');
169+
$expected_arguments = array(
170+
array (
171+
'test_event.multiple_listeners' => array (
172+
128 => array (
173+
array (
174+
'service' => array (
175+
'id' => 'foo',
176+
'method' => 'methodWithHighestPriority',
177+
),
178+
),
179+
),
180+
0 => array (
181+
array (
182+
'service' => array (
183+
'id' => 'foo',
184+
'method' => 'methodWithoutPriority',
185+
),
186+
),
187+
),
188+
),
189+
'test_event.single_listener_with_priority' => array (
190+
64 => array (
191+
array (
192+
'service' => array (
193+
'id' => 'foo',
194+
'method' => 'methodWithHighPriority',
195+
),
196+
),
197+
),
198+
),
199+
'test_event.single_listener_without_priority' => array (
200+
0 => array (
201+
array (
202+
'service' => array (
203+
'id' => 'foo',
204+
'method' => 'methodWithoutPriority',
205+
),
206+
),
207+
),
208+
),
209+
),
210+
);
211+
$this->assertSame($expected_arguments, $definition->getArguments());
212+
}
213+
214+
/**
215+
* @expectedException \InvalidArgumentException
216+
* @expectedExceptionMessage You have requested a non-existent parameter "subscriber.class"
217+
*/
218+
public function testEventSubscriberUnresolvableClassName()
219+
{
220+
$container = new ContainerBuilder();
221+
$container->register('foo', '%subscriber.class%')->addTag('kernel.event_subscriber', array());
222+
$container->register('event_dispatcher', 'stdClass');
223+
224+
$registerListenersPass = new CompiledRegisterListenersPass();
225+
$registerListenersPass->process($container);
226+
}
142227
}
143228

144229
class CompiledSubscriberService implements EventSubscriberInterface

0 commit comments

Comments
 (0)