public dispatch ( $eventName, Symfony\Component\EventDispatcher\Event $event = null ) | ||
$event | Symfony\Component\EventDispatcher\Event |
public function dispatch($eventName, Event $event = null)
{
if (isset($this->listenerIds[$eventName])) {
foreach ($this->listenerIds[$eventName] as $args) {
list($serviceId, $method, $priority) = $args;
$listener = $this->container->get($serviceId);
$key = $serviceId.$method;
if (!isset($this->listeners[$eventName][$key])) {
$this->addListener($eventName, array($listener, $method), $priority);
} elseif ($listener !== $this->listeners[$eventName][$key]) {
$this->removeListener($eventName, array($this->listeners[$eventName][$key], $method));
$this->addListener($eventName, array($listener, $method), $priority);
}
$this->listeners[$eventName][$key] = $listener;
}
}
parent::dispatch($eventName, $event);
}
public function testReEnteringAScope() { $event = new Event(); $service1 = $this->getMock('Symfony\\Bundle\\FrameworkBundle\\Tests\\Service'); $service1->expects($this->exactly(2))->method('onEvent')->with($event); $scope = new Scope('scope'); $container = new Container(); $container->addScope($scope); $container->enterScope('scope'); $container->set('service.listener', $service1, 'scope'); $dispatcher = new ContainerAwareEventDispatcher($container); $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent')); $dispatcher->dispatch('onEvent', $event); $service2 = $this->getMock('Symfony\\Bundle\\FrameworkBundle\\Tests\\Service'); $service2->expects($this->once())->method('onEvent')->with($event); $container->enterScope('scope'); $container->set('service.listener', $service2, 'scope'); $dispatcher->dispatch('onEvent', $event); $container->leaveScope('scope'); $dispatcher->dispatch('onEvent'); }