Example #1
0
 public function testStopPropagation()
 {
     $eventManager = new EventManager();
     $event = new Event('event1');
     $event->setParam('count', 0);
     $eventManager->addListener('event1', function ($e) {
         $count = $e->getParam('count', 0);
         $e->setParam('count', $count + 1);
     }, 2);
     $eventManager->addListener('event1', function ($e) {
         $count = $e->getParam('count', 0);
         $e->setParam('count', $count + 1);
     }, 2);
     $eventManager->addListener('event1', function ($e) {
         $count = $e->getParam('count', 0);
         $e->setParam('count', $count + 1);
     }, 1);
     $eventManager->addListener('event1', function ($e) {
         $count = $e->getParam('count', 0);
         $e->setParam('count', $count + 1);
         $e->stopPropagation();
     });
     $eventManager->trigger($event);
     $this->assertEquals($event->getParam('count'), 2);
 }
Example #2
0
 /**
  * Get EventManagr
  *
  * @return \Tlumx\EventManager\EventManager
  * @throws \RuntimeException
  */
 public function getEventManager()
 {
     if (!$this->eventManager) {
         $this->eventManager = new EventManager();
         $listeners = $this->getConfig('listeners');
         if (is_array($listeners)) {
             foreach ($listeners as $listener) {
                 if (!class_exists($listener)) {
                     throw new \RuntimeException(sprintf('Event listener class "%s" does not exist', $listener));
                 }
                 $instance = new $listener();
                 $this->eventManager->addListenerObject($instance);
             }
         }
     }
     return $this->eventManager;
 }
Example #3
0
 public function testSetEventManager()
 {
     $eventManager = new EventManager();
     $eventManager->addListener('my', function () {
         // ...
     });
     $this->provider->setEventManager($eventManager);
     $newEm = $this->provider->getEventManager();
     $this->assertTrue($newEm->hasListeners('my'));
 }