protected function raiseEvent($event)
    {
        if (!isset($this->eventHandlers[$event])) {
            return null;
        }

        $args = func_get_args();
        $args = array_slice($args, 1);

        // Check if the last argument is a callback.
        $count = count($args);
        $callBack = false;

        if (($count > 0) && is_object($args[$count - 1]) && is_callable($args[$count - 1])) {
            $callBack = $args[$count - 1];
            $args = array_slice($args, 0, -1);
        }

        $result = null;

        foreach ($this->eventHandlers[$event] as $delegate) {
            $answer = call_user_func_array($delegate, $args);

            if ($callBack !== false && $answer !== null) {
                call_user_func($callBack, $answer);
            }

            // If we don't have a result yet - make this the result. This way the first event handler to
            // return a non null result will be the overall result of the event.
            if ($result === null) {
                $result = $answer;
            }
        }

        return $result;
    }