This commit is contained in:
as2252258@163.com
2021-08-05 22:47:10 +08:00
parent 733d4fbc88
commit 92164057aa
3 changed files with 353 additions and 434 deletions
+9 -28
View File
@@ -33,7 +33,7 @@ class HttpHeaders
*/ */
public function toArray(): array public function toArray(): array
{ {
return $this->__handler__(); return $this->_headers;
} }
/** /**
@@ -42,7 +42,7 @@ class HttpHeaders
*/ */
public function getHeader($name): ?string public function getHeader($name): ?string
{ {
return $this->__handler__($name); return $this->_headers[$name];
} }
@@ -53,7 +53,7 @@ class HttpHeaders
*/ */
public function get($name, $default = null): mixed public function get($name, $default = null): mixed
{ {
return $this->__handler__($name, $default); return $this->_headers[$name] ?? $default;
} }
@@ -62,7 +62,7 @@ class HttpHeaders
*/ */
public function getContentType(): string public function getContentType(): string
{ {
return $this->__handler__('content-type'); return $this->_headers['content-type'];
} }
@@ -71,11 +71,7 @@ class HttpHeaders
*/ */
public function getRequestUri(): ?string public function getRequestUri(): ?string
{ {
$uri = $this->__handler__('request_uri', '/'); return $this->_headers['request_uri'];
if (empty($uri)) {
return '/';
}
return $uri;
} }
@@ -84,7 +80,7 @@ class HttpHeaders
*/ */
public function getRequestMethod(): ?string public function getRequestMethod(): ?string
{ {
return $this->__handler__('request_method'); return $this->_headers['request_method'];
} }
@@ -93,7 +89,7 @@ class HttpHeaders
*/ */
public function getAgent(): mixed public function getAgent(): mixed
{ {
return $this->__handler__('user-agent'); return $this->_headers['user-agent'];
} }
@@ -103,7 +99,7 @@ class HttpHeaders
*/ */
public function exists($name): bool public function exists($name): bool
{ {
return $this->__handler__($name) === null; return $this->_headers[$name] ?? null === null;
} }
@@ -112,22 +108,7 @@ class HttpHeaders
*/ */
public function getHeaders(): array public function getHeaders(): array
{ {
return $this->__handler__(); return $this->_headers;
}
/**
* @param null $name
* @param null $default
* @return mixed
*/
private function __handler__($name = null, $default = null): mixed
{
$headers = Context::getContext(Request::class);
if (!empty($name)) {
return $headers->_headers[$name] ?? $default;
}
return $headers->_headers;
} }
} }
+333 -392
View File
@@ -24,442 +24,383 @@ use Snowflake\Snowflake;
class HttpParams class HttpParams
{ {
/** @var array|null */ /** @var array|null */
private ?array $_gets = []; private ?array $_gets = [];
/** @var mixed */ /** @var mixed */
private mixed $_posts = []; private mixed $_posts = [];
/** @var array|null */ /** @var array|null */
private ?array $_files = []; private ?array $_files = [];
private mixed $_rawContent = ''; private mixed $_rawContent = '';
/** /**
* @param array|null $gets * @param array|null $gets
*/ */
public function setGets(?array $gets): void public function setGets(?array $gets): void
{ {
$this->_gets = $gets; $this->_gets = $gets;
} }
/** /**
* @param mixed $posts * @param mixed $posts
*/ */
public function setPosts(mixed $posts): void public function setPosts(mixed $posts): void
{ {
$this->_posts = $posts; $this->_posts = $posts;
} }
/** /**
* @param array|null $files * @param array|null $files
*/ */
public function setFiles(?array $files): void public function setFiles(?array $files): void
{ {
$this->_files = $files; $this->_files = $files;
} }
/** /**
* @param mixed|string $rawContent * @param mixed|string $rawContent
*/ */
public function setRawContent(mixed $rawContent, string $context_type): void public function setRawContent(mixed $rawContent, string $context_type): void
{ {
if (str_contains($context_type, 'json')) { if (str_contains($context_type, 'json')) {
$this->_rawContent = json_decode($rawContent, true); $this->_rawContent = json_decode($rawContent, true);
} else if (str_contains($context_type, 'xml')) { } else if (str_contains($context_type, 'xml')) {
$this->_rawContent = Xml::toArray($rawContent); $this->_rawContent = Xml::toArray($rawContent);
} else { } else {
$this->_rawContent = $rawContent; $this->_rawContent = $rawContent;
} }
} }
/** /**
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function getRawContent(): mixed public function getRawContent(): mixed
{ {
if (Context::hasContext('rawContent')) { return $this->_rawContent;
return Context::getContext('rawContent'); }
}
/** @var \Swoole\Http\Request $context */
$content = Context::getContext(\Swoole\Http\Request::class);
$context_type = request()->headers->getContentType();
if (str_contains($context_type, 'json')) {
return Context::setContext('rawContent', json_decode($content, true));
} else if (str_contains($context_type, 'xml')) {
return Context::setContext('rawContent', Xml::toArray($content));
} else {
return Context::setContext('rawContent', $content);
}
}
/** /**
* @return int * @return int
*/ */
public function offset(): int public function offset(): int
{ {
return ($this->page() - 1) * $this->size(); return ($this->page() - 1) * $this->size();
} }
/** /**
* @return array|null * @return array|null
*/ */
public function getBody(): ?array public function getBody(): ?array
{ {
return $this->__posts__(); return $this->_posts;
} }
/** /**
* @return int * @return int
*/ */
private function page(): int private function page(): int
{ {
return (int)$this->get('page', 1); return (int)$this->get('page', 1);
} }
/** /**
* @param $name * @param $name
* @param null $default * @param null $default
* @return mixed * @return mixed
*/ */
public function query($name, $default = null): mixed public function query($name, $default = null): mixed
{ {
return $this->__gets__($name, $default); return $this->_gets[$name] ?? $default;
} }
/** /**
* @return int * @return int
*/ */
public function size(): int public function size(): int
{ {
return (int)$this->get('size', 20); return (int)$this->get('size', 20);
} }
/** /**
* @param $name * @param $name
* @param null $defaultValue * @param null $defaultValue
* @return mixed * @return mixed
*/ */
public function get($name, $defaultValue = null): mixed public function get($name, $defaultValue = null): mixed
{ {
return $this->__gets__($name, $defaultValue); return $this->_gets[$name] ?? $defaultValue;
} }
/** /**
* @param $name * @param $name
* @param null $defaultValue * @param null $defaultValue
* @return mixed * @return mixed
*/ */
public function post($name, $defaultValue = null): mixed public function post($name, $defaultValue = null): mixed
{ {
return $this->__posts__($name, $defaultValue); return $this->_posts[$name] ?? $defaultValue;
} }
/** /**
* @param $name * @param $name
* @return bool|string * @return bool|string
* @throws Exception * @throws Exception
*/ */
public function json($name): bool|string public function json($name): bool|string
{ {
$data = $this->array($name); $data = $this->array($name);
if (empty($data)) { if (empty($data)) {
return Json::encode([]); return Json::encode([]);
} else if (!is_array($data)) { } else if (!is_array($data)) {
return Json::encode([]); return Json::encode([]);
} }
return Json::encode($data); return Json::encode($data);
} }
/** /**
* @return array * @return array
*/ */
public function gets(): array public function gets(): array
{ {
return $this->__gets__(); return $this->_gets;
} }
/** /**
* @return array * @return array
*/ */
public function params(): array public function params(): array
{ {
return array_merge($this->__posts__(), $this->__gets__()); return array_merge($this->_gets, $this->_posts);
} }
/** /**
* @return array * @return array
*/ */
public function load(): array public function load(): array
{ {
return array_merge($this->__posts__(), $this->__gets__(), $this->__files__()); return array_merge($this->_files, $this->_gets, $this->_posts);
} }
/** /**
* @param $name * @param $name
* @param array $defaultValue * @param array $defaultValue
* @return mixed * @return mixed
*/ */
public function array($name, array $defaultValue = []): mixed public function array($name, array $defaultValue = []): mixed
{ {
return $this->__posts__($name, $defaultValue); return $this->_posts[$name] = $defaultValue;
} }
/** /**
* @param $name * @param $name
* @return File|null * @return File|null
* @throws ReflectionException * @throws ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function file($name): File|null public function file($name): File|null
{ {
$param = $this->__files__($name); $param = $this->_files[$name] ?? null;
if (!empty($param)) { if (!empty($param)) {
$param['class'] = File::class; $param['class'] = File::class;
return Snowflake::createObject($param); return Snowflake::createObject($param);
} }
return null; return null;
} }
/** /**
* @param string $name * @param string $name
* @param bool $isNeed * @param bool $isNeed
* @return mixed * @return mixed
* @throws RequestException * @throws RequestException
*/ */
private function required(string $name, bool $isNeed = false): mixed private function required(string $name, bool $isNeed = false): mixed
{ {
$int = $this->__posts__($name); $int = $this->_posts[$name] ?? null;
if (is_null($int) && $isNeed === true) { if (is_null($int) && $isNeed === true) {
throw new RequestException("You need to add request parameter $name"); throw new RequestException("You need to add request parameter $name");
} }
return $int; return $int;
} }
/** /**
* @param string $name * @param string $name
* @param bool $isNeed * @param bool $isNeed
* @param array|int|null $min * @param array|int|null $min
* @param int|null $max * @param int|null $max
* @return int|null * @return int|null
* @throws RequestException * @throws RequestException
*/ */
public function int(string $name, bool $isNeed = FALSE, array|int|null $min = NULL, int|null $max = NULL): ?int public function int(string $name, bool $isNeed = FALSE, array|int|null $min = NULL, int|null $max = NULL): ?int
{ {
return (int)$this->required($name, $isNeed); return (int)$this->required($name, $isNeed);
} }
/** /**
* @param string $name * @param string $name
* @param bool $isNeed * @param bool $isNeed
* @param int $round * @param int $round
* @return float|null * @return float|null
* @throws RequestException * @throws RequestException
*/ */
public function float(string $name, bool $isNeed = FALSE, int $round = 0): ?float public function float(string $name, bool $isNeed = FALSE, int $round = 0): ?float
{ {
return (float)$this->required($name, $isNeed); return (float)$this->required($name, $isNeed);
} }
/** /**
* @param string $name * @param string $name
* @param bool $isNeed * @param bool $isNeed
* @param int|array|null $length * @param int|array|null $length
* *
* @return string|null * @return string|null
* @throws RequestException * @throws RequestException
*/ */
public function string(string $name, bool $isNeed = FALSE, int|array|null $length = NULL): ?string public function string(string $name, bool $isNeed = FALSE, int|array|null $length = NULL): ?string
{ {
return (string)$this->required($name, $isNeed); return (string)$this->required($name, $isNeed);
} }
/** /**
* @param string $name * @param string $name
* @param bool $isNeed * @param bool $isNeed
* *
* @return string|null * @return string|null
* @throws RequestException * @throws RequestException
*/ */
public function email(string $name, bool $isNeed = FALSE): ?string public function email(string $name, bool $isNeed = FALSE): ?string
{ {
$email = $this->required($name, $isNeed); $email = $this->required($name, $isNeed);
if ($email === null) { if ($email === null) {
return null; return null;
} }
if (!preg_match('/^\w+([.-_]\w+)+@\w+(\.\w+)+$/', $email)) { if (!preg_match('/^\w+([.-_]\w+)+@\w+(\.\w+)+$/', $email)) {
throw new RequestException("Request parameter $name is in the wrong format", 4001); throw new RequestException("Request parameter $name is in the wrong format", 4001);
} }
return $email; return $email;
} }
/** /**
* @param string $name * @param string $name
* @param bool $isNeed * @param bool $isNeed
* *
* @return bool * @return bool
* @throws RequestException * @throws RequestException
*/ */
public function bool(string $name, bool $isNeed = FALSE): bool public function bool(string $name, bool $isNeed = FALSE): bool
{ {
return (boolean)$this->required($name, $isNeed); return (boolean)$this->required($name, $isNeed);
} }
/** /**
* @param string $name * @param string $name
* @param int|null $default * @param int|null $default
* *
* @return int|string|null * @return int|string|null
* @throws RequestException * @throws RequestException
*/ */
public function timestamp(string $name, int|null $default = NULL): null|int|string public function timestamp(string $name, int|null $default = NULL): null|int|string
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value === null) { if ($value === null) {
return $default; return $default;
} }
if (!is_numeric($value)) { if (!is_numeric($value)) {
throw new RequestException('The request param :attribute not is a timestamp value'); throw new RequestException('The request param :attribute not is a timestamp value');
} }
if (strlen((string)$value) != 10) { if (strlen((string)$value) != 10) {
throw new RequestException('The request param :attribute not is a timestamp value'); throw new RequestException('The request param :attribute not is a timestamp value');
} }
if (!date('YmdHis', $value)) { if (!date('YmdHis', $value)) {
throw new RequestException('The request param :attribute format error', 4001); throw new RequestException('The request param :attribute format error', 4001);
} }
return $value; return $value;
} }
/** /**
* @param string $name * @param string $name
* @param string|null $default * @param string|null $default
* *
* @return string|null * @return string|null
* @throws RequestException * @throws RequestException
*/ */
public function datetime(string $name, string $default = NULL): string|null public function datetime(string $name, string $default = NULL): string|null
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value === null) { if ($value === null) {
return $default; return $default;
} }
$match = '/^\d{4}.*?([1-12]).*([1-31]).*?[0-23].*?[0-59].*?[0-59].*?$/'; $match = '/^\d{4}.*?([1-12]).*([1-31]).*?[0-23].*?[0-59].*?[0-59].*?$/';
$match = preg_match($match, $value, $result); $match = preg_match($match, $value, $result);
if (!$match || $result[0] != $value) { if (!$match || $result[0] != $value) {
throw new RequestException('The request param :attribute format error', 4001); throw new RequestException('The request param :attribute format error', 4001);
} }
return $value; return $value;
} }
/** /**
* @param string $name * @param string $name
* @param string|null $default * @param string|null $default
* *
* @return string|null * @return string|null
* @throws RequestException * @throws RequestException
*/ */
public function date(string $name, string $default = NULL): string|null public function date(string $name, string $default = NULL): string|null
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value === null) { if ($value === null) {
return $default; return $default;
} }
$match = '/^\d{4}.*?([1-12]).*([1-31])$/'; $match = '/^\d{4}.*?([1-12]).*([1-31])$/';
$match = preg_match($match, $value, $result); $match = preg_match($match, $value, $result);
if (!$match || $result[0] != $value) { if (!$match || $result[0] != $value) {
throw new RequestException('The request param :attribute format error', 4001); throw new RequestException('The request param :attribute format error', 4001);
} }
return $value; return $value;
} }
/** /**
* @param string $name * @param string $name
* @param string|null $default * @param string|null $default
* @return string|null * @return string|null
* @throws RequestException * @throws RequestException
*/ */
public function ip(string $name, string $default = NULL): string|null public function ip(string $name, string $default = NULL): string|null
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value == NULL) { if ($value == NULL) {
return $default; return $default;
} }
$match = preg_match('/^\d{1,3}(\.\d{1,3}){3}$/', $value, $result); $match = preg_match('/^\d{1,3}(\.\d{1,3}){3}$/', $value, $result);
if (!$match || $result[0] != $value) { if (!$match || $result[0] != $value) {
throw new RequestException('The request param :attribute format error', 4001); throw new RequestException('The request param :attribute format error', 4001);
} }
return $value; return $value;
} }
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
*/ */
public function __get($name): mixed public function __get($name): mixed
{ {
$load = $this->load(); $load = $this->load();
return $load[$name] ?? null; return $load[$name] ?? null;
} }
/**
* @param null $name
* @param null $default
* @return mixed
*/
private function __posts__($name = null, $default = null): mixed
{
/** @var \Swoole\Http\Request $content */
$content = Context::getContext(\Swoole\Http\Request::class);
if (!empty($name)) {
return $content->post[$name] ?? $default;
}
return $content->post ?? [];
}
/**
* @param null $name
* @param null $default
* @return mixed
*/
private function __gets__($name = null, $default = null): mixed
{
/** @var \Swoole\Http\Request $content */
$content = Context::getContext(\Swoole\Http\Request::class);
if (!empty($name)) {
return $content->get[$name] ?? $default;
}
return $content->get ?? [];
}
/**
* @param null $name
* @return mixed
*/
private function __files__($name = null): mixed
{
/** @var Request $content */
$content = Context::getContext(Request::class);
if (!empty($name)) {
return $content->_f[$name] ?? null;
}
return $content->files ?? [];
}
} }
+11 -14
View File
@@ -46,23 +46,20 @@ class Request implements RequestInterface
{ {
Context::setContext(Response::class, new Response()); Context::setContext(Response::class, new Response());
try { $sRequest = new HttpResponse();
$sRequest = new HttpResponse();
$sRequest->headers = new HttpHeaders(); $sRequest->headers = new HttpHeaders();
$sRequest->headers->setHeaders(array_merge($request->header, $request->server)); $sRequest->headers->setHeaders(array_merge($request->header, $request->server));
$sRequest->setUri($sRequest->headers->getRequestUri()); $sRequest->setUri($sRequest->headers->getRequestUri());
$sRequest->setClientId($request->fd); $sRequest->setClientId($request->fd);
$sRequest->params = new HttpParams();
$sRequest->params->setRawContent($request->rawContent(), $sRequest->headers->getContentType());
$sRequest->params->setFiles($request->files);
$sRequest->params->setPosts($request->post);
$sRequest->params->setGets($request->get);
$sRequest->params = new HttpParams();
$sRequest->params->setRawContent($request->rawContent(), $sRequest->headers->getContentType());
$sRequest->params->setFiles($request->files);
$sRequest->params->setPosts($request->post);
$sRequest->params->setGets($request->get);
}catch (\Throwable $exception){
var_dump($exception);
}
Context::setContext(HttpResponse::class, $sRequest); Context::setContext(HttpResponse::class, $sRequest);