public static function retrieve($context)
    {
        if ($context instanceof sfContext || $context instanceof sfProjectConfiguration) {
            return $context->getEventDispatcher();
        } elseif (isset($GLOBALS['dispatcher'])) {
            return $GLOBALS['dispatcher'];
        }

        $refClass = new ReflectionClass(get_class($context));

        if ($refClass->hasProperty('dispatcher')) {
            return self::retrieveByDispatcherProperty($refClass, $context);
        } elseif ($refClass->hasProperty('configuration')) {
            return self::retrieveByConfigurationProperty($refClass, $context);
        }
    }