8000 [EventDispatcher] Add missing checks to RegisterListenersPass · symfony/symfony@3ea8580 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ea8580

Browse files
committed
[EventDispatcher] Add missing checks to RegisterListenersPass
* Support services using a parameter for their class name. * Prevent abstract services as event subscribers. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR |
1 parent 31ad8c9 commit 3ea8580

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,12 @@ public function process(ContainerBuilder $container)
9191
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
9292
}
9393

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

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

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ public function testAbstractEventListener()
138138
$registerListenersPass = new RegisterListenersPass();
139139
$registerListenersPass->process($container);
140140
}
141+
142+
/**
143+
* @expectedException \InvalidArgumentException
144+
* @expectedExceptionMessage The service "foo" must not be abstract as event subscribers are lazy-loaded.
145+
*/
146+
public function testAbstractEventSubscriber()
147+
{
148+
$container = new ContainerBuilder();
149+
$container->register('foo', 'stdClass')->setAbstract(true)->addTag('kernel.event_subscriber', array());
150+
$container->register('event_dispatcher', 'stdClass');
151+
152+
$registerListenersPass = new RegisterListenersPass();
153+
$registerListenersPass->process($container);
154+
}
141155
}
142156

143157
class SubscriberService implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

0 commit comments

Comments
 (0)
0