111
This commit is contained in:
+240
-171
@@ -15,226 +15,295 @@ use Psr\Http\Message\UriInterface;
|
|||||||
class Request implements RequestInterface
|
class Request implements RequestInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
use Message;
|
use Message;
|
||||||
|
|
||||||
|
|
||||||
public string $requestTarget;
|
public string $requestTarget;
|
||||||
|
|
||||||
|
|
||||||
public string $method;
|
public string $method;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Uri|UriInterface
|
* @var Uri|UriInterface
|
||||||
*/
|
*/
|
||||||
private Uri|UriInterface $uri;
|
private Uri|UriInterface $uri;
|
||||||
|
|
||||||
|
|
||||||
private \Swoole\Http\Request $serverRequest;
|
private \Swoole\Http\Request $serverRequest;
|
||||||
|
|
||||||
|
|
||||||
private array $parseBody;
|
private array $parseBody;
|
||||||
|
|
||||||
private array $files = [];
|
private array $files = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var AuthIdentity|null
|
* @var AuthIdentity|null
|
||||||
*/
|
*/
|
||||||
public ?AuthIdentity $authority = null;
|
public ?AuthIdentity $authority = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getClientId(): int
|
public function getClientId(): int
|
||||||
{
|
{
|
||||||
return $this->serverRequest->fd;
|
return $this->serverRequest->fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param AuthIdentity $authority
|
* @param AuthIdentity $authority
|
||||||
*/
|
*/
|
||||||
public function setAuthority(AuthIdentity $authority)
|
public function setAuthority(AuthIdentity $authority)
|
||||||
{
|
{
|
||||||
$this->authority = $authority;
|
$this->authority = $authority;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Http\Request $request
|
* @param \Swoole\Http\Request $request
|
||||||
* @return RequestInterface
|
* @return RequestInterface
|
||||||
*/
|
*/
|
||||||
public static function parseRequest(\Swoole\Http\Request $request): RequestInterface
|
public static function parseRequest(\Swoole\Http\Request $request): RequestInterface
|
||||||
{
|
{
|
||||||
$message = new Request();
|
$message = new Request();
|
||||||
$message->uri = Uri::parseUri($request);
|
$message->uri = Uri::parseUri($request);
|
||||||
$message->method = $request->getMethod();
|
$message->method = $request->getMethod();
|
||||||
$message->requestTarget = '';
|
$message->requestTarget = '';
|
||||||
$message->serverRequest = $request;
|
$message->serverRequest = $request;
|
||||||
$message->version = $request->server['server_protocol'];
|
$message->version = $request->server['server_protocol'];
|
||||||
$message->stream = new Stream($request->getContent());
|
$message->stream = new Stream($request->getContent());
|
||||||
$message->servers = $request->server;
|
$message->servers = $request->server;
|
||||||
$message->parseRequestHeaders($request);
|
$message->parseRequestHeaders($request);
|
||||||
return $message;
|
return $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return float
|
* @return float
|
||||||
*/
|
*/
|
||||||
#[Pure] public function getStartTime(): float
|
#[Pure] public function getStartTime(): float
|
||||||
{
|
{
|
||||||
return $this->servers['request_time_float'];
|
return $this->servers['request_time_float'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param null $default
|
* @param null $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function input($name, $default = null): mixed
|
public function input($name, $default = null): mixed
|
||||||
{
|
{
|
||||||
if (empty($this->parseBody)) {
|
if (empty($this->parseBody)) {
|
||||||
$this->parseBody = $this->parseBody($this->stream);
|
$this->parseBody = $this->parseBody($this->stream);
|
||||||
}
|
}
|
||||||
if (!is_array($this->parseBody)) {
|
if (!is_array($this->parseBody)) {
|
||||||
return $default;
|
return $default;
|
||||||
}
|
}
|
||||||
return $this->parseBody[$name] ?? $default;
|
return $this->parseBody[$name] ?? $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param null $default
|
* @param null $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function post($name, $default = null): mixed
|
public function post($name, $default = null): mixed
|
||||||
{
|
{
|
||||||
return $this->serverRequest->post[$name] ?? $default;
|
return $this->serverRequest->post[$name] ?? $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|null
|
* @param $name
|
||||||
*/
|
* @param $required
|
||||||
public function gets(): ?array
|
* @return mixed
|
||||||
{
|
* @throws \Exception
|
||||||
return $this->serverRequest->get;
|
*/
|
||||||
}
|
public function string($name, $required)
|
||||||
|
{
|
||||||
|
if (is_null($data = $this->post($name))) {
|
||||||
|
if ($required) {
|
||||||
|
throw new \Exception('Parameter is required and cannot be empty.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param null $default
|
* @param $required
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
* @throws \Exception
|
||||||
public function query($name, $default = null): mixed
|
*/
|
||||||
{
|
public function int($name, $required)
|
||||||
return $this->serverRequest->get[$name] ?? $default;
|
{
|
||||||
}
|
if (is_null($data = $this->post($name))) {
|
||||||
|
if ($required) {
|
||||||
|
throw new \Exception('Parameter is required and cannot be empty.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (string)$data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return Uploaded|null
|
* @param $required
|
||||||
*/
|
* @return mixed
|
||||||
public function file($name): ?Uploaded
|
* @throws \Exception
|
||||||
{
|
*/
|
||||||
if (isset($this->serverRequest->files[$name])) {
|
public function float($name, $required)
|
||||||
return new Uploaded($this->serverRequest->files[$name]);
|
{
|
||||||
}
|
if (is_null($data = $this->post($name))) {
|
||||||
return null;
|
if ($required) {
|
||||||
}
|
throw new \Exception('Parameter is required and cannot be empty.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (float)$data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @param $name
|
||||||
*/
|
* @param $required
|
||||||
public function all(): array
|
* @return mixed
|
||||||
{
|
* @throws \Exception
|
||||||
if (empty($this->parseBody)) {
|
*/
|
||||||
$this->parseBody = $this->parseBody($this->stream);
|
public function array($name, $required)
|
||||||
}
|
{
|
||||||
return array_merge($this->serverRequest->post ?? [],
|
if (is_null($data = $this->post($name))) {
|
||||||
$this->serverRequest->get ?? [],
|
if ($required) {
|
||||||
is_array($this->parseBody) ? $this->parseBody : []
|
throw new \Exception('Parameter is required and cannot be empty.');
|
||||||
);
|
}
|
||||||
}
|
}
|
||||||
|
if (!is_array($data)) return [];
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return array|null
|
||||||
*/
|
*/
|
||||||
public function getRequestTarget(): string
|
public function gets(): ?array
|
||||||
{
|
{
|
||||||
throw new BadMethodCallException('Not Accomplish Method.');
|
return $this->serverRequest->get;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $requestTarget
|
* @param $name
|
||||||
* @return RequestInterface
|
* @param null $default
|
||||||
*/
|
* @return mixed
|
||||||
public function withRequestTarget($requestTarget): RequestInterface
|
*/
|
||||||
{
|
public function query($name, $default = null): mixed
|
||||||
|
{
|
||||||
|
return $this->serverRequest->get[$name] ?? $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
* @return Uploaded|null
|
||||||
|
*/
|
||||||
|
public function file($name): ?Uploaded
|
||||||
|
{
|
||||||
|
if (isset($this->serverRequest->files[$name])) {
|
||||||
|
return new Uploaded($this->serverRequest->files[$name]);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function all(): array
|
||||||
|
{
|
||||||
|
if (empty($this->parseBody)) {
|
||||||
|
$this->parseBody = $this->parseBody($this->stream);
|
||||||
|
}
|
||||||
|
return array_merge($this->serverRequest->post ?? [],
|
||||||
|
$this->serverRequest->get ?? [],
|
||||||
|
is_array($this->parseBody) ? $this->parseBody : []
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getRequestTarget(): string
|
||||||
|
{
|
||||||
|
throw new BadMethodCallException('Not Accomplish Method.');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $requestTarget
|
||||||
|
* @return RequestInterface
|
||||||
|
*/
|
||||||
|
public function withRequestTarget($requestTarget): RequestInterface
|
||||||
|
{
|
||||||
$this->requestTarget = $requestTarget;
|
$this->requestTarget = $requestTarget;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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 bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isMethod(string $method): bool
|
public function isMethod(string $method): bool
|
||||||
{
|
{
|
||||||
return $this->method === $method;
|
return $this->method === $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $method
|
* @param string $method
|
||||||
* @return RequestInterface
|
* @return RequestInterface
|
||||||
*/
|
*/
|
||||||
public function withMethod($method): RequestInterface
|
public function withMethod($method): RequestInterface
|
||||||
{
|
{
|
||||||
$class = clone $this;
|
$class = clone $this;
|
||||||
$class->method = $method;
|
$class->method = $method;
|
||||||
return $class;
|
return $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Server\Message\Uri|\Psr\Http\Message\UriInterface
|
* @return \Server\Message\Uri|\Psr\Http\Message\UriInterface
|
||||||
*/
|
*/
|
||||||
public function getUri(): Uri|UriInterface
|
public function getUri(): Uri|UriInterface
|
||||||
{
|
{
|
||||||
return $this->uri;
|
return $this->uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param UriInterface $uri
|
* @param UriInterface $uri
|
||||||
* @param false $preserveHost
|
* @param false $preserveHost
|
||||||
* @return RequestInterface
|
* @return RequestInterface
|
||||||
*/
|
*/
|
||||||
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
|
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
|
||||||
{
|
{
|
||||||
$class = clone $this;
|
$class = clone $this;
|
||||||
$class->uri = $uri;
|
$class->uri = $uri;
|
||||||
return $class;
|
return $class;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user