public static function normalizePath($filepath, $separator = DIRECTORY_SEPARATOR, $removeTrailing = true)
    {
        $scheme = '';
        if (false !== $parseUrl = parse_url($filepath)) {
            $auth = isset($parseUrl['user']) ? $parseUrl['user'] : '';
            $auth .= isset($parseUrl['pass']) ? ':' . $parseUrl['pass'] : '';
            $auth .= (isset($parseUrl['user']) || isset($parseUrl['pass'])) ? '@' : '';
            $scheme .= isset($parseUrl['scheme']) ? $parseUrl['scheme'] . ':' : '';
            $scheme .= isset($parseUrl['host']) ? '//' . $auth . $parseUrl['host'] : '';
            $scheme .= isset($parseUrl['port']) ? ':' . $parseUrl['port'] : '';

            if (isset($parseUrl['path'])) {
                $filepath = $parseUrl['path'];
            }

            if (isset($parseUrl['scheme']) && isset($parseUrl['host'])) {
                $separator = '/';
            }
        }

        $patterns = array('/\//', '/\\\\/', '/\/+/');
        $replacements = array_fill(0, 3, '/');

        if (true === $removeTrailing) {
            $patterns[] = '/\/$/';
            $replacements[] = '';
        }

        return str_replace('/', $separator, $scheme.preg_replace($patterns, $replacements, $filepath));
    }