function Psr17Factory::buildUriFromGlobals
1 call to Psr17Factory::buildUriFromGlobals()
- Psr17Factory::createUriFromGlobals in vendor/
php-http/ discovery/ src/ Psr17Factory.php
File
-
vendor/
php-http/ discovery/ src/ Psr17Factory.php, line 219
Class
- Psr17Factory
- A generic PSR-17 implementation.
Namespace
Http\DiscoveryCode
private function buildUriFromGlobals(UriInterface $uri, array $server) : UriInterface {
$uri = $uri->withScheme(!empty($server['HTTPS']) && 'off' !== strtolower($server['HTTPS']) ? 'https' : 'http');
$hasPort = false;
if (isset($server['HTTP_HOST'])) {
$parts = parse_url('http://' . $server['HTTP_HOST']);
$uri = $uri->withHost($parts['host'] ?? 'localhost');
if ($parts['port'] ?? false) {
$hasPort = true;
$uri = $uri->withPort($parts['port']);
}
}
else {
$uri = $uri->withHost($server['SERVER_NAME'] ?? $server['SERVER_ADDR'] ?? 'localhost');
}
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;
}