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; namespace Kiri\Router\Constrict;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Swoole\Http\Request;
class Uri implements UriInterface class Uri implements UriInterface
{ {
@@ -15,7 +16,15 @@ class Uri implements UriInterface
private string $user = ''; private string $user = '';
private string $password = ''; private string $password = '';
private string $queryString; 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. * Retrieve the scheme component of the URI.
@@ -34,7 +43,10 @@ class Uri implements UriInterface
public function getScheme(): string public function getScheme(): string
{ {
// TODO: Implement getScheme() method. // 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 public function getHost(): string
{ {
// TODO: Implement getHost() method. // 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 public function getPort(): ?int
{ {
// TODO: Implement getPort() method. // 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 public function getPath(): string
{ {
// TODO: Implement getPath() method. // 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 public function getQuery(): string
{ {
// TODO: Implement getQuery() method. // 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 public function withScheme(string $scheme): static
{ {
// TODO: Implement withScheme() method.
$this->scheme = $scheme;
return $this; return $this;
} }
@@ -237,9 +247,6 @@ class Uri implements UriInterface
*/ */
public function withUserInfo(string $user, ?string $password = null): static public function withUserInfo(string $user, ?string $password = null): static
{ {
// TODO: Implement withUserInfo() method.
$this->user = $user;
$this->password = $password;
return $this; return $this;
} }
@@ -258,7 +265,6 @@ class Uri implements UriInterface
public function withHost(string $host): static public function withHost(string $host): static
{ {
// TODO: Implement withHost() method. // TODO: Implement withHost() method.
$this->host = $host;
return $this; return $this;
} }
@@ -282,7 +288,6 @@ class Uri implements UriInterface
public function withPort(?int $port): static public function withPort(?int $port): static
{ {
// TODO: Implement withPort() method. // TODO: Implement withPort() method.
$this->port = $port;
return $this; return $this;
} }
@@ -311,7 +316,6 @@ class Uri implements UriInterface
public function withPath(string $path): static public function withPath(string $path): static
{ {
// TODO: Implement withPath() method. // TODO: Implement withPath() method.
$this->path = $path;
return $this; return $this;
} }
@@ -333,7 +337,6 @@ class Uri implements UriInterface
public function withQuery(string $query): static public function withQuery(string $query): static
{ {
// TODO: Implement withQuery() method. // TODO: Implement withQuery() method.
$this->queryString = $query;
return $this; return $this;
} }
@@ -354,7 +357,6 @@ class Uri implements UriInterface
public function withFragment(string $fragment): static public function withFragment(string $fragment): static
{ {
// TODO: Implement withFragment() method. // TODO: Implement withFragment() method.
$this->fragment = $fragment;
return $this; return $this;
} }
@@ -384,7 +386,12 @@ class Uri implements UriInterface
public function __toString() public function __toString()
{ {
// TODO: Implement __toString() method. // 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;
} }
} }