Lazily loads listeners for this event from the dependency injection
container.
public dispatch ( $eventName, Symfony\Component\EventDispatcher\Event $event = null ) | ||
$event | Symfony\Component\EventDispatcher\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'); }
public function dispatch($eventName, Event $event = null) { if ('kernel.request' === $eventName) { $this->stopwatch->startSection(); } elseif ('kernel.view' === $eventName || 'kernel.response' === $eventName) { // stop only if a controller has been executed try { $this->stopwatch->stop('controller'); } catch (\LogicException $e) { } } $e1 = $this->stopwatch->start($eventName, 'section'); parent::dispatch($eventName, $event); $e1->stop(); if ('kernel.controller' === $eventName) { $this->stopwatch->start('controller', 'section'); } elseif ('kernel.response' === $eventName) { $token = $event->getResponse()->headers->get('X-Debug-Token'); $this->stopwatch->stopSection($token); $this->updateProfile($token); } }
/** * {@inheritdoc} */ public function dispatch($eventName, Event $event = null) { switch ($eventName) { case 'kernel.request': $this->stopwatch->openSection(); break; case 'kernel.view': case 'kernel.response': // stop only if a controller has been executed try { $this->stopwatch->stop('controller'); } catch (\LogicException $e) { } break; case 'kernel.terminate': $token = $event->getResponse()->headers->get('X-Debug-Token'); $this->stopwatch->openSection($token); break; } $e1 = $this->stopwatch->start($eventName, 'section'); parent::dispatch($eventName, $event); $e1->stop(); switch ($eventName) { case 'kernel.controller': $this->stopwatch->start('controller', 'section'); break; case 'kernel.response': $token = $event->getResponse()->headers->get('X-Debug-Token'); $this->stopwatch->stopSection($token); $this->updateProfile($token); break; case 'kernel.terminate': $this->stopwatch->stopSection($token); $this->updateProfile($token); break; } return $event; }
public function testRemoveAfterDispatch() { $event = new Event(); $service = $this->getMock('Symfony\\Bundle\\FrameworkBundle\\Tests\\Service'); $container = new Container(); $container->set('service.listener', $service); $dispatcher = new ContainerAwareEventDispatcher($container); $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent')); $dispatcher->dispatch('onEvent', new Event()); $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent')); $this->assertFalse($dispatcher->hasListeners('onEvent')); }
public function testHasListenersOnLazyLoad() { $event = new Event(); $service = $this->getMock('Symfony\\Bundle\\FrameworkBundle\\Tests\\Service'); $service->expects($this->once())->method('onEvent')->with($event); $container = new Container(); $container->set('service.listener', $service); $dispatcher = new ContainerAwareEventDispatcher($container); $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent')); $this->assertTrue($dispatcher->hasListeners()); if ($dispatcher->hasListeners('onEvent')) { $dispatcher->dispatch('onEvent'); } }