Skip to content

Commit

Permalink
[HttpKernel] added a check for private event listeners/subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 12, 2013
1 parent 427ee19 commit 8444339
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Expand Up @@ -25,6 +25,11 @@ public function process(ContainerBuilder $container)
$definition = $container->getDefinition('event_dispatcher');

foreach ($container->findTaggedServiceIds('kernel.event_listener') as $id => $events) {
$def = $container->getDefinition($id);
if (!$def->isPublic()) {
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event listeners are lazy-loaded.', $id));
}

foreach ($events as $event) {
$priority = isset($event['priority']) ? $event['priority'] : 0;

Expand All @@ -45,8 +50,13 @@ public function process(ContainerBuilder $container)
}

foreach ($container->findTaggedServiceIds('kernel.event_subscriber') as $id => $attributes) {
$def = $container->getDefinition($id);
if (!$def->isPublic()) {
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
}

// We must assume that the class value has been correctly filled, even if the service is created by a factory
$class = $container->getDefinition($id)->getClass();
$class = $def->getClass();

$refClass = new \ReflectionClass($class);
$interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
Expand Down
Expand Up @@ -31,6 +31,9 @@ public function testEventSubscriberWithoutInterface()
);

$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition');
$definition->expects($this->atLeastOnce())
->method('isPublic')
->will($this->returnValue(true));
$definition->expects($this->atLeastOnce())
->method('getClass')
->will($this->returnValue('stdClass'));
Expand Down Expand Up @@ -60,6 +63,9 @@ public function testValidEventSubscriber()
);

$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition');
$definition->expects($this->atLeastOnce())
->method('isPublic')
->will($this->returnValue(true));
$definition->expects($this->atLeastOnce())
->method('getClass')
->will($this->returnValue('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\SubscriberService'));
Expand All @@ -81,6 +87,34 @@ public function testValidEventSubscriber()
$registerListenersPass = new RegisterKernelListenersPass();
$registerListenersPass->process($builder);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The service "foo" must be public as event listeners are lazy-loaded.
*/
public function testPrivateEventListener()
{
$container = new ContainerBuilder();
$container->register('foo', 'stdClass')->setPublic(false)->addTag('kernel.event_listener', array());
$container->register('event_dispatcher', 'stdClass');

$registerListenersPass = new RegisterKernelListenersPass();
$registerListenersPass->process($container);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The service "foo" must be public as event subscribers are lazy-loaded.
*/
public function testPrivateEventSubscriber()
{
$container = new ContainerBuilder();
$container->register('foo', 'stdClass')->setPublic(false)->addTag('kernel.event_subscriber', array());
$container->register('event_dispatcher', 'stdClass');

$registerListenersPass = new RegisterKernelListenersPass();
$registerListenersPass->process($container);
}
}

class SubscriberService implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expand Down

0 comments on commit 8444339

Please sign in to comment.