You are here

public static function ServerRequest::getUriFromGlobals in Lockr 7.3

Get a Uri populated with values from $_SERVER.

Return value

UriInterface

1 call to ServerRequest::getUriFromGlobals()
ServerRequest::fromGlobals in vendor/guzzlehttp/psr7/src/ServerRequest.php
Return a ServerRequest populated with superglobals: $_GET $_POST $_COOKIE $_FILES $_SERVER

File

vendor/guzzlehttp/psr7/src/ServerRequest.php, line 202

Class

ServerRequest
Server-side HTTP request

Namespace

GuzzleHttp\Psr7

Code

public static function getUriFromGlobals() {
  $uri = new Uri('');
  $uri = $uri
    ->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
  $hasPort = false;
  if (isset($_SERVER['HTTP_HOST'])) {
    list($host, $port) = self::extractHostAndPortFromAuthority($_SERVER['HTTP_HOST']);
    if ($host !== null) {
      $uri = $uri
        ->withHost($host);
    }
    if ($port !== null) {
      $hasPort = true;
      $uri = $uri
        ->withPort($port);
    }
  }
  elseif (isset($_SERVER['SERVER_NAME'])) {
    $uri = $uri
      ->withHost($_SERVER['SERVER_NAME']);
  }
  elseif (isset($_SERVER['SERVER_ADDR'])) {
    $uri = $uri
      ->withHost($_SERVER['SERVER_ADDR']);
  }
  if (!$hasPort && isset($_SERVER['SERVER_PORT'])) {
    $uri = $uri
      ->withPort($_SERVER['SERVER_PORT']);
  }
  $hasQuery = false;
  if (isset($_SERVER['REQUEST_URI'])) {
    $requestUriParts = explode('?', $_SERVER['REQUEST_URI'], 2);
    $uri = $uri
      ->withPath($requestUriParts[0]);
    if (isset($requestUriParts[1])) {
      $hasQuery = true;
      $uri = $uri
        ->withQuery($requestUriParts[1]);
    }
  }
  if (!$hasQuery && isset($_SERVER['QUERY_STRING'])) {
    $uri = $uri
      ->withQuery($_SERVER['QUERY_STRING']);
  }
  return $uri;
}