protected function getSystemUri($requestUri = null, $refresh = false)
    {
        if ($this->psrUri && !$refresh) {
            return $this->psrUri;
        }

        $uri = $requestUri ? new PsrUri($requestUri) : $this->getRequest()->getUri();

        $server = $this->getRequest()->getServerParams();

        // If we are working from a CGI SAPI with the 'cgi.fix_pathinfo' directive disabled we use PHP_SELF.
        if (strpos(PHP_SAPI, 'cgi') !== false && !ini_get('cgi.fix_pathinfo') && !empty($server['REQUEST_URI'])) {
            // We aren't expecting PATH_INFO within PHP_SELF so this should work.
            $uri = $uri->withPath(rtrim(\dirname((string) ServerHelper::getValue($server, 'PHP_SELF')), '/\\'));
        } else {
            // Pretty much everything else should be handled with SCRIPT_NAME.
            $uri = $uri->withPath(rtrim(\dirname((string) ServerHelper::getValue($server, 'SCRIPT_NAME')), '/\\'));
        }

        // Clear the unused parts of the requested URI.
        $uri = $uri->withFragment('');

        return $this->psrUri = $uri;
    }