Example #1
0
 /**
  * Internal handler to call a queue of events
  *
  * @param \SplPriorityQueue|array queue
  * @param Phalcon\Events\Event event
  * @return mixed
  */
 public final function fireQueue($queue, \WpPepVN\Event $event)
 {
     if (!is_array($queue)) {
         if (is_object($queue)) {
             if (!$queue instanceof \SplPriorityQueue) {
                 throw new \Exception(sprintf("Unexpected value type: expected object of type SplPriorityQueue, %s given", get_class($queue)));
             }
         } else {
             throw new \Exception("The queue is not valid");
         }
     }
     $status = null;
     $arguments = null;
     // Get the event type
     $eventName = $event->getType();
     if (!is_string($eventName)) {
         throw new \Exception("The event type not valid");
     }
     // Get the object who triggered the event
     $source = $event->getSource();
     // Get extra data passed to the event
     $data = $event->getData();
     // Tell if the event is cancelable
     $cancelable = (bool) $event->getCancelable();
     // Responses need to be traced?
     $collect = (bool) $this->_collect;
     if (is_object($queue)) {
         // We need to clone the queue before iterate over it
         $iterator = clone $queue;
         // Move the queue to the top
         $iterator->top();
         while ($iterator->valid()) {
             // Get the current data
             $handler = $iterator->current();
             $iterator->next();
             // Only handler objects are valid
             if (is_object($handler)) {
                 // Check if the event is a closure
                 if ($handler instanceof \Closure) {
                     // Create the closure arguments
                     if ($arguments === null) {
                         $arguments = array($event, $source, $data);
                     }
                     // Call the function in the PHP userland
                     $status = call_user_func_array($handler, $arguments);
                     // Trace the response
                     if ($collect) {
                         $this->_responses[] = $status;
                     }
                     if ($cancelable) {
                         // Check if the event was stopped by the user
                         if ($event->isStopped()) {
                             break;
                         }
                     }
                 } else {
                     // Check if the listener has implemented an event with the same name
                     if (method_exists($handler, $eventName)) {
                         // Call the function in the PHP userland
                         $status = $handler->{$eventName}($event, $source, $data);
                         // Collect the response
                         if ($collect) {
                             $this->_responses[] = $status;
                         }
                         if ($cancelable) {
                             // Check if the event was stopped by the user
                             if ($event->isStopped()) {
                                 break;
                             }
                         }
                     }
                 }
             }
         }
     } else {
         foreach ($queue as $handler) {
             // Only handler objects are valid
             if (is_object($handler)) {
                 // Check if the event is a closure
                 if ($handler instanceof \Closure) {
                     // Create the closure arguments
                     if ($arguments === null) {
                         $arguments = array($event, $source, $data);
                     }
                     // Call the function in the PHP userland
                     $status = call_user_func_array($handler, $arguments);
                     // Trace the response
                     if ($collect) {
                         $this->_responses[] = $status;
                     }
                     if ($cancelable) {
                         // Check if the event was stopped by the user
                         if ($event->isStopped()) {
                             break;
                         }
                     }
                 } else {
                     // Check if the listener has implemented an event with the same name
                     if (method_exists($handler, $eventName)) {
                         // Call the function in the PHP userland
                         $status = $handler->{$eventName}($event, $source, $data);
                         // Collect the response
                         if ($collect) {
                             $this->_responses[] = $status;
                         }
                         if ($cancelable) {
                             // Check if the event was stopped by the user
                             if ($event->isStopped()) {
                                 break;
                             }
                         }
                     }
                 }
             }
         }
     }
     return $status;
 }