Skip to content

Commit

Permalink
Fix getUriFromGlobals to work with CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ivank committed Jan 8, 2016
1 parent 3af3146 commit f494712
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,27 @@ public static function fromGlobals()
public static function getUriFromGlobals() {
$uri = new Uri('');

return $uri
->withScheme(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http')
->withHost($_SERVER['SERVER_NAME'])
->withPort($_SERVER['SERVER_PORT'])
->withPath(current(explode('?', $_SERVER['REQUEST_URI'])))
->withQuery(isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '');
if (isset($_SERVER['HTTPS'])) {
$uri = $uri->withScheme($_SERVER['HTTPS'] == 'on' ? 'https' : 'http');
}

if (isset($_SERVER['SERVER_NAME'])) {
$uri = $uri->withHost($_SERVER['SERVER_NAME']);
}

if (isset($_SERVER['SERVER_PORT'])) {
$uri = $uri->withPort($_SERVER['SERVER_PORT']);
}

if (isset($_SERVER['REQUEST_URI'])) {
$uri = $uri->withPath(current(explode('?', $_SERVER['REQUEST_URI'])));
}

if (isset($_SERVER['QUERY_STRING'])) {
$uri = $uri->withQuery($_SERVER['QUERY_STRING']);
}

return $uri;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/ServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ public function dataGetUriFromGlobals()
'http://www.blakesimpson.co.uk:8324/blog/article.php?id=10&user=foo',
array_merge($server, ['SERVER_PORT' => '8324']),
],
'Empty server variable' => [
'',
[],
],
];
}

Expand Down

0 comments on commit f494712

Please sign in to comment.