This commit is contained in:
2021-08-25 19:11:15 +08:00
parent 84703373a2
commit bd1f60623e
22 changed files with 689 additions and 975 deletions
+179
View File
@@ -0,0 +1,179 @@
<?php
namespace HttpMessage;
use Http\Context\Context;
use Kiri\Di\ContainerInterface;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;
use Server\RequestInterface;
/**
*
*/
trait Message
{
/**
* @var string
*/
public string $protocol = '1.1';
/**
* @var mixed|string
*/
public mixed $body = '';
/**
* @var array<string,array>
*/
public array $headers = [];
/**
* @return string
*/
public function getProtocolVersion(): string
{
return $this->protocol;
}
/**
* @param string $version
* @return $this
*/
public function withProtocolVersion($version): static
{
// TODO: Implement withProtocolVersion() method.
$new = clone $this;
$new->protocol = $version;
return $new;
}
/**
* @return array
*/
public function getHeaders(): array
{
// TODO: Implement getHeaders() method.
return $this->headers;
}
/**
* @param string $name
* @return bool
*/
public function hasHeader($name): bool
{
// TODO: Implement hasHeader() method.
return isset($this->headers[$name]);
}
/**
* @param string $name
* @return mixed
*/
public function getHeader($name): mixed
{
// TODO: Implement getHeader() method.
if (!$this->hasHeader($name)) {
return null;
}
return $this->headers[$name];
}
/**
* @param string $name
* @return string|null
*/
public function getHeaderLine($name): ?string
{
// TODO: Implement getHeaderLine() method.
// TODO: Implement getHeader() method.
if (!$this->hasHeader($name)) {
return null;
}
return $this->headers[$name];
}
/**
* @param string $name
* @param string|string[] $value
* @return Message
*/
public function withHeader($name, $value): static
{
// TODO: Implement withHeader() method.
$newInstance = clone $this;
$newInstance->headers[$name] = [$value];
return $newInstance;
}
/**
* @param string $name
* @param string|string[] $value
* @return $this
*/
public function withAddedHeader($name, $value): static
{
// TODO: Implement withAddedHeader() method.
// TODO: Implement withHeader() method.
$newInstance = clone $this;
if (!isset($newInstance->headers)) {
$newInstance->headers[$name] = [$value];
} else {
$newInstance->headers[$name][] = $value;
}
return $newInstance;
}
/**
* @param string $name
* @return $this
*/
public function withoutHeader($name): static
{
// TODO: Implement withoutHeader() method.
$newInstance = clone $this;
if (!isset($newInstance->headers)) {
return $newInstance;
}
unset($newInstance->headers[$name]);
return $newInstance;
}
/**
* @return mixed
*/
public function getBody(): mixed
{
// TODO: Implement getBody() method.
return $this->body;
}
/**
* @param StreamInterface $body
* @return $this
*/
public function withBody(StreamInterface $body): static
{
// TODO: Implement withBody() method.
$newInstance = clone $this;
$newInstance->body = $body;
return $newInstance;
}
}
+113
View File
@@ -0,0 +1,113 @@
<?php
namespace HttpMessage;
use Annotation\Inject;
use JetBrains\PhpStorm\Pure;
use Kiri\Di\ContainerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
/**
*
*/
class Request implements RequestInterface
{
use Message;
public string $method;
public mixed $requestTarget;
/**
* @var UriInterface
*/
#[Inject(UriInterface::class)]
public UriInterface $uri;
/**
* @param ContainerInterface $container
*/
public function __construct(public ContainerInterface $container)
{
}
/**
* @return mixed
*/
public function getRequestTarget(): mixed
{
// TODO: Implement getRequestTarget() method.
return $this->requestTarget;
}
/**
* @param mixed $requestTarget
* @return Request
*/
public function withRequestTarget($requestTarget): static
{
// TODO: Implement withRequestTarget() method.
$newInstance = clone $this;
$newInstance->requestTarget = $requestTarget;
return $newInstance;
}
/**
* @return string
*/
public function getMethod(): string
{
// TODO: Implement getMethod() method.
return $this->method;
}
/**
* @param string $method
* @return $this
*/
public function withMethod($method): static
{
// TODO: Implement withMethod() method.
$newInstance = clone $this;
$newInstance->method = $method;
return $newInstance;
}
/**
* @return string
*/
#[Pure] public function getUri(): string
{
// TODO: Implement getUri() method.
$uri = $this->uri->getPath();
if (!empty($this->uri->getQuery())) {
$uri .= '?' . $this->uri->getQuery();
}
return $uri;
}
/**
* @param UriInterface $uri
* @param false $preserveHost
* @return Request
*/
public function withUri(UriInterface $uri, $preserveHost = false): static
{
// TODO: Implement withUri() method.
$newInstance = clone $this;
$newInstance->uri = $uri;
return $newInstance;
}
}
+162
View File
@@ -0,0 +1,162 @@
<?php
namespace HttpMessage;
use Psr\Http\Message\UriInterface;
/**
*
*/
class Uri implements UriInterface
{
/**
* @var string
*/
public string $path = '';
/**
* @var string
*/
public string $host = '';
/**
* @var int
*/
public int $port = 0;
/**
* @var string
*/
public string $query = '';
/**
* @var string
*/
public string $scheme = '';
/**
* @return string
*/
public function getScheme(): string
{
// TODO: Implement getScheme() method.
return $this->scheme;
}
/**
* @return string
*/
public function getAuthority(): string
{
// TODO: Implement getAuthority() method.
return '';
}
public function getUserInfo()
{
// TODO: Implement getUserInfo() method.
}
/**
* @return string
*/
public function getHost(): string
{
// TODO: Implement getHost() method.
return $this->host;
}
/**
* @return int
*/
public function getPort(): int
{
// TODO: Implement getPort() method.
return $this->port;
}
/**
* @return string
*/
public function getPath(): string
{
// TODO: Implement getPath() method.
return $this->path;
}
/**
* @return string
*/
public function getQuery(): string
{
// TODO: Implement getQuery() method.
return $this->query;
}
/**
* @return string
*/
public function getFragment(): string
{
// TODO: Implement getFragment() method.
return $this->path;
}
/**
* @param string $scheme
* @return Uri|void
*/
public function withScheme($scheme): string
{
// TODO: Implement withScheme() method.
}
public function withUserInfo($user, $password = null)
{
// TODO: Implement withUserInfo() method.
}
public function withHost($host)
{
// TODO: Implement withHost() method.
}
public function withPort($port)
{
// TODO: Implement withPort() method.
}
public function withPath($path)
{
// TODO: Implement withPath() method.
}
public function withQuery($query)
{
// TODO: Implement withQuery() method.
}
public function withFragment($fragment)
{
// TODO: Implement withFragment() method.
}
public function __toString()
{
// TODO: Implement __toString() method.
}
}