Example #1
0
 /**
  * Publish an event by calling all listeners that have registered to receive it.
  *
  * @param  string|KEventInterface  $event     The event name or a KEventInterface object
  * @param  array|Traversable       $attributes An associative array or a Traversable object
  * @param  KObjectInterface        $target    The event target
  * @return null|KEventInterface Returns the event object. If the chain is not enabled will return NULL.
  */
 public function publishEvent($event, $attributes = array(), $target = null)
 {
     if ($this->isEnabled()) {
         //Make sure we have an event object
         if (!$event instanceof KEventInterface) {
             $event = new KEvent($event, $attributes, $target);
         }
         //Notify the listeners
         $listeners = $this->getListeners($event->getName());
         foreach ($listeners as $listener) {
             $start = microtime(true);
             call_user_func($listener, $event, $this);
             $this->__profiles[] = array('message' => $event->getName(), 'period' => microtime(true) - $start, 'time' => microtime(true), 'memory' => $this->getMemoryUsage(), 'target' => $target instanceof KObjectInterface ? $target->getIdentifier() : $target, 'listener' => $listener);
             if (!$event->canPropagate()) {
                 break;
             }
         }
         return $event;
     } else {
         $this->getDelegate()->publishEvent($event, $attributes, $target);
     }
     return null;
 }
Example #2
0
 /**
  * Publish an event by calling all listeners that have registered to receive it.
  *
  * @param  string|KEventInterface             $event      The event name or a KEventInterface object
  * @param  array|Traversable|KEventInterface  $attributes An associative array, an object implementing the
  *                                                        KEventInterface or a Traversable object
  * @param  mixed                              $target     The event target
  * @throws InvalidArgumentException  If the event is not a string or does not implement the KEventInterface
  * @return null|KEventInterface Returns the event object. If the chain is not enabled will return NULL.
  */
 public function publishEvent($event, $attributes = array(), $target = null)
 {
     if ($this->isEnabled()) {
         if (!is_string($event) && !$event instanceof KEventInterface) {
             throw new InvalidArgumentException('The event must be a string or implement the KEventInterface, "' . gettype($event) . '" given.');
         }
         //Make sure we have an event object
         if (!$event instanceof KEventInterface) {
             if ($attributes instanceof KEventInterface) {
                 $name = $event;
                 $event = $attributes;
                 $event->setName($name);
             } else {
                 $event = new KEvent($event, $attributes, $target);
             }
         }
         //Notify the listeners
         $listeners = $this->getListeners($event->getName());
         foreach ($listeners as $listener) {
             call_user_func($listener, $event, $this);
             if (!$event->canPropagate()) {
                 break;
             }
         }
         return $event;
     }
     return null;
 }