public static function fromCurrent()
{
$scheme = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$baseUrl = "{$scheme}://{$host}";
$url = new self($baseUrl);
if (!empty($_SERVER['REQUEST_URI'])) {
if (strpos($_SERVER['REQUEST_URI'], '?') !== false) {
list($path, $query) = explode('?', $_SERVER['REQUEST_URI'], 2);
} else {
$path = $_SERVER['REQUEST_URI'];
$query = '';
}
$url->set('path', $path);
$url->set('query', $query);
}
// Only set port if different from default (80 or 443)
if (!empty($_SERVER['SERVER_PORT'])) {
$port = $_SERVER['SERVER_PORT'];
if ($scheme == 'http' && $port != 80 || $scheme == 'https' && $port != 443) {
$url->set('port', $port);
}
}
// Authentication
if (!empty($_SERVER['PHP_AUTH_USER'])) {
$url->set('user', $_SERVER['PHP_AUTH_USER']);
if (!empty($_SERVER['PHP_AUTH_PW'])) {
$url->set('pass', $_SERVER['PHP_AUTH_PW']);
}
}
return $url;
}