This commit is contained in:
2021-09-10 10:24:11 +08:00
parent c02da89654
commit dca5d3e6ec
8 changed files with 955 additions and 957 deletions
+168 -170
View File
@@ -3,7 +3,6 @@
namespace Protocol\Message; namespace Protocol\Message;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Core\Xml;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
@@ -13,211 +12,210 @@ use Psr\Http\Message\StreamInterface;
trait Message trait Message
{ {
/** /**
* @var string * @var string
*/ */
protected string $version; protected string $version;
/** /**
* @var \Psr\Http\Message\StreamInterface * @var StreamInterface
*/ */
protected StreamInterface $stream; protected StreamInterface $stream;
/** /**
* @var array * @var array
*/ */
protected array $headers = []; protected array $headers = [];
/** /**
* @return string * @return string
*/ */
public function getProtocolVersion(): string public function getProtocolVersion(): string
{ {
return $this->version; return $this->version;
} }
/** /**
* @param $version * @param $version
* @return static * @return static
*/ */
public function withProtocolVersion($version): static public function withProtocolVersion($version): static
{ {
$this->version = $version; $this->version = $version;
return $this; return $this;
} }
/** /**
* @return array * @return array
*/ */
public function getHeaders(): array public function getHeaders(): array
{ {
return $this->headers; return $this->headers;
} }
/** /**
* @param $name * @param $name
* @return bool * @return bool
*/ */
public function hasHeader($name): bool public function hasHeader($name): bool
{ {
return array_key_exists($name, $this->headers); return array_key_exists($name, $this->headers);
} }
/** /**
* @param $name * @param $name
* @return string|array|null * @return string|array|null
*/ */
#[Pure] public function getHeader($name): string|null|array #[Pure] public function getHeader($name): string|null|array
{ {
if (!$this->hasHeader($name)) { if (!$this->hasHeader($name)) {
return null; return null;
} }
return $this->headers[$name]; return $this->headers[$name];
} }
/** /**
* @param \Swoole\Http\Request $request * @param \Swoole\Http\Request $request
* @return static * @return static
*/ */
public function parseRequestHeaders(\Swoole\Http\Request $request): static public function parseRequestHeaders(\Swoole\Http\Request $request): static
{ {
$index = strpos($request->getData(), "\r\n\r\n"); $index = strpos($request->getData(), "\r\n\r\n");
$headers = explode("\r\n", substr($request->getData(), 0, $index)); $headers = explode("\r\n", substr($request->getData(), 0, $index));
array_shift($headers); array_shift($headers);
foreach ($headers as $header) { foreach ($headers as $header) {
[$key, $value] = explode(': ', $header); [$key, $value] = explode(': ', $header);
$this->addRequestHeader($key, $value); $this->addRequestHeader($key, $value);
} }
return $this; return $this;
} }
/** /**
* @param $key * @param $key
* @param $value * @param $value
*/ */
private function addRequestHeader($key, $value) private function addRequestHeader($key, $value)
{ {
$this->headers[$key] = [$value]; $this->headers[$key] = [$value];
} }
/** /**
* @param $name * @param $name
* @return string|null * @return string|null
*/ */
#[Pure] public function getHeaderLine($name): string|null #[Pure] public function getHeaderLine($name): string|null
{ {
if ($this->hasHeader($name)) { if ($this->hasHeader($name)) {
return implode(';', $this->headers[$name]); return implode(';', $this->headers[$name]);
} }
return null; return null;
} }
/** /**
* @return string|null * @return string|null
*/ */
#[Pure] public function getContentType(): ?string #[Pure] public function getContentType(): ?string
{ {
return $this->getHeaderLine('Content-Type'); return $this->getHeaderLine('Content-Type');
} }
/** /**
* @param $name * @param $name
* @param $value * @param $value
* @return static * @return static
*/ */
public function withHeader($name, $value): static public function withHeader($name, $value): static
{ {
if (!is_array($value)) { if (!is_array($value)) {
$value = [$value]; $value = [$value];
} }
$this->headers[$name] = $value; $this->headers[$name] = $value;
return $this; return $this;
} }
/** /**
* @param $name * @param $name
* @param $value * @param $value
* @return static * @return static
* @throws * @throws
*/ */
public function withAddedHeader($name, $value): static public function withAddedHeader($name, $value): static
{ {
if (!array_key_exists($name, $this->headers)) { if (!array_key_exists($name, $this->headers)) {
throw new \Exception('Headers `' . $name . '` not exists.'); throw new \Exception('Headers `' . $name . '` not exists.');
} }
$this->headers[$name][] = $value; $this->headers[$name][] = $value;
return $this; return $this;
} }
/** /**
* @param $name * @param $name
* @return static * @return static
*/ */
public function withoutHeader($name): static public function withoutHeader($name): static
{ {
unset($this->headers[$name]); unset($this->headers[$name]);
return $this; return $this;
} }
/** /**
* @return string * @return string
*/ */
public function getBody(): string public function getBody(): string
{ {
return $this->stream->getContents(); return $this->stream->getContents();
} }
/** /**
* @param StreamInterface $body * @param StreamInterface $body
* @return static * @return static
*/ */
public function withBody(StreamInterface $body): static public function withBody(StreamInterface $body): static
{ {
$this->stream = $body; $this->stream = $body;
return $this; return $this;
} }
/**
/** * @return string|null
* @return string|null */
*/ #[Pure] public function getAccessControlAllowOrigin(): ?string
#[Pure] public function getAccessControlAllowOrigin(): ?string {
{ return $this->getHeaderLine('Access-Control-Allow-Origin');
return $this->getHeaderLine('Access-Control-Allow-Origin'); }
}
/** /**
* @return string|null * @return string|null
*/ */
#[Pure] public function getAccessControlAllowHeaders(): ?string #[Pure] public function getAccessControlAllowHeaders(): ?string
{ {
return $this->getHeaderLine('Access-Control-Allow-Headers'); return $this->getHeaderLine('Access-Control-Allow-Headers');
} }
/** /**
* @return string|null * @return string|null
*/ */
#[Pure] public function getAccessControlRequestMethod(): ?string #[Pure] public function getAccessControlRequestMethod(): ?string
{ {
return $this->getHeaderLine('Access-Control-Request-Method'); return $this->getHeaderLine('Access-Control-Request-Method');
} }
} }
+22 -22
View File
@@ -8,27 +8,27 @@ class Parse
{ {
/** /**
* @param $content * @param $content
* @param $contentType * @param $contentType
* @return mixed * @return mixed
*/ */
public static function data($content, $contentType): mixed public static function data($content, $contentType): mixed
{ {
if (str_contains($contentType, 'json')) { if (str_contains($contentType, 'json')) {
return json_encode($contentType); return json_encode($contentType);
} }
if (str_contains($contentType, 'xml')) { if (str_contains($contentType, 'xml')) {
return Xml::toArray($contentType); return Xml::toArray($contentType);
} }
if (str_contains($contentType, 'x-www-form-urlencoded')) { if (str_contains($contentType, 'x-www-form-urlencoded')) {
parse_str($content, $array); parse_str($content, $array);
return $array; return $array;
} }
if (str_contains($contentType, 'serialize')) { if (str_contains($contentType, 'serialize')) {
return unserialize($content); return unserialize($content);
} }
return $content; return $content;
} }
} }
+76 -76
View File
@@ -14,102 +14,102 @@ use Psr\Http\Message\UriInterface;
abstract class Request implements RequestInterface abstract class Request implements RequestInterface
{ {
use Message; use Message;
/** /**
* @var \Psr\Http\Message\UriInterface * @var UriInterface
*/ */
protected UriInterface $uriInterface; protected UriInterface $uriInterface;
/** /**
* @var string * @var string
*/ */
protected string $method; protected string $method;
/** /**
* @var \Http\IInterface\AuthIdentity * @var AuthIdentity|null
*/ */
public AuthIdentity $authority; public ?AuthIdentity $authority = null;
/** /**
* @param \Http\IInterface\AuthIdentity $authIdentity * @param AuthIdentity|null $authIdentity
*/ */
public function setAuthority(AuthIdentity $authIdentity): void public function setAuthority(?AuthIdentity $authIdentity): void
{ {
$this->authority = $authIdentity; $this->authority = $authIdentity;
} }
/** /**
* @return string * @return string
*/ */
public function getRequestTarget(): string public function getRequestTarget(): string
{ {
throw new BadMethodCallException('Not Accomplish Method.'); throw new BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @param mixed $requestTarget * @param mixed $requestTarget
* @return static * @return static
*/ */
public function withRequestTarget($requestTarget): static public function withRequestTarget($requestTarget): static
{ {
throw new BadMethodCallException('Not Accomplish Method.'); throw new BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @return string * @return string
*/ */
public function getMethod(): string public function getMethod(): string
{ {
return $this->method; return $this->method;
} }
/** /**
* @param string $method * @param string $method
* @return RequestInterface * @return RequestInterface
*/ */
public function withMethod($method): RequestInterface public function withMethod($method): RequestInterface
{ {
$this->method = $method; $this->method = $method;
return $this; return $this;
} }
/** /**
* @param string $method * @param string $method
* @return bool * @return bool
*/ */
public function isMethod(string $method): bool public function isMethod(string $method): bool
{ {
return $this->method == $method; return $this->method == $method;
} }
/** /**
* @return UriInterface * @return UriInterface
*/ */
public function getUri(): UriInterface public function getUri(): UriInterface
{ {
return $this->uriInterface; return $this->uriInterface;
} }
/** /**
* @param UriInterface $uri * @param UriInterface $uri
* @param false $preserveHost * @param false $preserveHost
* @return $this|Request * @return $this|Request
*/ */
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
{ {
$this->uriInterface = $uri; $this->uriInterface = $uri;
return $this; return $this;
} }
} }
+49 -49
View File
@@ -10,69 +10,69 @@ class Response implements ResponseInterface
{ {
use Message; use Message;
protected int $statusCode = 200; protected int $statusCode = 200;
protected string $reasonPhrase = ''; protected string $reasonPhrase = '';
/** /**
* @return int * @return int
*/ */
public function getStatusCode(): int public function getStatusCode(): int
{ {
return $this->statusCode; return $this->statusCode;
} }
/** /**
* @param int $code * @param int $code
* @param string $reasonPhrase * @param string $reasonPhrase
* @return $this|\Protocol\Message\Response * @return $this|Response
*/ */
public function withStatus($code, $reasonPhrase = ''): static public function withStatus($code, $reasonPhrase = ''): static
{ {
$this->statusCode = $code; $this->statusCode = $code;
$this->reasonPhrase = $reasonPhrase; $this->reasonPhrase = $reasonPhrase;
return $this; return $this;
} }
/** /**
* @return string * @return string
*/ */
public function getReasonPhrase(): string public function getReasonPhrase(): string
{ {
return $this->reasonPhrase; return $this->reasonPhrase;
} }
/** /**
* @return string|null * @return string|null
*/ */
#[Pure] public function getAccessControlAllowOrigin(): ?string #[Pure] public function getAccessControlAllowOrigin(): ?string
{ {
return $this->getHeaderLine('Access-Control-Allow-Origin'); return $this->getHeaderLine('Access-Control-Allow-Origin');
} }
/** /**
* @return string|null * @return string|null
*/ */
#[Pure] public function getAccessControlAllowHeaders(): ?string #[Pure] public function getAccessControlAllowHeaders(): ?string
{ {
return $this->getHeaderLine('Access-Control-Allow-Headers'); return $this->getHeaderLine('Access-Control-Allow-Headers');
} }
/** /**
* @return string|null * @return string|null
*/ */
#[Pure] public function getAccessControlRequestMethod(): ?string #[Pure] public function getAccessControlRequestMethod(): ?string
{ {
return $this->getHeaderLine('Access-Control-Request-Method'); return $this->getHeaderLine('Access-Control-Request-Method');
} }
} }
+173 -173
View File
@@ -14,217 +14,217 @@ class ServerRequest extends Request implements ServerRequestInterface
{ {
/** /**
* @var mixed * @var mixed
*/ */
protected ?array $parsedBody = null; protected ?array $parsedBody = null;
/** /**
* @var array|null * @var array|null
*/ */
protected ?array $serverParams; protected ?array $serverParams;
/** /**
* @var array|null * @var array|null
*/ */
protected ?array $cookieParams = []; protected ?array $cookieParams = [];
/** /**
* @var array|null * @var array|null
*/ */
protected ?array $queryParams; protected ?array $queryParams;
/** /**
* @var array|null * @var array|null
*/ */
protected ?array $uploadedFiles; protected ?array $uploadedFiles;
protected \Swoole\Http\Request $serverTarget; protected \Swoole\Http\Request $serverTarget;
/** /**
* @param array $server * @param array $server
* @return static * @return static
*/ */
public function withServerParams(array $server): static public function withServerParams(array $server): static
{ {
$this->serverParams = $server; $this->serverParams = $server;
return $this; return $this;
} }
/** /**
* @param \Swoole\Http\Request $server * @param \Swoole\Http\Request $server
* @return static * @return static
*/ */
public function withServerTarget(\Swoole\Http\Request $server): static public function withServerTarget(\Swoole\Http\Request $server): static
{ {
$this->serverTarget = $server; $this->serverTarget = $server;
return $this; return $this;
} }
/** /**
* @param \Swoole\Http\Request $request * @param \Swoole\Http\Request $request
* @return static * @return static
*/ */
public static function createServerRequest(\Swoole\Http\Request $request): static public static function createServerRequest(\Swoole\Http\Request $request): static
{ {
return (new static())->withServerParams($request->server) return (new static())->withServerParams($request->server)
->withServerTarget($request) ->withServerTarget($request)
->withCookieParams($request->cookie) ->withCookieParams($request->cookie)
->withUri(Uri::parseUri($request)) ->withUri(Uri::parseUri($request))
->withQueryParams($request->get ?? []) ->withQueryParams($request->get ?? [])
->withUploadedFiles($request->files ?? []) ->withUploadedFiles($request->files ?? [])
->withMethod($request->getMethod()) ->withMethod($request->getMethod())
->parseRequestHeaders($request) ->parseRequestHeaders($request)
->withParsedBody(function (StreamInterface $stream, ?array $posts) { ->withParsedBody(function (StreamInterface $stream, ?array $posts) {
try { try {
$content = $stream->getContents(); $content = $stream->getContents();
if (!empty($content)) { if (!empty($content)) {
return Parse::data($content, $this->getContentType()); return Parse::data($content, $this->getContentType());
} }
return $posts; return $posts;
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
return $posts; return $posts;
} }
}); });
} }
/** /**
* @return null|array * @return null|array
*/ */
public function getServerParams(): ?array public function getServerParams(): ?array
{ {
return $this->serverParams; return $this->serverParams;
} }
/** /**
* @return null|array * @return null|array
*/ */
public function getCookieParams(): ?array public function getCookieParams(): ?array
{ {
return $this->cookieParams; return $this->cookieParams;
} }
/** /**
* @param array|null $cookies * @param array|null $cookies
* @return $this|ServerRequest * @return $this|ServerRequest
*/ */
public function withCookieParams(?array $cookies): ServerRequestInterface public function withCookieParams(?array $cookies): ServerRequestInterface
{ {
$this->cookieParams = $cookies; $this->cookieParams = $cookies;
return $this; return $this;
} }
/** /**
* @return array|null * @return array|null
*/ */
public function getQueryParams(): ?array public function getQueryParams(): ?array
{ {
return $this->queryParams; return $this->queryParams;
} }
/** /**
* @param array $query * @param array $query
* @return \Psr\Http\Message\ServerRequestInterface * @return \Psr\Http\Message\ServerRequestInterface
*/ */
public function withQueryParams(array $query): ServerRequestInterface public function withQueryParams(array $query): ServerRequestInterface
{ {
$this->queryParams = $query; $this->queryParams = $query;
return $this; return $this;
} }
/** /**
* @return array|null * @return array|null
*/ */
public function getUploadedFiles(): ?array public function getUploadedFiles(): ?array
{ {
return $this->uploadedFiles; return $this->uploadedFiles;
} }
/** /**
* @param array $uploadedFiles * @param array $uploadedFiles
* @return \Psr\Http\Message\ServerRequestInterface * @return \Psr\Http\Message\ServerRequestInterface
*/ */
public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
{ {
$this->uploadedFiles = $uploadedFiles; $this->uploadedFiles = $uploadedFiles;
return $this; return $this;
} }
/** /**
* @return array|object|null * @return array|object|null
*/ */
public function getParsedBody(): object|array|null public function getParsedBody(): object|array|null
{ {
if (empty($this->parsedBody)) { if (empty($this->parsedBody)) {
$callback = Context::getContext('with.parsed.body.callback'); $callback = Context::getContext('with.parsed.body.callback');
$this->parsedBody = $callback($this->getBody()); $this->parsedBody = $callback($this->getBody());
} }
return $this->parsedBody; return $this->parsedBody;
} }
/** /**
* @param array|object|null $data * @param array|object|null $data
* @return ServerRequestInterface * @return ServerRequestInterface
*/ */
public function withParsedBody($data): ServerRequestInterface public function withParsedBody($data): ServerRequestInterface
{ {
Context::setContext('with.parsed.body.callback', $data); Context::setContext('with.parsed.body.callback', $data);
return $this; return $this;
} }
/** /**
* @return array * @return array
*/ */
public function getAttributes(): array public function getAttributes(): array
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @param string $name * @param string $name
* @param null $default * @param null $default
* @return mixed * @return mixed
*/ */
public function getAttribute($name, $default = null): mixed public function getAttribute($name, $default = null): mixed
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
* @return ServerRequestInterface * @return ServerRequestInterface
*/ */
public function withAttribute($name, $value): ServerRequestInterface public function withAttribute($name, $value): ServerRequestInterface
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @param string $name * @param string $name
* @return ServerRequestInterface * @return ServerRequestInterface
*/ */
public function withoutAttribute($name): ServerRequestInterface public function withoutAttribute($name): ServerRequestInterface
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
} }
+190 -190
View File
@@ -10,228 +10,228 @@ class Stream implements StreamInterface
{ {
/** /**
* @var string|resource * @var string|resource
*/ */
private mixed $content = ''; private mixed $content = '';
/** /**
* @var int * @var int
*/ */
private int $size = 0; private int $size = 0;
/** /**
* @param mixed $stream * @param mixed $stream
*/ */
public function __construct(mixed $stream = '') public function __construct(mixed $stream = '')
{ {
$this->content = $stream; $this->content = $stream;
if (!is_resource($stream)) { if (!is_resource($stream)) {
$this->size = strlen($stream); $this->size = strlen($stream);
} else { } else {
$state = fstat($this->content); $state = fstat($this->content);
if ($state) { if ($state) {
$this->size = $state['size']; $this->size = $state['size'];
} }
} }
} }
/** /**
* @return string * @return string
*/ */
public function __toString() public function __toString()
{ {
return $this->content; return $this->content;
} }
/** /**
* *
*/ */
public function close() public function close()
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @return resource|null * @return resource|null
*/ */
public function detach() public function detach()
{ {
if (!is_resource($this->content)) { if (!is_resource($this->content)) {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
$steam = stream_context_create(); $steam = stream_context_create();
stream_copy_to_stream($this->content, $steam); stream_copy_to_stream($this->content, $steam);
return $steam; return $steam;
} }
/** /**
* @return int * @return int
*/ */
public function getSize(): int public function getSize(): int
{ {
return $this->size; return $this->size;
} }
/** /**
* @return bool|int * @return bool|int
*/ */
public function tell(): bool|int public function tell(): bool|int
{ {
if (!is_resource($this->content)) { if (!is_resource($this->content)) {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
return ftell($this->content); return ftell($this->content);
} }
/** /**
* @return bool * @return bool
*/ */
public function eof(): bool public function eof(): bool
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @return bool * @return bool
*/ */
public function isSeekable(): bool public function isSeekable(): bool
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @param int $offset * @param int $offset
* @param int $whence * @param int $whence
*/ */
public function seek($offset, $whence = SEEK_SET) public function seek($offset, $whence = SEEK_SET)
{ {
if (!is_resource($this->content)) { if (!is_resource($this->content)) {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
fseek($this->content, $offset, $whence); fseek($this->content, $offset, $whence);
} }
/** /**
* *
*/ */
public function rewind() public function rewind()
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @return bool * @return bool
*/ */
public function isWritable(): bool public function isWritable(): bool
{ {
if (!is_resource($this->content)) { if (!is_resource($this->content)) {
return true; return true;
} }
if (is_writable($this->content)) { if (is_writable($this->content)) {
return true; return true;
} }
return false; return false;
} }
/** /**
* @param string $string * @param string $string
* @return int * @return int
*/ */
public function write($string): int public function write($string): int
{ {
if (is_resource($this->content)) { if (is_resource($this->content)) {
fwrite($this->content, $string); fwrite($this->content, $string);
$state = fstat($this->content); $state = fstat($this->content);
if ($state) { if ($state) {
$this->size = $state['size']; $this->size = $state['size'];
} }
} else { } else {
$this->content = $string; $this->content = $string;
$this->size = strlen($string); $this->size = strlen($string);
} }
return $this->size; return $this->size;
} }
/** /**
* @return bool * @return bool
*/ */
public function isReadable(): bool public function isReadable(): bool
{ {
if (!is_resource($this->content)) { if (!is_resource($this->content)) {
return true; return true;
} }
if (is_readable($this->content)) { if (is_readable($this->content)) {
return true; return true;
} }
return false; return false;
} }
/** /**
* @param int $length * @param int $length
* @return false|string * @return false|string
*/ */
public function read($length): bool|string public function read($length): bool|string
{ {
if (is_resource($this->content)) { if (is_resource($this->content)) {
return fread($this->content, $length); return fread($this->content, $length);
} else { } else {
return $this->content; return $this->content;
} }
} }
/** /**
* @return string|bool * @return string|bool
*/ */
public function getContents(): string|bool public function getContents(): string|bool
{ {
if (is_resource($this->content)) { if (is_resource($this->content)) {
return stream_get_contents($this->content); return stream_get_contents($this->content);
} else { } else {
return $this->content; return $this->content;
} }
} }
/** /**
* @param null $key * @param null $key
* @return array * @return array
*/ */
#[ArrayShape([ #[ArrayShape([
"timed_out" => "bool", "timed_out" => "bool",
"blocked" => "bool", "blocked" => "bool",
"eof" => "bool", "eof" => "bool",
"unread_bytes" => "int", "unread_bytes" => "int",
"stream_type" => "string", "stream_type" => "string",
"wrapper_type" => "string", "wrapper_type" => "string",
"wrapper_data" => "mixed", "wrapper_data" => "mixed",
"mode" => "string", "mode" => "string",
"seekable" => "bool", "seekable" => "bool",
"uri" => "string", "uri" => "string",
"crypto" => "array", "crypto" => "array",
"mediatype" => "string", "mediatype" => "string",
])] ])]
public function getMetadata($key = null): array public function getMetadata($key = null): array
{ {
if (is_resource($this->content)) { if (is_resource($this->content)) {
return stream_get_meta_data($this->content); return stream_get_meta_data($this->content);
} }
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
} }
+77 -77
View File
@@ -9,94 +9,94 @@ use Psr\Http\Message\UploadedFileInterface;
class Uploaded implements UploadedFileInterface class Uploaded implements UploadedFileInterface
{ {
const ERROR = [ const ERROR = [
0 => "There is no error, the file uploaded with success", 0 => "There is no error, the file uploaded with success",
1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini", 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3 => "The uploaded file was only partially uploaded", 3 => "The uploaded file was only partially uploaded",
4 => "No file was uploaded", 4 => "No file was uploaded",
6 => "Missing a temporary folder" 6 => "Missing a temporary folder"
]; ];
/** /**
* @param string $tmp_name * @param string $tmp_name
* @param string $name * @param string $name
* @param string $type * @param string $type
* @param int $size * @param int $size
* @param int $error * @param int $error
*/ */
public function __construct( public function __construct(
public string $tmp_name, public string $tmp_name,
public string $name, public string $name,
public string $type, public string $type,
public int $size, public int $size,
public int $error public int $error
) )
{ {
} }
/** /**
* @return StreamInterface * @return StreamInterface
* @throws \Exception * @throws \Exception
*/ */
public function getStream(): StreamInterface public function getStream(): StreamInterface
{ {
if (!fopen($this->tmp_name, 'r')) { if (!fopen($this->tmp_name, 'r')) {
throw new \Exception('The file "' . $this->name . '" con\'t readable.'); throw new \Exception('The file "' . $this->name . '" con\'t readable.');
} }
return new Stream(fopen($this->tmp_name, 'r')); return new Stream(fopen($this->tmp_name, 'r'));
} }
/** /**
* @param string $targetPath * @param string $targetPath
* @return \Psr\Http\Message\StreamInterface * @return \Psr\Http\Message\StreamInterface
* @throws \Exception * @throws \Exception
*/ */
public function moveTo($targetPath): StreamInterface public function moveTo($targetPath): StreamInterface
{ {
@move_uploaded_file($this->tmp_name, $targetPath); @move_uploaded_file($this->tmp_name, $targetPath);
if (!file_exists($targetPath)) { if (!file_exists($targetPath)) {
throw new \Exception('File save fail.'); throw new \Exception('File save fail.');
} }
$this->tmp_name = $targetPath; $this->tmp_name = $targetPath;
return $this->getStream(); return $this->getStream();
} }
/** /**
* @return int * @return int
*/ */
public function getSize(): int public function getSize(): int
{ {
return $this->size; return $this->size;
} }
/** /**
* @return int * @return int
*/ */
public function getError(): int public function getError(): int
{ {
return $this->error; return $this->error;
} }
/** /**
* @return string * @return string
*/ */
public function getClientFilename(): string public function getClientFilename(): string
{ {
return $this->name; return $this->name;
} }
/** /**
* @return string * @return string
*/ */
public function getClientMediaType(): string public function getClientMediaType(): string
{ {
return $this->type; return $this->type;
} }
} }
+200 -200
View File
@@ -10,246 +10,246 @@ class Uri implements UriInterface
{ {
protected string $scheme = ''; protected string $scheme = '';
protected string $host = ''; protected string $host = '';
protected string $path = ''; protected string $path = '';
protected string $fragment = ''; protected string $fragment = '';
protected int $port = 80; protected int $port = 80;
protected string $queryString = ''; protected string $queryString = '';
/** /**
* @return string * @return string
*/ */
public function getScheme(): string public function getScheme(): string
{ {
return $this->scheme; return $this->scheme;
} }
/** /**
* @return string * @return string
*/ */
public function getAuthority(): string public function getAuthority(): string
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @return string * @return string
*/ */
public function getUserInfo(): string public function getUserInfo(): string
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @return string * @return string
*/ */
public function getHost(): string public function getHost(): string
{ {
return $this->host; return $this->host;
} }
/** /**
* @return int|null * @return int|null
*/ */
public function getPort(): ?int public function getPort(): ?int
{ {
return $this->port; return $this->port;
} }
/** /**
* @return string * @return string
*/ */
public function getPath(): string public function getPath(): string
{ {
return $this->path; return $this->path;
} }
/** /**
* @return string * @return string
*/ */
public function getQuery(): string public function getQuery(): string
{ {
return $this->queryString; return $this->queryString;
} }
/** /**
* @return string * @return string
*/ */
public function getFragment(): string public function getFragment(): string
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @param string $scheme * @param string $scheme
* @return $this|\Protocol\Message\Uri * @return $this|Uri
*/ */
public function withScheme($scheme): UriInterface public function withScheme($scheme): UriInterface
{ {
$this->scheme = $scheme; $this->scheme = $scheme;
return $this; return $this;
} }
/** /**
* @param string $user * @param string $user
* @param null $password * @param null $password
* @return \Protocol\Message\Uri * @return Uri
*/ */
public function withUserInfo($user, $password = null): UriInterface public function withUserInfo($user, $password = null): UriInterface
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @param string $host * @param string $host
* @return $this|\Protocol\Message\Uri * @return $this|Uri
*/ */
public function withHost($host): UriInterface public function withHost($host): UriInterface
{ {
$this->host = $host; $this->host = $host;
return $this; return $this;
} }
/** /**
* @param int|null $port * @param int|null $port
* @return $this|\Protocol\Message\Uri * @return $this|Uri
*/ */
public function withPort($port): UriInterface public function withPort($port): UriInterface
{ {
$this->port = $port; $this->port = $port;
return $this; return $this;
} }
/** /**
* @param string $path * @param string $path
* @return $this|\Protocol\Message\Uri * @return $this|Uri
*/ */
public function withPath($path): UriInterface public function withPath($path): UriInterface
{ {
$this->path = $path; $this->path = $path;
return $this; return $this;
} }
/** /**
* @param string $query * @param string $query
* @return $this|\Protocol\Message\Uri * @return $this|Uri
*/ */
public function withQuery($query): UriInterface public function withQuery($query): UriInterface
{ {
$this->queryString = $query; $this->queryString = $query;
return $this; return $this;
} }
/** /**
* @param string $fragment * @param string $fragment
* @return \Protocol\Message\Uri * @return Uri
*/ */
public function withFragment($fragment): UriInterface public function withFragment($fragment): UriInterface
{ {
throw new \BadMethodCallException('Not Accomplish Method.'); throw new \BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @return string * @return string
*/ */
public function __toString(): string public function __toString(): string
{ {
$domain = sprintf('%s://%s', $this->scheme, $this->host); $domain = sprintf('%s://%s', $this->scheme, $this->host);
if (!in_array($this->port, [80, 443])) { if (!in_array($this->port, [80, 443])) {
$domain .= ':' . $this->port; $domain .= ':' . $this->port;
} }
if (empty($this->query) && empty($this->fragment)) { if (empty($this->query) && empty($this->fragment)) {
return $domain . $this->path; return $domain . $this->path;
} }
return sprintf('%s?%s#%s', $domain . $this->path, return sprintf('%s?%s#%s', $domain . $this->path,
$this->queryString, $this->fragment); $this->queryString, $this->fragment);
} }
/** /**
* @return int * @return int
*/ */
public function getDefaultPort(): int public function getDefaultPort(): int
{ {
return $this->scheme == 'https' ? 443 : 80; return $this->scheme == 'https' ? 443 : 80;
} }
/** /**
* @param \Swoole\Http\Request $request * @param Request $request
* @return UriInterface * @return UriInterface
*/ */
public static function parseUri(Request $request): UriInterface public static function parseUri(Request $request): UriInterface
{ {
$server = $request->server; $server = $request->server;
$header = $request->header; $header = $request->header;
$uri = new static(); $uri = new static();
$uri = $uri->withScheme(!empty($server['https']) && $server['https'] !== 'off' ? 'https' : 'http'); $uri = $uri->withScheme(!empty($server['https']) && $server['https'] !== 'off' ? 'https' : 'http');
if (isset($request->header['x-forwarded-proto'])) { if (isset($request->header['x-forwarded-proto'])) {
$uri->withScheme($request->header['x-forwarded-proto'])->withPort(443); $uri->withScheme($request->header['x-forwarded-proto'])->withPort(443);
} }
$hasPort = false; $hasPort = false;
if (isset($server['http_host'])) { if (isset($server['http_host'])) {
$hostHeaderParts = explode(':', $server['http_host']); $hostHeaderParts = explode(':', $server['http_host']);
$uri = $uri->withHost($hostHeaderParts[0]); $uri = $uri->withHost($hostHeaderParts[0]);
if (isset($hostHeaderParts[1])) { if (isset($hostHeaderParts[1])) {
$hasPort = true; $hasPort = true;
$uri = $uri->withPort($hostHeaderParts[1]); $uri = $uri->withPort($hostHeaderParts[1]);
} }
} elseif (isset($server['server_name'])) { } elseif (isset($server['server_name'])) {
$uri = $uri->withHost($server['server_name']); $uri = $uri->withHost($server['server_name']);
} elseif (isset($server['server_addr'])) { } elseif (isset($server['server_addr'])) {
$uri = $uri->withHost($server['server_addr']); $uri = $uri->withHost($server['server_addr']);
} elseif (isset($header['host'])) { } elseif (isset($header['host'])) {
$hasPort = true; $hasPort = true;
if (strpos($header['host'], ':')) { if (strpos($header['host'], ':')) {
[$host, $port] = explode(':', $header['host'], 2); [$host, $port] = explode(':', $header['host'], 2);
if ($port != $uri->getDefaultPort()) { if ($port != $uri->getDefaultPort()) {
$uri = $uri->withPort($port); $uri = $uri->withPort($port);
} }
} else { } else {
$host = $header['host']; $host = $header['host'];
} }
$uri = $uri->withHost($host); $uri = $uri->withHost($host);
} }
if (!$hasPort && isset($server['server_port'])) { if (!$hasPort && isset($server['server_port'])) {
$uri = $uri->withPort($server['server_port']); $uri = $uri->withPort($server['server_port']);
} }
$hasQuery = false; $hasQuery = false;
if (isset($server['request_uri'])) { if (isset($server['request_uri'])) {
$requestUriParts = explode('?', $server['request_uri']); $requestUriParts = explode('?', $server['request_uri']);
$uri = $uri->withPath($requestUriParts[0]); $uri = $uri->withPath($requestUriParts[0]);
if (isset($requestUriParts[1])) { if (isset($requestUriParts[1])) {
$hasQuery = true; $hasQuery = true;
$uri = $uri->withQuery($requestUriParts[1]); $uri = $uri->withQuery($requestUriParts[1]);
} }
} }
if (!$hasQuery && isset($server['query_string'])) { if (!$hasQuery && isset($server['query_string'])) {
$uri = $uri->withQuery($server['query_string']); $uri = $uri->withQuery($server['query_string']);
} }
return $uri; return $uri;
} }
} }