Example #1
0
 /**
  * Call all handlers for a given event
  *
  * Note: if a handler returns boolean false, any remaining handlers are skipped
  * @param sndsgd\Event|string $event An event, or a type/namespace combo
  * @param array.<string,mixed> $data Data to add to the event
  * @return boolean
  * @return boolean:false A handler returned false
  * @return boolean:true All handlers returned true or no handlers exist
  * @throws InvalidArgumentException If the event isn't a string
  */
 public function fire($event, array $data = [])
 {
     if (is_string($event)) {
         $event = new Event($event);
         $event->setData($data);
     } else {
         if ($event instanceof Event) {
             if ($data) {
                 $event->addData($data);
             }
         } else {
             throw new InvalidArgumentException("invalid value provided for 'event'; " . "expecting an event name as string");
         }
     }
     $type = $event->getType();
     $namespace = $event->getNamespace();
     foreach ($this->eventHandlers as $handler) {
         if ($handler->canHandle($type, $namespace) && $handler->handle($event) === false) {
             return false;
         }
     }
     return true;
 }