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;
} }
} }
+11 -70
View File
@@ -82,19 +82,7 @@ class HttpParams
*/ */
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);
}
} }
@@ -112,7 +100,7 @@ class HttpParams
*/ */
public function getBody(): ?array public function getBody(): ?array
{ {
return $this->__posts__(); return $this->_posts;
} }
@@ -132,7 +120,7 @@ class HttpParams
*/ */
public function query($name, $default = null): mixed public function query($name, $default = null): mixed
{ {
return $this->__gets__($name, $default); return $this->_gets[$name] ?? $default;
} }
@@ -152,7 +140,7 @@ class HttpParams
*/ */
public function get($name, $defaultValue = null): mixed public function get($name, $defaultValue = null): mixed
{ {
return $this->__gets__($name, $defaultValue); return $this->_gets[$name] ?? $defaultValue;
} }
/** /**
@@ -162,7 +150,7 @@ class HttpParams
*/ */
public function post($name, $defaultValue = null): mixed public function post($name, $defaultValue = null): mixed
{ {
return $this->__posts__($name, $defaultValue); return $this->_posts[$name] ?? $defaultValue;
} }
/** /**
@@ -186,7 +174,7 @@ class HttpParams
*/ */
public function gets(): array public function gets(): array
{ {
return $this->__gets__(); return $this->_gets;
} }
/** /**
@@ -194,7 +182,7 @@ class HttpParams
*/ */
public function params(): array public function params(): array
{ {
return array_merge($this->__posts__(), $this->__gets__()); return array_merge($this->_gets, $this->_posts);
} }
/** /**
@@ -202,7 +190,7 @@ class HttpParams
*/ */
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);
} }
/** /**
@@ -212,7 +200,7 @@ class HttpParams
*/ */
public function array($name, array $defaultValue = []): mixed public function array($name, array $defaultValue = []): mixed
{ {
return $this->__posts__($name, $defaultValue); return $this->_posts[$name] = $defaultValue;
} }
/** /**
@@ -223,7 +211,7 @@ class HttpParams
*/ */
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);
@@ -239,7 +227,7 @@ class HttpParams
*/ */
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");
} }
@@ -415,51 +403,4 @@ class HttpParams
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 ?? [];
}
} }
+1 -4
View File
@@ -46,7 +46,6 @@ 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();
@@ -60,9 +59,7 @@ class Request implements RequestInterface
$sRequest->params->setFiles($request->files); $sRequest->params->setFiles($request->files);
$sRequest->params->setPosts($request->post); $sRequest->params->setPosts($request->post);
$sRequest->params->setGets($request->get); $sRequest->params->setGets($request->get);
}catch (\Throwable $exception){
var_dump($exception);
}
Context::setContext(HttpResponse::class, $sRequest); Context::setContext(HttpResponse::class, $sRequest);