public function testGetListenersWhenAddedCallbackListenerIsRemoved()
 {
     $listener = function () {};
     $this->dispatcher->addListener('foo', $listener);
     $this->dispatcher->removeListener('foo', $listener);
     $this->assertSame(array(), $this->dispatcher->getListeners());
 }
Пример #2
0
 /**
  * Load all persistent events handlers into EventManager.
  *
  * This loads persistent events registered by modules and module plugins.
  *
  * @internal
  *
  * @return void
  */
 public static function loadPersistentEvents()
 {
     $handlerGroup = ModUtil::getVar(self::HANDLERS);
     if (!$handlerGroup) {
         return;
     }
     foreach ($handlerGroup as $module => $handlers) {
         if (!$handlers) {
             continue;
         }
         foreach ($handlers as $handler) {
             if (ModUtil::available($module)) {
                 try {
                     if (isset($handler['classname'])) {
                         self::attachEventHandler($handler['classname']);
                     } else {
                         self::$dispatcher->addListener($handler['eventname'], $handler['callable'], $handler['weight']);
                     }
                 } catch (InvalidArgumentException $e) {
                     LogUtil::log(sprintf("Event handler could not be attached because %s", $e->getMessage()), Zikula_AbstractErrorHandler::ERR);
                 }
             }
         }
     }
 }
 /**
  * @covers \Heystack\Core\EventDispatcher::dispatch
  */
 public function testDoesDispatchWhenEnabled()
 {
     $fired = false;
     $e = new EventDispatcher();
     $e->addListener('test', function () use(&$fired) {
         $fired = true;
     });
     $e->dispatch('test');
     $this->assertTrue($fired);
 }
Пример #4
0
 private function registerListeners()
 {
     $reg = $this->service->getExtensionRegistry();
     $listeners = $reg->getExtensions(CoreModule::EXT_LISTENER);
     $map = new Map();
     $getClass = function ($className) use($map) {
         if (class_exists($className)) {
             if ($map->has($className)) {
                 $class = $map->get($className);
             } else {
                 $class = new $className();
                 $map->set($className, $class);
             }
             if ($class instanceof KeekoEventListenerInterface) {
                 $class->setServiceContainer($this->service);
             }
             return $class;
         }
         return null;
     };
     foreach ($listeners as $listener) {
         // subscriber first
         if (isset($listener['subscriber'])) {
             $className = $listener['subscriber'];
             $subscriber = $getClass($className);
             if ($subscriber !== null && $subscriber instanceof KeekoEventSubscriberInterface) {
                 $this->dispatcher->addSubscriber($subscriber);
             }
         }
         // class
         if (isset($listener['class']) && isset($listener['method']) && isset($listener['event'])) {
             $className = $listener['class'];
             $class = $getClass($className);
             if ($class !== null && $class instanceof KeekoEventListenerInterface && method_exists($class, $listener['method'])) {
                 $this->dispatcher->addListener($listener['event'], [$class, $listener['method']]);
             }
         }
     }
 }
Пример #5
0
 public function onKernalInit(\Event $event)
 {
     $listeners = [['eventName' => KernalEvent::REQUEST, 'listener' => 'Group\\Listeners\\KernalRequestListener', 'priority' => 0], ['eventName' => KernalEvent::RESPONSE, 'listener' => 'Group\\Listeners\\KernalResponseListener', 'priority' => 0], ['eventName' => KernalEvent::EXCEPTION, 'listener' => 'Group\\Listeners\\ExceptionListener', 'priority' => 0], ['eventName' => KernalEvent::NOTFOUND, 'listener' => 'Group\\Listeners\\NotFoundListener', 'priority' => 0]];
     $listeners = array_merge(\Config::get('listener::services'), $listeners);
     foreach ($listeners as $listener) {
         if (!class_exists($listener['listener'])) {
             throw new NotFoundException("Class " . $listener['listener'] . " not found !");
         }
         $lis = new $listener['listener']();
         if (!$lis instanceof Listener) {
             throw new \RuntimeException("Class " . $listener['listener'] . " must instanceof Listener !");
         }
         \EventDispatcher::addListener($listener['eventName'], $lis, $listener['priority']);
     }
 }
Пример #6
0
 public function onEventDispatcherInit(\Event $event)
 {
     $listeners = [['eventName' => 'kernal.response', 'listener' => 'core\\Group\\Listeners\\KernalResponseListener', 'priority' => 0]];
     $listeners = array_merge(\Config::get('listener::services'), $listeners);
     foreach ($listeners as $listener) {
         if (!class_exists($listener['listener'])) {
             throw new NotFoundException("Class " . $listener['listener'] . " not found !");
         }
         $lis = new $listener['listener']();
         if (!$lis instanceof Listener) {
             throw new \RuntimeException("Class " . $listener['listener'] . " must instanceof Listener !");
         }
         \EventDispatcher::addListener($listener['eventName'], $lis, $listener['priority']);
     }
 }
Пример #7
0
 /**
  * {@inheritDoc}
  */
 public function addListener(EventListener $listener, $priority = 0, $important = false)
 {
     $this->eventDispatcher->addListener($listener, $priority, $important);
     return $this;
 }
 public function registerConsumer(Consumer $consumer)
 {
     foreach ($consumer->getQueueOptions()['routing_keys'] as $routingKey) {
         $this->dispatcher->addListener($routingKey, array(new AMQPMessageListener($consumer, $routingKey), 'execute'));
     }
 }
 public function on($event, $then, $p = 0)
 {
     $this->dispatcher->addListener($event, $then, $p);
 }