} public static function initialize() { self::$instance = new PhabricatorEventEngine(); // Instantiate and register custom event listeners so they can react to // events. $listeners = PhabricatorEnv::getEnvConfig('events.listeners'); foreach ($listeners as $listener) { id(new $listener())->register(); } } public static function getInstance() { if (!self::$instance) { throw new Exception("Event engine has not been initialized!"); } return self::$instance; } public function addListener( PhabricatorEventListener $listener, $type) { $this->listeners[$type][] = $listener; return $this; } public static function dispatchEvent(PhabricatorEvent $event) { $instance = self::getInstance(); $listeners = idx($instance->listeners, $event->getType(), array()); foreach ($listeners as $listener) { if ($event->isStopped()) { // Do this first so if someone tries to dispatch a stopped event it // doesn't go anywhere. Silly but less surprising. break; } $listener->handleEvent($event); } } }