public function addHandler($name, $handler, $priority = self::PRIORITY_DEFAULT)
    {
        $priority = (int)$priority;
        if ($priority >= self::PRIORITY_MIN || $priority < self::PRIORITY_MAX) {
            throw new InvalidArgumentException("Invalid event priority");
        }
        $isObject = false;
        if (is_array($handler)) {
            $isObject = is_object($handler[0]);
            $key = sprintf(
                "%s%s%s",
                $isObject ? get_class($handler[0]) : $handler[0],
                $isObject ? "->" : "::",
                $handler[1]
            );
        } else {
            $key = $handler;
        }
        $addHandler = true;
        if (empty($this->handlers[$name])) {
            $this->handlers[$name] = [];
        } else if (isset($this->handlers[$name][$key])) {
            if ($isObject) {
                // Detect different instances and try to add handler.
                $this->key = $key . " ";
                $handlers = array_filter(
                    $this->handlers[$name],
                    [$this, 'filterHandlers'],
                    ARRAY_FILTER_USE_KEY
                );
                $handlers[$key] = $this->handlers[$name][$key];
                foreach ($handlers as $existing) {
                    if ($handler[0] === $existing[1][0]) {
                        $addHandler = false;
                        break;
                    }
                    if ($addHandler) {
                        $key = sprintf("%s #%d", $key, sizeof($handlers) + 1);
                    }
                }
            } else {
                $addHandler = false;
            }
        }
        if ($addHandler) {
            $this->handlers[$name][$key] = [$priority, $handler];
            unset($this->orderedHandlers[$name]);
        }
        if ($this->debug && !in_array($name, self::$debugEvents)) {
            $debugArgs = new Args([
                'name'    => $name,
                'handler' => $handler,
                'added'   => $addHandler,
                'source'  => 'core:event:debug',
                'level'   => $addHandler ? E_NOTICE : E_WARNING,
            ]);
            $this->fire(':onAddHandler:', $debugArgs);
        }
    }