public function testAppendData() { $data = array('package' => true); $event = new Event('test', $data); $this->assertEquals(array('package' => true), $event->data()); $data2 = array('package2' => true); $event->data($data2, true); $this->assertEquals(array('package' => true, 'package2' => true), $event->data()); }
/** * Call all listeners registered to provided event with data. * * Returns true if a registered event's callback was called. * * @param \Zumba\Symbiosis\Event\Event $event Event object * @param array $data Data to append/override in the event object * @return boolean */ public function trigger(Event $event, $data = array()) { $eventName = $event->name(); $event->data(array_merge($event->data(), $data)); if (!isset($this->registry[$eventName])) { Log::write('No event registered.', Log::LEVEL_DEBUG, compact('eventName')); return false; } Log::write('Event triggered.', Log::LEVEL_DEBUG, compact('eventName')); foreach ($this->registry[$eventName] as $listeners) { foreach ($listeners as $listener) { if ($listener($event) === false) { $event->stopPropagation(); } if (!$event->isPropagating()) { Log::write('Propagation stopped.', Log::LEVEL_DEBUG, compact('listener', 'eventName')); break; } } } return true; }
public function register2(Event $e) { $data = $e->data(); $data['called']++; $e->data($data); }
public function testEventPriority() { $event = new Event('test.event1', array()); // lower priority $lowPriority = function (Event $event) { $data = $event->data(); $data['order'][] = 3; $event->data($data); }; $highPriority = function (Event $event) { $data = $event->data(); $data['order'][] = 1; $event->data($data); }; $alsoHighPriority = function (Event $event) { $data = $event->data(); $data['order'][] = 2; $event->data($data); }; $this->registry->register('test.event1', $lowPriority, EventRegistry::PRIORITY_LOW); $this->registry->register('test.event1', $highPriority, EventRegistry::PRIORITY_HIGH); $this->registry->register('test.event1', $alsoHighPriority, EventRegistry::PRIORITY_HIGH); $event = new Event('test.event1', array('order' => array())); $this->registry->trigger($event); $data = $event->data(); $this->assertEquals(array(1, 2, 3), $data['order']); }