yii\base\Event::hasHandlers PHP Method

hasHandlers() public static method

Note that this method will also check all parent classes to see if there is any handler attached to the named event.
public static hasHandlers ( string | object $class, string $name ) : boolean
$class string | object the object or the fully qualified class name specifying the class-level event.
$name string the event name.
return boolean whether there is any handler attached to the event.
    public static function hasHandlers($class, $name)
    {
        if (empty(self::$_events[$name])) {
            return false;
        }
        if (is_object($class)) {
            $class = get_class($class);
        } else {
            $class = ltrim($class, '\\');
        }
        $classes = array_merge([$class], class_parents($class, true), class_implements($class, true));
        foreach ($classes as $class) {
            if (!empty(self::$_events[$name][$class])) {
                return true;
            }
        }
        return false;
    }

Usage Example

 public function test_MigrationEvent()
 {
     try {
         $this->cleanDb();
         $this->mockYiiApplication();
         \Yii::$app->mycfg->system->version = null;
         // to trigger migration event
         $bootstrap = new MyLibraryBootstrap();
         $bootstrap->bootstrap(\Yii::$app);
         $this->assertTrue(\yii\base\Event::hasHandlers(\app\components\Controller::class, \app\components\Controller::EVENT_BEFORE_ACTION), 'migration event for controller was not added');
     } finally {
         $this->resetConnection();
     }
 }
All Usage Examples Of yii\base\Event::hasHandlers