Symfony\Bundle\FrameworkBundle\ContainerAwareEventDispatcher::addListenerService PHP Метод

addListenerService() публичный Метод

Adds a service as event listener
public addListenerService ( string $eventName, array $callback, integer $priority )
$eventName string Event for which the listener is added
$callback array The service ID of the listener service & the method name that has to be called
$priority integer The higher this value, the earlier an event listener will be triggered in the chain. Defaults to 0.
    public function addListenerService($eventName, $callback, $priority = 0)
    {
        if (!is_array($callback) || 2 !== count($callback)) {
            throw new \InvalidArgumentException('Expected an array("service", "method") argument');
        }

        $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority);
    }

Usage Example

 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');
 }
All Usage Examples Of Symfony\Bundle\FrameworkBundle\ContainerAwareEventDispatcher::addListenerService