This commit is contained in:
2023-07-26 09:58:35 +08:00
parent 00cd571ffe
commit 329029169a
+24 -17
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Kiri\Router\Constrict;
use Psr\Http\Message\UriInterface;
use Swoole\Http\Request;
class Uri implements UriInterface
{
@@ -15,7 +16,15 @@ class Uri implements UriInterface
private string $user = '';
private string $password = '';
private string $queryString;
private string $fragment;
private string $fragment = '';
/**
* @param Request $request
*/
public function __construct(readonly public Request $request)
{
}
/**
* Retrieve the scheme component of the URI.
@@ -34,7 +43,10 @@ class Uri implements UriInterface
public function getScheme(): string
{
// TODO: Implement getScheme() method.
return $this->scheme;
if (isset($this->request->server['https']) && $this->request->server['https'] !== 'off') {
return 'https';
}
return 'http';
}
/**
@@ -96,7 +108,7 @@ class Uri implements UriInterface
public function getHost(): string
{
// TODO: Implement getHost() method.
return $this->host;
return $this->request->server['host'] ?? '127.0.0.1';
}
/**
@@ -117,7 +129,7 @@ class Uri implements UriInterface
public function getPort(): ?int
{
// TODO: Implement getPort() method.
return $this->port;
return $this->request->server['server_port'];
}
/**
@@ -148,7 +160,7 @@ class Uri implements UriInterface
public function getPath(): string
{
// TODO: Implement getPath() method.
return $this->path;
return $this->request->server['path_info'];
}
/**
@@ -174,7 +186,7 @@ class Uri implements UriInterface
public function getQuery(): string
{
// TODO: Implement getQuery() method.
return $this->queryString;
return $request->server['query_string'] ?? '';
}
/**
@@ -216,8 +228,6 @@ class Uri implements UriInterface
*/
public function withScheme(string $scheme): static
{
// TODO: Implement withScheme() method.
$this->scheme = $scheme;
return $this;
}
@@ -237,9 +247,6 @@ class Uri implements UriInterface
*/
public function withUserInfo(string $user, ?string $password = null): static
{
// TODO: Implement withUserInfo() method.
$this->user = $user;
$this->password = $password;
return $this;
}
@@ -258,7 +265,6 @@ class Uri implements UriInterface
public function withHost(string $host): static
{
// TODO: Implement withHost() method.
$this->host = $host;
return $this;
}
@@ -282,7 +288,6 @@ class Uri implements UriInterface
public function withPort(?int $port): static
{
// TODO: Implement withPort() method.
$this->port = $port;
return $this;
}
@@ -311,7 +316,6 @@ class Uri implements UriInterface
public function withPath(string $path): static
{
// TODO: Implement withPath() method.
$this->path = $path;
return $this;
}
@@ -333,7 +337,6 @@ class Uri implements UriInterface
public function withQuery(string $query): static
{
// TODO: Implement withQuery() method.
$this->queryString = $query;
return $this;
}
@@ -354,7 +357,6 @@ class Uri implements UriInterface
public function withFragment(string $fragment): static
{
// TODO: Implement withFragment() method.
$this->fragment = $fragment;
return $this;
}
@@ -384,7 +386,12 @@ class Uri implements UriInterface
public function __toString()
{
// TODO: Implement __toString() method.
return $this->scheme . '://x.x.x.x:' . $this->port . '/' . $this->path . '?' . $this->queryString;
$url = $this->getScheme() . '://' . $this->getHost();
if ($this->getPort() !== 80 && $this->getPort() !== 443) {
$url .= ':' . $this->getPort();
}
$url .= '/' . $this->getPath() . '?' . $this->getQuery();
return $url;
}
}