/** * @covers Panadas\Event\Event::stop() * @covers Panadas\Event\Event::isStopped() * @covers Panadas\Event\Event::setStopped() */ public function testStop() { $event = new Event("foo", new Publisher()); $this->assertFalse($event->isStopped()); $event->stop(); $this->assertTrue($event->isStopped()); }
/** * Dispatches an event. * * @param string $name The event name * @param Event $event The event object * * @return Event */ public function dispatch($name, Event $event) { if (!$this->hasListeners($name)) { return $event; } foreach ($this->getSortedListeners($name) as $listener) { call_user_func_array($listener, [$event]); if ($event->isStopped()) { // Stop looping if a listener // has stopped the event propagation. break; } } return $event; }
/** * Before exception is happening. * * @param Event $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * @param PhalconException $exception Exception object. * * @throws \Phalcon\Exception * @return bool */ public function beforeException($event, $dispatcher, $exception) { // Handle 404 exceptions. if ($exception instanceof PhDispatchException) { $dispatcher->forward(['module' => EngineApplication::SYSTEM_DEFAULT_MODULE, 'namespace' => ucfirst(EngineApplication::SYSTEM_DEFAULT_MODULE) . '\\Controller', 'controller' => 'Error', 'action' => 'show404']); return false; } if (ENV == ENV_DEVELOPMENT) { throw $exception; } else { EngineException::logException($exception); } // Handle other exceptions. $dispatcher->forward(['module' => EngineApplication::SYSTEM_DEFAULT_MODULE, 'namespace' => ucfirst(EngineApplication::SYSTEM_DEFAULT_MODULE) . '\\Controller', 'controller' => 'Error', 'action' => 'show500']); return $event->isStopped(); }
/** * Trigger an event. * * @param EventInterface|string $event The event object or name. * @param array $args The arguments to set in event. * * @return EventInterface The event after being passed through all listeners. * * @since 2.0 */ public function triggerEvent($event, $args = array()) { if (!$event instanceof EventInterface) { if (isset($this->events[$event])) { $event = $this->events[$event]; } else { $event = new Event($event); } } $arguments = array_merge($event->getArguments(), $args); $event->setArguments($arguments); if (isset($this->listeners[$event->getName()])) { foreach ($this->listeners[$event->getName()] as $listener) { if ($event->isStopped()) { return $event; } if (is_callable($listener)) { call_user_func($listener, $event); } else { call_user_func(array($listener, $event->getName()), $event); } } } return $event; }
/** * Notify listeners for a given event * * @param string|Event $event The Event to be dispatched (or event name) * @param array $data Shortcut when using $event as a string. If an Event * instance is provided, this param will be ignored. * * @return Event The event */ public function notify($event, array $data = array()) { if (is_string($event)) { $event = new Event($event, $data); } $name = strtolower($event->getName()); if (!isset($this->listeners[$name]) || !is_array($this->listeners[$name]) || !count($this->listeners[$name])) { return $event; } foreach ($this->listeners[$name] as $callable) { if (!$event->isStopped()) { call_user_func($callable, $event); $event->setProcessed(true); } } return $event; }
/** * Dispatches a new event to all configured listeners * * @param string|\Cake\Event\Event $event the event key name or instance of Event * @return \Cake\Event\Event */ public function dispatch($event) { if (is_string($event)) { $event = new Event($event); } $listeners = $this->listeners($event->name()); if (empty($listeners)) { return $event; } foreach ($listeners as $listener) { if ($event->isStopped()) { break; } $result = $this->_callListener($listener['callable'], $event); if ($result === false) { $event->stopPropagation(); } if ($result !== null) { $event->result = $result; } } return $event; }