This commit is contained in:
2021-07-27 18:21:30 +08:00
parent 74532d2509
commit bd37881db7
3 changed files with 62 additions and 92 deletions
+25 -58
View File
@@ -16,32 +16,13 @@ namespace HttpServer\Http;
class HttpHeaders class HttpHeaders
{ {
/**
* @var string[]
*/
private array $headers = [];
/**
* @var string[]
*/
private array $response = [];
/**
* HttpHeaders constructor.
* @param $headers
*/
public function __construct($headers)
{
$this->headers = $headers;
}
/** /**
* @param string $uri * @param string $uri
*/ */
public function setRequestUri(string $uri) public function setRequestUri(string $uri)
{ {
$this->headers['request_uri'] = $uri; $this->replace('request_uri', $uri);
} }
@@ -50,36 +31,17 @@ class HttpHeaders
*/ */
public function setRequestMethod(string $method) public function setRequestMethod(string $method)
{ {
$this->headers['request_method'] = $method; $this->replace('request_method', $method);
} }
/**
* @param $name
* @param $value
*/
public function setHeader($name, $value)
{
$this->response[$name] = $value;
}
/**
* @param array $headers
*/
public function setHeaders(array $headers)
{
foreach ($headers as $key => $val) {
$this->response[$key] = $val;
}
}
/** /**
* @param $name * @param $name
* @param $value * @param $value
*/ */
public function replace($name, $value) public function replace($name, $value)
{ {
$this->headers[$name] = $value; $this->addHeaders([$name => $value]);
} }
/** /**
@@ -88,7 +50,7 @@ class HttpHeaders
*/ */
public function addHeader($name, $value) public function addHeader($name, $value)
{ {
$this->headers[$name] = $value; $this->addHeaders([$name => $value]);
} }
/** /**
@@ -100,27 +62,20 @@ class HttpHeaders
if (empty($headers)) { if (empty($headers)) {
return $this; return $this;
} }
if (!empty($this->headers)) { $request = Context::getContext('request');
$headers = array_merge($this->headers, $headers); if (!empty($request->headers)) {
$headers = array_merge($request->headers, $headers);
} }
$this->headers = $headers; $request->headers = $headers;
return $this; return $this;
} }
/**
* @return array
*/
public function getResponseHeaders(): array
{
return $this->response;
}
/** /**
* @return array * @return array
*/ */
public function toArray(): array public function toArray(): array
{ {
return $this->headers; return $this->___call();
} }
/** /**
@@ -129,10 +84,11 @@ class HttpHeaders
*/ */
public function getHeader($name): ?string public function getHeader($name): ?string
{ {
if (!isset($this->headers[$name])) { $headers = $this->___call();
if (!isset($headers[$name])) {
return null; return null;
} }
return $this->headers[$name]; return $headers[$name];
} }
@@ -156,7 +112,8 @@ class HttpHeaders
*/ */
public function exists($name): bool public function exists($name): bool
{ {
return isset($this->headers[$name]) && $this->headers[$name] != null; $headers = $this->___call();
return isset($headers[$name]) && $headers[$name] != null;
} }
@@ -165,7 +122,17 @@ class HttpHeaders
*/ */
public function getHeaders(): array public function getHeaders(): array
{ {
return $this->headers; return $this->___call();
} }
/**
* @return mixed
*/
private function ___call(): array
{
return Context::getContext('request')->header ?? [];
}
} }
+27 -22
View File
@@ -26,6 +26,7 @@ class HttpParams
private string|array|null $body = []; private string|array|null $body = [];
/** @var array */ /** @var array */
private array $gets = []; private array $gets = [];
@@ -33,19 +34,13 @@ class HttpParams
private array $files = []; private array $files = [];
private array $socket = []; private array $socket = [];
/** /**
* HttpParams constructor. * @return mixed
* @param mixed $body
* @param array|null $get
* @param array|null $files
* @param array $socket
*/ */
public function __construct(mixed $body, ?array $get, ?array $files, array $socket = []) public function getRawContent(): mixed
{ {
$this->gets = $get ?? []; return $this->getRequest()->rawContent();
$this->files = $files ?? [];
$this->socket = $socket ?? [];
$this->body = $body ?? '';
} }
/** /**
@@ -152,7 +147,7 @@ class HttpParams
*/ */
public function get($name, $defaultValue = null, $call = null): mixed public function get($name, $defaultValue = null, $call = null): mixed
{ {
return $this->gets[$name] ?? $defaultValue; return $this->getRequest()->get[$name] ?? $defaultValue;
} }
/** /**
@@ -163,7 +158,7 @@ class HttpParams
*/ */
public function post($name, $defaultValue = null, $call = null): mixed public function post($name, $defaultValue = null, $call = null): mixed
{ {
$data = $this->body[$name] ?? $defaultValue; $data = $this->getRequest()->post[$name] ?? $defaultValue;
if ($call !== null) { if ($call !== null) {
$data = call_user_func($call, $data); $data = call_user_func($call, $data);
} }
@@ -191,7 +186,7 @@ class HttpParams
*/ */
public function gets(): array public function gets(): array
{ {
return $this->gets; return $this->getRequest()->get;
} }
/** /**
@@ -199,7 +194,9 @@ class HttpParams
*/ */
#[Pure] public function params(): array #[Pure] public function params(): array
{ {
return array_merge($this->body ?? [], $this->files ?? []); $request = $this->getRequest();
return array_merge($request->post ?? [], $request->files ?? []);
} }
/** /**
@@ -207,7 +204,8 @@ class HttpParams
*/ */
#[Pure] public function load(): array #[Pure] public function load(): array
{ {
return array_merge($this->files ?? [], $this->body ?? [], $this->gets ?? [], $this->socket ?? []); $request = $this->getRequest();
return array_merge($request->files ?? [], $request->post ?? [], $request->gets ?? []);
} }
/** /**
@@ -217,7 +215,7 @@ class HttpParams
*/ */
public function array($name, array $defaultValue = []): mixed public function array($name, array $defaultValue = []): mixed
{ {
return $this->body[$name] ?? $defaultValue; return $this->getRequest()->post[$name] ?? $defaultValue;
} }
/** /**
@@ -228,10 +226,10 @@ class HttpParams
*/ */
public function file($name): File|null public function file($name): File|null
{ {
if (!isset($this->files[$name])) { $param = $this->getRequest()->files[$name] ?? null;
if (empty($param)) {
return null; return null;
} }
$param = $this->files[$name];
$param['class'] = File::class; $param['class'] = File::class;
return Snowflake::createObject($param); return Snowflake::createObject($param);
} }
@@ -244,9 +242,7 @@ class HttpParams
*/ */
private function required(string $name, bool $isNeed = false): mixed private function required(string $name, bool $isNeed = false): mixed
{ {
$body = array_merge($this->body ?? [], $this->socket ?? []); $int = $this->getRequest()->post[$name] ?? NULL;
$int = $body[$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");
} }
@@ -275,7 +271,7 @@ class HttpParams
*/ */
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);
} }
/** /**
@@ -422,4 +418,13 @@ class HttpParams
return $load[$name] ?? null; return $load[$name] ?? null;
} }
/**
* @return mixed
*/
private function getRequest(): \Swoole\Http\Request
{
return Context::getContext('request');
}
} }
+10 -12
View File
@@ -7,8 +7,9 @@ use Exception;
use HttpServer\Abstracts\HttpService; use HttpServer\Abstracts\HttpService;
use HttpServer\IInterface\AuthIdentity; use HttpServer\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Snowflake\Core\Help; use ReflectionException;
use Snowflake\Core\Json; use Snowflake\Core\Json;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use function router; use function router;
@@ -445,22 +446,19 @@ class Request extends HttpService
/** /**
* @param \Swoole\Http\Request $request * @param \Swoole\Http\Request $request
* @return mixed * @return Request
* @throws ReflectionException
* @throws NotFindClassException
*/ */
public static function create(\Swoole\Http\Request $request): Request public static function create(\Swoole\Http\Request $request): Request
{ {
$request->header = array_merge($request->header, $request->server);
Context::setContext('request', $request);
/** @var Request $sRequest */ /** @var Request $sRequest */
$sRequest = Context::setContext('request', new Request()); $sRequest = Snowflake::getDi()->get(Request::class);
$sRequest->fd = $request->fd; $sRequest->params = di(HttpParams::class);
$sRequest->startTime = microtime(true); $sRequest->headers = di(HttpHeaders::class);
$sRequest->params = new HttpParams(Help::toArray($request->rawContent()), $request->get, $request->files);
if (!empty($request->post)) {
$sRequest->params->setPosts($request->post ?? []);
}
$sRequest->headers = new HttpHeaders(array_merge($request->server, $request->header));
$sRequest->uri = $sRequest->headers->get('request_uri'); $sRequest->uri = $sRequest->headers->get('request_uri');